Upgrade to Pro — share decks privately, control downloads, hide ads and more …

polars 1.1.0以降の使える新機能

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for Tatsuya Ute Tatsuya Ute
May 10, 2025
310

polars 1.1.0以降の使える新機能

Avatar for Tatsuya Ute

Tatsuya Ute

May 10, 2025
Tweet

Transcript

  1. © 2025 Wantedly, Inc. 自己紹介 • Tatsuya Ute • データサイエンティスト

    @ウォンテッドリー • 趣味:カラオケ、旅行、散歩 • X: @ghibney
  2. © 2025 Wantedly, Inc. Polars 1.0.0 までの話 Polars 1.0.0 までは、Polars

    Data Crunch #2 で LT 発表されています https://speakerdeck.com/zerebom/polarsnocheng-chang-v0-dot-14karav1-dot-0madenobian-qian-tojin-hou-nozhan-wang
  3. © 2025 Wantedly, Inc. Polars 1.0.0 までのリリース 各マイナーバージョンのリリースに、複数のパッチをリリースしています Ver リリース日

    リリース回数 前バージョンからの 経過日数 0.14 2022-10-03 14 0.15 2022-11-26 13 54 0.16 2023-01-29 18 64 0.17 2023-04-08 16 69 0.18 2023-05-29 16 51 0.19 2023-08-30 22 93 0.20 2023-12-16 37 108 1.0 2024-06-11 5 178 https://speakerdeck.com/zerebom/polarsnocheng-chang-v0-dot-14karav1-d ot-0madenobian-qian-tojin-hou-nozhan-wang をデータ取得しています
  4. © 2025 Wantedly, Inc. Polars 1.1.0 以降のリリース 1.1.0以降は、どんどんマイナーバージョンの更新が行われています Ver リリース日

    リリース回 数 前バージョンからの 経過日数 1.1 2024-07-07 1 26 1.2 2024-07-16 2 9 1.3 2024-07-28 1 12 1.4 2024-08-02 2 5 1.5 2024-08-14 1 12 1.6 2024-08-28 1 14 1.7 2024-09-11 2 14 1.8 2024-09-23 3 12 1.9 2024-10-01 1 8 1.10 2024-10-20 1 19 1.11 2024-10-23 1 3 1.12 2024-10-27 1 4 1.13 2024-11-12 2 16 1.14 2024-11-17 1 5 1.15 2024-11-25 1 8 Ver リリース日 リリース回 数 前バージョンからの 経過日数 1.16 2024-11-29 1 4 1.17 2024-12-08 2 9 1.18 2024-12-24 1 16 1.19 2025-01-03 1 10 1.20 2025-01-16 1 13 1.21 2025-01-24 1 8 1.22 2025-02-08 1 15 1.23 2025-02-23 1 15 1.24 2025-03-02 1 7 1.25 2025-03-15 1 13 1.26 2025-03-23 1 8 1.27 2025-04-09 2 17 1.28 2025-04-26 2 17 1.29 2025-04-30 1 4 https://speakerdeck.com/zerebom/polarsnocheng-chang-v0-dot-14karav1-d ot-0madenobian-qian-tojin-hou-nozhan-wang をデータ取得しています
  5. © 2025 Wantedly, Inc. join_where (since ver 1.7.0) cross join

    + filter を一度に実行できる experimental left = pl.DataFrame({"l_id": [100, 101, 102],               "l_time": [120, 140, 160],}) right = pl.DataFrame({"r_id": [404, 498, 676],               "r_time": [90, 130, 150],}) # Before left.join(right, how="cross").filter(   pl.col("l_time") < pl.col("r_time") ) # After left.join_where( right, pl.col("l_time") < pl.col("r_time") ) https://docs.pola.rs/api/python/stable/reference/dataframe/api/polars.DataFram e.join_where.html#polars.DataFrame.join_where
  6. © 2025 Wantedly, Inc. insert_column が expression に対応 (since ver

    1.9.0) Series で与えていた部分が、expression に対応した df = pl.DataFrame( { "id": ["xx", "yy", "zz"], "v1": [5, 4, 6], } ) # Before (insert_columnを使う場合) s = pl.Series(df.select((pl.col("v1") - 1).alias("v0"))) df.insert_column(1, s) # After df.insert_column( 1, (pl.col("v1") - 1).alias("v0") ) expression が使える https://docs.pola.rs/api/python/stable/reference/dataframe/api/polars.DataFrame.insert_ column.html#polars.DataFrame.insert_column
  7. © 2025 Wantedly, Inc. join が maintain_order パラメータを採用 (since ver

    1.17.0) join で順序を保持する方向(データフレーム)を指定できる left = pl.DataFrame({"a": [2, 1, 5]}) right = pl.DataFrame({"a": [1, 2], "b": [6, 9]}) # 左のデータフレームの順序を保持 left.join(right, on="a", how="inner", maintain_order="left") # 右のデータフレームの順序を保持 left.join(right, on="a", how="inner", maintain_order="right") “left” “right” https://docs.pola.rs/api/python/stable/reference/dataframe/api/polars.DataFrame.join.htm l#polars-dataframe-join
  8. © 2025 Wantedly, Inc. dt.replace (since ver 1.18.0) date 型のカラムに対して

    replace できる from datetime import date df = pl.DataFrame( { "date": [ date(2024, 4, 1), date(2025, 3, 16), ], "new_day": [10, 15], } ) df.with_columns( pl.col("date") .dt.replace(day="new_day") .alias("replaced") ) https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.Expr.dt.r eplace.html#polars.Expr.dt.replace
  9. © 2025 Wantedly, Inc. index_of (since ver 1.20.0) 特定の要素における index

    を検索できる(複数ある場合は最初の index) df = pl.DataFrame({"a": [1, None, 17, 1]}) df.select( [ pl.col("a").index_of(17).alias("seventeen"), pl.col("a").index_of(None).alias("null"), pl.col("a").index_of(55).alias("fiftyfive"), pl.col("a").index_of(1).alias("one"), ] ) https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.Expr.index_of.htm l#polars.Expr.index_of
  10. © 2025 Wantedly, Inc. str.normalize (since ver 1.20.0) テキストを Unicode

    正規化できる df = pl.DataFrame( {"original": ["01²", "KADOKAWA"]} ) df.with_columns( pl.col("original") .str.normalize("NFKC") .alias("normalized"), ) https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.Expr.str.normalize .html#polars.Expr.str.normalize
  11. © 2025 Wantedly, Inc. read_csv が lossy decoding に対応 (since

    ver 1.24.0) 一部読み取れない文字があっても、� で置き換えて decode できる file_path = "invalid_utf8.csv" with open(file_path, "wb") as f: f.write(b"id, hoge\n") f.write(b"010, huga\n") f.write(b"011, \xc0") # UTF-8では読み取れない pl.read_csv( file_path, encoding="utf-8-lossy", ) https://docs.pola.rs/api/python/stable/reference/api/polars.read_csv.html#polars-read-csv
  12. © 2025 Wantedly, Inc. dt.is_business_day (since ver 1.27.0) 祝日や曜日を指定して、営業日列を作成できる from

    datetime import date df = pl.DataFrame({"date": [date(2025, 1, 1), date(2025, 5, 10), date(2025, 5, 11)]}) holidays = [date(2025, 1, 1)] week_mask = (True, True, True, True, True, True, False) df.with_columns( is_business_day=pl.col("date") .dt.is_business_day(holidays=holidays, week_mask=week_mask) ) Unstable 月、火、...、日 の順番 https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.Expr.dt.is_busin ess_day.html#polars.Expr.dt.is_business_day
  13. © 2025 Wantedly, Inc. 1.1.0以降の破壊的変更 version 変更内容 1.6.0 ※ df.plot

    のバックボーンが hvplot から Altair に変更 1.16.0 join に maintain_order パラメータが追加されたことで、 今後はleft join による順序の保証しない (現状、保証はされていないが、 left で保持されている) 1.27.0 pl.Seriesの hist の挙動が変更 1.27.0 ※ streaming API で複数ファイルに分けて書き出しする際の 記法が変更 1.1.0以降、破壊的変更が4件あるのでお気をつけください ※ Unstable Breaking Change