3

我正在尝试使用 dask 处理不适合内存的数据集。这是各种“ID”的时间序列数据。在阅读了 dask 文档后,我选择使用“parquet”文件格式并按“ID”进行分区。

但是,在从镶木地板中读取并设置索引时,我遇到了“TypeError: to union ordered Categoricals, all categories must be the same”,这是我自己无法解决的。

此代码复制了我遇到的问题:

import dask.dataframe as dd
import numpy as np
import pandas as pd
import traceback

# create ids
ids = ["AAA", "BBB", "CCC", "DDD"]

# create data
df = pd.DataFrame(index=np.random.choice(ids, 50), data=np.random.rand(50, 1), columns=["FOO"]).reset_index().rename(columns={"index": "ID"})
# serialize  to parquet
f = r"C:/temp/foo.pq"
df.to_parquet(f, compression='gzip', engine='fastparquet', partition_cols=["ID"])
# read with dask
df = dd.read_parquet(f)

try:
    df = df.set_index("ID")
except Exception as ee:
    print(traceback.format_exc())

此时我收到以下错误:

~\.conda\envs\env_dask_py37\lib\site-packages\pandas\core\arrays\categorical.py in check_for_ordered(self, op)
   1492         if not self.ordered:
   1493             raise TypeError(
-> 1494                 f"Categorical is not ordered for operation {op}\n"
   1495                 "you can use .as_ordered() to change the "
   1496                 "Categorical to an ordered one\n"

TypeError: Categorical is not ordered for operation max
you can use .as_ordered() to change the Categorical to an ordered one

然后我做了:

# we order the categorical
df.ID = df.ID.cat.as_ordered()
df = df.set_index("ID")

而且,当我尝试使用时df.compute(scheduler="processes"),我得到了我之前提到的 TypeError:

try:
    schd_str = 'processes'
    aa = df.compute(scheduler=schd_str)
    print(f"{schd_str}: OK")
except:
    print(f"{schd_str}: KO")
    print(traceback.format_exc())

给出:

Traceback (most recent call last):
  File "<ipython-input-6-e15c4e86fee2>", line 3, in <module>
    aa = df.compute(scheduler=schd_str)
  File "C:\Users\xxx\.conda\envs\env_dask_py37\lib\site-packages\dask\base.py", line 166, in compute
    (result,) = compute(self, traverse=False, **kwargs)
  File "C:\Users\xxx\.conda\envs\env_dask_py37\lib\site-packages\dask\base.py", line 438, in compute
    return repack([f(r, *a) for r, (f, a) in zip(results, postcomputes)])
  File "C:\Users\xxx\.conda\envs\env_dask_py37\lib\site-packages\dask\base.py", line 438, in <listcomp>
    return repack([f(r, *a) for r, (f, a) in zip(results, postcomputes)])
  File "C:\Users\xxx\.conda\envs\env_dask_py37\lib\site-packages\dask\dataframe\core.py", line 103, in finalize
    return _concat(results)
  File "C:\Users\xxx\.conda\envs\env_dask_py37\lib\site-packages\dask\dataframe\core.py", line 98, in _concat
    else methods.concat(args2, uniform=True, ignore_index=ignore_index)
  File "C:\Users\xxx\.conda\envs\env_dask_py37\lib\site-packages\dask\dataframe\methods.py", line 383, in concat
    ignore_index=ignore_index,
  File "C:\Users\xxx\.conda\envs\env_dask_py37\lib\site-packages\dask\dataframe\methods.py", line 431, in concat_pandas
    ind = concat([df.index for df in dfs])
  File "C:\Users\xxx\.conda\envs\env_dask_py37\lib\site-packages\dask\dataframe\methods.py", line 383, in concat
    ignore_index=ignore_index,
  File "C:\Users\xxx\.conda\envs\env_dask_py37\lib\site-packages\dask\dataframe\methods.py", line 400, in concat_pandas
    return pd.CategoricalIndex(union_categoricals(dfs), name=dfs[0].name)
  File "C:\Users\xxx\.conda\envs\env_dask_py37\lib\site-packages\pandas\core\dtypes\concat.py", line 352, in union_categoricals
    raise TypeError("Categorical.ordered must be the same")
TypeError: Categorical.ordered must be the same

令人惊讶的是,使用df.compute(scheduler="threads"),df.compute(scheduler="synchronous")或根本不设置索引都可以正常工作。

但是,这似乎不是我应该做的事情,因为我实际上是在尝试合并其中的几个数据集,并且认为设置索引会比不设置任何索引更快。(尝试合并以这种方式索引的两个数据帧时,我遇到了同样的错误)

我试图检查 df._meta,结果发现我的类别应该是“已知的”?分类

我还阅读了这篇 github 帖子,内容看起来很相似,但不知何故没有找到解决方案。

谢谢你的帮助,

4

0 回答 0