Polars 分类功能和惰性 api 无法按预期工作

seb*_*704 6 python python-polars

我正在尝试借助分类功能和惰性 API 来连接两个 Dataframe。我尝试按照用户指南中描述的方式进行操作(https://pola-rs.github.io/polars-book/user-guide/performance/strings.html

count = admin_df.groupby(['admin','EVENT_DATE']).pivot(pivot_column='FIVE_TYPE',values_column='count').first().lazy()
fatalities = admin_df.groupby(['admin','EVENT_DATE']).pivot(pivot_column='FIVE_TYPE',values_column='FATALITIES').first().lazy()
fatalities = fatalities.with_column(pl.col("admin").cast(pl.Categorical))
count = count.with_column(pl.col("admin").cast(pl.Categorical))
admin_df = fatalities.join(count,on=['admin','EVENT_DATE']).collect()
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

    Traceback (most recent call last):
  File "country_level.py", line 33, in <module>
    country_level('/c/Users/Sebastian/feast/fluent_sunfish/data/ACLED_geocoded.parquet')
  File "country_level.py", line 10, in country_level
    country_df=aggregate_by_date(df)
  File "country_level.py", line 29, in aggregate_by_date
    admin_df = fatalities.join(count,on=['admin','EVENT_DATE']).collect()
  File "/home/sebastian/.local/lib/python3.8/site-packages/polars/internals/lazy_frame.py", line 293, in collect
    return pli.wrap_df(ldf.collect())
RuntimeError: Any(ValueError("joins on categorical dtypes can only happen if they are created under the same global string cache"))
Run Code Online (Sandbox Code Playgroud)

使用with pl.StringCache():一切都可以正常工作,尽管用户指南说如果您使用惰性 API 则不需要它,但我是否遗漏了某些内容或者这是一个错误?

rit*_*e46 6

用户指南不正确。您需要设置全局字符串缓存。

\n

pl.StringCache():您可以使用或 来设置全局字符串缓存pl.Config.set_global_string_cache

\n
\nimport polars as pl\npl.Config.set_global_string_cache()\n\nlf1 = pl.DataFrame({\n    "a": ["foo", "bar", "ham"], \n    "b": [1, 2, 3]\n}).lazy()\nlf2 = pl.DataFrame({\n    "a": ["foo", "spam", "eggs"], \n    "c": [3, 2, 2]\n}).lazy()\n\nlf1 = lf1.with_column(pl.col("a").cast(pl.Categorical))\nlf2 = lf2.with_column(pl.col("a").cast(pl.Categorical))\n\nlf1.join(df2, on="a", how="inner").collect()\n
Run Code Online (Sandbox Code Playgroud)\n

输出:

\n
shape: (1, 3)\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 a     \xe2\x94\x86 b   \xe2\x94\x86 c   \xe2\x94\x82\n\xe2\x94\x82 ---   \xe2\x94\x86 --- \xe2\x94\x86 --- \xe2\x94\x82\n\xe2\x94\x82 cat   \xe2\x94\x86 i64 \xe2\x94\x86 i64 \xe2\x94\x82\n\xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1\n\xe2\x94\x82 "foo" \xe2\x94\x86 1   \xe2\x94\x86 3   \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\n\n
Run Code Online (Sandbox Code Playgroud)\n

  • 现在是“polars.toggle_string_cache(True)” (4认同)
  • 现在是“polars.enable_string_cache(True)”:) (2认同)