我是dask的新手,正在尝试弄清楚如何重塑从dask数据帧的单个列中获得的dask数组,但遇到了错误。想知道有没有人知道这个修复方法(不需要强行计算)?谢谢!
示例:
import pandas as pd
import numpy as np
from dask import dataframe as dd, array as da
df = pd.DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]})
ddf = dd.from_pandas(df, npartitions=2)
# This does not work - error ValueError: cannot convert float NaN to integer
ddf['x'].values.reshape([-1,1])
# this works, but requires a compute
ddf['x'].values.compute().reshape([-1,1])
# this works, if the dask array is created directly from a np array
ar = np.array([1, 2, 3])
dar = da.from_array(ar, chunks=2)
dar.reshape([-1,1])发布于 2019-07-30 07:12:50
另外:
ddf['x'].to_dask_array(lengths=True).reshape([-1,1])发布于 2018-09-08 08:55:51
不幸的是,在Dask中,数据帧及其片段的长度通常是惰性的,并且只在显式请求时计算。这意味着数组不知道它的长度或分区,所以你不能改变它的形状。下面的笨拙的代码解决了这个问题,但我觉得应该有一种更简单的方法。
找到区块:
chunks = tuple(ddf['x'].map_partitions(len).compute())
size = sum(chunks)使用现在已知的块和大小创建一个新的数组对象:
a = ddf['x'].values
arr = da.Array(a.dask, a.name, chunks, a.dtype, (size,))https://stackoverflow.com/questions/52212827
复制相似问题