【发布时间】:2020-02-03 02:21:24
【问题描述】:
我有一系列 Dask 数据帧。
我想索引到这个系列以获取 dask 数据帧以供后续工作。
但是,使用 loc[0] 会导致另一个 dask 系列。使用 to_frame 也不起作用,因为结果是“Dask 系列 Dask 数据帧的 Dask 数据帧”。
这是一个使用 Dask 系列 Pandas 数据帧的最小示例(不完全相同,但说明了问题):
import pandas as pd
import dask.dataframe as dd
pdf1 = pd.DataFrame({'a': [1,2,3,4], 'b': [4,3,2,1]})
pdf2 = pd.DataFrame({'a': [4,3,2,1], 'b': [1,2,3,4]})
ps = pd.Series([pdf1, pdf2])
ds = dd.from_pandas(ps, npartitions=1)
print(type(ds.loc[0])) # still dask series
print(type(ds.loc[1])) # still dask series
print(ds.compute().loc[0]) # this is a pandas dataframe
print(ds.loc[0].compute()) # this is a pandas series
print(ds.loc[0].compute().loc[0]) # need to index into the singleton series to get back the dataframe
似乎一旦对象变成了 dask 系列,所有后续的组合器都会将其保持为 dask 系列,直到 compute 返回一个“单例”pandas 系列。
无论如何要告诉 dask 将索引对象视为我期望的类型吗?
【问题讨论】: