dask dataframe head() returns empty df

[亡魂溺海] 提交于 2020-07-21 04:36:29

问题


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())

回答1:


Try calling head with npartitions=-1, to use all partitions (by default, only the first is used, and there may not be enough elements to return the head).

daskDf.head(npartitions=-1)



回答2:


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


来源:https://stackoverflow.com/questions/50524050/dask-dataframe-head-returns-empty-df

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!