python - DataFrame 性能警告

标签 python python-3.x pandas hdf5 pytables

我收到 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/

相关文章:

python - sklearn Predict_proba 不匹配类标签

python - ImageMagick 魔杖无法识别 pdf 图像?

python - 计算 unique( ) 的返回值

python - numpy数组的可变大小

python - Keras 中的 RMSE/RMSLE 损失函数

python - 不使用点符号间接访问 Python 实例属性

python - 从 sys.stdin 读取管道输入时如何防止 "UnicodeDecodeError"?

python - Pandas :将特定功能应用于列并创建其他列

python - 评估 SMOTE 和 RandomUnderSampling 不同策略

python - 返回字典覆盖所有字典的函数