본문 바로가기
C.W.K.
Stream
Lesson 04 of 06 · published

Polars — Pandas 가 벽에 부딪힐 때

~13 min · polars, performance, lazy

Level 0구경꾼
0 XP0/47 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

Rust 로 지은 대안

Polars (2026.8 기준 1.43.1) 는 Rust 로 쓰인 DataFrame 라이브러리고, Python 바인딩으로 써. In-memory 포맷은 Arrow 고, query 는 기본으로 병렬 실행되고, API 는 두 벌이야 — eager (Pandas 쓰던 손에 익숙한 쪽) 와 lazy (query 를 plan 으로 쌓아 최적화한 뒤 한 번에 실행하는 쪽, SQL 처럼).

Pandas 대신 Polars 로 손이 갈 때

  • 데이터가 메모리보다 큰데 분산 시스템까지는 필요 없을 때. pl.scan_* + lazy 평가가 stream 으로 흘려 줘.
  • Transformation 이 많아서 Pandas 가 분 단위로 걸릴 때. Polars 의 병렬 실행이 그걸 보통 초 단위로 끌어내려.
  • Query planner 가 필요할 때. Polars 는 최적화해서 돌리고, Pandas 는 적힌 그대로 돌려.
  • Core 많은 머신에서 Pandas 가 core 를 놀릴 때.

Pandas 에 머무를 때

  • Ecosystem 라이브러리 (statsmodels, sklearn, plotnine) 대부분이 Pandas DataFrame 을 그대로 받아 줄 때.
  • 데이터가 메모리에 여유 있게 들어가고, 속도 차이가 체감도 안 될 만큼 작을 때.
  • 탐색 중이라 chain 중간중간 print 를 찍고 싶을 때 — Polars 의 lazy plan 은 .collect() 전엔 중간 결과를 안 보여 줘.

Code

Polars eager API — Pandas 같은 느낌, 근데 default 병렬·python
import polars as pl

df = pl.read_parquet('orders.parquet')

monthly = (
    df.filter(pl.col('status') == 'completed')
      .with_columns(pl.col('order_date').dt.truncate('1mo').alias('month'))
      .group_by('month')
      .agg([
          pl.col('amount_usd').sum().alias('revenue'),
          pl.col('order_id').n_unique().alias('orders'),
      ])
      .sort('month')
)
Polars lazy API — query 빌드, 최적화, 한 번에 실행·python
import polars as pl

monthly = (
    pl.scan_parquet('warehouse/orders/year=2026/**/*.parquet')
      .filter(pl.col('status') == 'completed')
      .with_columns(pl.col('order_date').dt.truncate('1mo').alias('month'))
      .group_by('month')
      .agg([
          pl.col('amount_usd').sum().alias('revenue'),
          pl.col('order_id').n_unique().alias('orders'),
      ])
      .sort('month')
      .collect(streaming=True)   # 청크로 stream; 메모리 큰 것도 작동
)

# Polars 가 실제 실행한 최적화된 plan 살펴보기
print(
    pl.scan_parquet('warehouse/orders/year=2026/**/*.parquet')
      .filter(pl.col('status') == 'completed')
      .explain(optimized=True)
)

External links

Exercise

Pandas 로 짜 두었던 monthly-revenue 계산을 Polars (lazy) 로 다시 써 봐. %timeit 스타일로 둘 다 재. 파일이 클수록 Polars 가 더 크게 이긴다는 것 — 그리고 .explain() 으로 본 lazy plan 이 진짜 DB 의 query plan 처럼 생겼다는 것까지 확인해.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.