gpt4 book ai didi

python - reshape dask 数组(从 dask 数据框列获得)

转载 作者:行者123 更新时间:2023-12-01 09:04:09 27 4
gpt4 key购买 nike

我是 dask 新手,正在尝试弄清楚如何 reshape 从 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])

最佳答案

另外:

ddf['x'].to_dask_array(lengths=True).reshape([-1,1])

关于python - reshape dask 数组(从 dask 数据框列获得),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52212827/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com