简体   繁体   中英

dask dataframe head() returns empty df

I have a dask dataframe with an index on one of the columns. The issue is if I do a df.head() it always treturns an empty df, whereas df.tail always returns the correct df. I checked df.head always checks for the first n entries in the first partition. So if i do df.reset_index(), it should work but thats not the case

Below is the code to reproduce this:

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)

This works as expected for me

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM