迭代使用日期列上的 group_by 创建的组

问题描述 投票:0回答:1

更新:已通过pull/18251修复


我是 Polars 新手。我想迭代通过对列进行分组而创建的组,其中该列的每个单元格都包含两个日期的列表。我使用以下(示例)代码来实现,它曾经在

polars==0.20.18
版本中正常工作:

import polars as pl
import datetime

dt_str = [{'ts': datetime.date(2023, 7, 1), 'files': 'AGG_202307.xlsx',
           'period_bins': [datetime.date(2023, 7, 1), datetime.date(2024, 1, 1)]},
          {'ts': datetime.date(2023, 8, 1), 'files': 'AGG_202308.xlsx',
           'period_bins': [datetime.date(2023, 7, 1), datetime.date(2024, 1, 1)]},
          {'ts': datetime.date(2023, 11, 1), 'files': 'KFC_202311.xlsx',
           'period_bins': [datetime.date(2023, 7, 1), datetime.date(2024, 1, 1)]},
          {'ts': datetime.date(2024, 2, 1), 'files': 'KFC_202402.xlsx',
           'period_bins': [datetime.date(2024, 1, 1), datetime.date(2024, 7, 1)]}]

dt = pl.from_dicts(dt_str)

df_groups = dt.group_by("period_bins")
print(df_groups.all().to_dicts())

上面的代码不适用于

polars==1.x
并给出以下错误:

thread 'polars-0' panicked at crates/polars-row/src/encode.rs:289:15:
not implemented: Date32
thread 'polars-1' panicked at crates/polars-row/src/encode.rs:289:15:
not implemented: Date32
Traceback (most recent call last):
  File "testpad.py", line 18, in <module>
    print(df_groups.all().to_dicts())
  File "python3.10/site-packages/polars/dataframe/group_by.py", line 430, in all
    return self.agg(F.all())
  File "python3.10/site-packages/polars/dataframe/group_by.py", line 228, in agg
    self.df.lazy()
  File "python3.10/site-packages/polars/lazyframe/frame.py", line 2027, in collect
    return wrap_df(ldf.collect(callback))
pyo3_runtime.PanicException: not implemented: Date32

如何修复此错误?

python datetime python-polars
1个回答
1
投票

您可以按

.hash()
(或强制转换)进行分组作为解决方法。

(df.group_by(pl.col("period_bins").hash().alias("key"))
   .all()
)
shape: (2, 4)
┌─────────────────────┬─────────────────────────────────┬─────────────────────────────────┬─────────────────────────────────┐
│ key                 ┆ ts                              ┆ files                           ┆ period_bins                     │
│ ---                 ┆ ---                             ┆ ---                             ┆ ---                             │
│ u64                 ┆ list[date]                      ┆ list[str]                       ┆ list[list[date]]                │
╞═════════════════════╪═════════════════════════════════╪═════════════════════════════════╪═════════════════════════════════╡
│ 6836989170623494942 ┆ [2023-07-01, 2023-08-01, 2023-… ┆ ["AGG_202307.xlsx", "AGG_20230… ┆ [[2023-07-01, 2024-01-01], [20… │
│ 2692156858231355433 ┆ [2024-02-01]                    ┆ ["KFC_202402.xlsx"]             ┆ [[2024-01-01, 2024-07-01]]      │
└─────────────────────┴─────────────────────────────────┴─────────────────────────────────┴─────────────────────────────────┘
© www.soinside.com 2019 - 2024. All rights reserved.