我收到 Pandas 的性能警告
/usr/local/lib/python3.4/dist-packages/pandas/core/generic.py:1471:
PerformanceWarning:
your performance may suffer as PyTables will pickle object types that it cannot
map directly to c-types [inferred_type->mixed-integer,key->block0_values] [items->['int', 'str']]
我已经阅读了 github 上的几个问题和此处的问题,他们都说这是因为我在一栏中混合了类型,但我肯定不是。简单示例如下:
import pandas as pd
df = pd.DataFrame(columns=['int', 'str'])
df = df.append({ 'int': 0, 'str': '0'}, ignore_index=True)
df = df.append({ 'int': 1, 'str': '1'}, ignore_index=True)
for _, row in df.iterrows():
print(type(row['int']), type(row['str']))
# <class 'int'> <class 'str'>
# <class 'int'> <class 'str'>
# however
df.dtypes
# int object
# str object
# dtype: object
# the following causes the warning
df.to_hdf('table.h5', 'table')
这是关于什么的,我能做什么?
最佳答案
在适当的情况下,您需要将数据框系列转换为数字类型。
对于整数,有两种主要方法可以实现这一点:
# Method 1
df['col'] = df['col'].astype(int)
# Method 2
df['col'] = pd.to_numeric(df['col'], downcast='integer')
这确保数据类型被适本地映射到 C 类型,从而使数据能够以 HDF5 格式(PyTables 使用)存储,而无需 pickling。
关于python - DataFrame 性能警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49713113/