DASK 数据帧 head() 返回空的 df



我有一个 dask 数据帧,其中一列上有一个索引。 问题是如果我做一个 df.head((,它总是返回一个空的 df,而 df.tail 总是返回正确的 df。 我检查了 df.head 总是检查第一个分区中的前 n 个条目。因此,如果我执行 df.reset_index((,它应该可以工作,但事实并非如此

下面是重现此内容的代码:

import dask.dataframe as dd
import pandas as pd
data = pd.DataFrame({
'i64': np.arange(1000, dtype=np.int64),
'Ii32': np.arange(1000, dtype=np.int32),
'bhello': np.random.choice(['hello', 'Yo', 'people'], size=1000).astype("O")
})

daskDf = dd.from_pandas(data, chunksize=3)
daskDf = daskDf.set_index('bhello')
print(daskDf.head())

尝试使用npartitions=-1调用head以使用所有分区(默认情况下,仅使用第一个分区,并且可能没有足够的元素返回head(。

daskDf.head(npartitions=-1)

这对我来说是预期的

In [1]: import numpy as np
In [2]: import dask.dataframe as dd
...: import pandas as pd
...: 
...: data = pd.DataFrame({
...:      'i64': np.arange(1000, dtype=np.int64),
...:      'Ii32': np.arange(1000, dtype=np.int32),
...:      'bhello': np.random.choice(['hello', 'Yo', 'people'], size=1000).as
...: type("O")
...: })
...: 
In [3]: daskDf = dd.from_pandas(data, chunksize=3)
In [4]: daskDf
Out[4]: 
Dask DataFrame Structure:
Ii32  bhello    i64
npartitions=333                      
0                int32  object  int64
3                  ...     ...    ...
...                ...     ...    ...
996                ...     ...    ...
999                ...     ...    ...
Dask Name: from_pandas, 333 tasks
In [5]: daskDf.head()
/home/mrocklin/workspace/dask/dask/dataframe/core.py:4221: UserWarning: Insufficient elements for `head`. 5 elements requested, only 3 elements available. Try passing larger `npartitions` to `head`.
warnings.warn(msg.format(n, len(r)))
Out[5]: 
Ii32 bhello  i64
0     0     Yo    0
1     1     Yo    1
2     2  hello    2

最新更新