【问题标题】:DataFrame performance warningDataFrame 性能警告
【发布时间】:2018-09-17 16:21:24
【问题描述】:

我收到来自 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')

这是怎么回事?我该怎么办?

【问题讨论】:

  • 您是否尝试过将数字列转换为数字?例如,df[col] = df[col].astype(int)?
  • @jpp 哇!而已!谢谢!我只是有点缺乏经验,不知道它是必需的还是一个把戏?
  • 见下面的答案。

标签: python python-3.x pandas hdf5 pytables


【解决方案1】:

您需要在适当的情况下将数据框系列转换为数字类型。

对于整数有两种主要的实现方式:

# Method 1
df['col'] = df['col'].astype(int)

# Method 2
df['col'] = pd.to_numeric(df['col'], downcast='integer')

这可确保数据类型被适当地映射到 C 类型,从而使数据能够以 HDF5 格式(PyTables 使用)存储,而无需进行酸洗。

【讨论】:

    【解决方案2】:

    在@jpp 的回答的基础上,我在自己的数据中将此问题追踪到在将大型 CSV 加载到 pandas 数据框时默认使用 int64float64 数据类型。

    解决方法:

    for c in df.columns[df.dtypes=='float64'].values:
            df[c] = df[c].astype('float')
        
        for c in df.columns[df.dtypes=='int64'].values:
            df[c] = df[c].astype('int')
    

    现在可以导出到 HDF 而不会出现警告。当然,您可以自动执行此操作,但就我而言,这已经“足够好”了。

    【讨论】:

      猜你喜欢
      • 2022-01-27
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 2012-08-16
      • 1970-01-01
      • 2014-04-25
      • 1970-01-01
      • 2012-08-13
      相关资源
      最近更新 更多