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.4 기준 1.39.3) 는 Rust 로 쓰인 DataFrame 라이브러리, Python 바인딩 있음. Arrow 를 in-memory 포맷으로 사용, default 로 병렬 query 실행, eager API (Pandas 사용자 친숙) 와 lazy API (query 가 plan 으로 빌드되고 최적화돼서 한 번에 실행 — SQL 처럼) 둘 다 있어.

Pandas 대신 Polars 잡을 때

  • 데이터가 메모리보다 큰데 분산 시스템 필요 없을 때. pl.scan_* + lazy eval 이 stream.
  • 많은 transformation 하는 중 Pandas 가 분 단위 걸릴 때. Polars 의 병렬 실행이 보통 그걸 초로 줄임.
  • Query planner 원할 때. Polars 는 최적화하고 Pandas 는 본인이 쓴 그대로 실행.
  • Many-core 머신에서 Pandas 가 core 놀릴 때.

Pandas 에 머무를 때

  • 많은 ecosystem 라이브러리 (statsmodels, sklearn, plotnine) 가 Pandas DataFrame native 받음.
  • 데이터가 메모리에 쾌적하고 속도 향상 안 중요할 정도로 작음.
  • 탐색 작업 중 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

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

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