gpt4 book ai didi

python - 带有 HDFStore 的 pandas.DataFrame.convert_dtypes 会导致属性错误?

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

我想通过 convert_dtypes 转换我的 df 的 dtypes 但如果我想通过 HDFStore 存储它,我会得到这个: AttributeError: 'IntegerArray' object has no attribute 'size'

df = pd.DataFrame()
df["test"] = [0,1,2,3]
df["test1"] = [0,1,2,3.5]
df = dfdf.convert_dtypes()
store=pd.HDFStore(r"C:\Users\User\Desktop\test.h5")
store["test"] = df
store.close()

最佳答案

我遇到了同样的问题。 IntegerArrays 具有可以表示 NaN 的属性(类似于例如 float64),这对于 Pandas 中的普通 numpy int 数据类型是不可能的。但是,这会导致此 dtype 在写入 HDF 时失败。请参见此处 ( https://github.com/pandas-dev/pandas/issues/26144 )。如果您的列中没有任何 NaN,以下是一个简单快捷的解决方案:

cols = df.columns
for col in cols:
col_dtype = df[col].dtype
try:
if col_dtype == pd.Int8Dtype():
df[col] = df[col].astype('int8')
elif col_dtype == pd.Int16Dtype():
df[col] = df[col].astype('int16')
elif col_dtype == pd.Int32Dtype():
df[col] = df[col].astype('int32')
elif col_dtype == pd.Int64Dtype():
df[col] = df[col].astype('int64')
except:
pass


关于python - 带有 HDFStore 的 pandas.DataFrame.convert_dtypes 会导致属性错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60559306/

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