Pandas HDFStore 警告

4 投票
2 回答
4518 浏览
提问于 2025-04-18 08:47

我在想,为什么在使用pandas的HDFStore时会对字符串列发出警告。我原以为是因为我的数据库里有NaN(缺失值),但在这里测试时,即使其中一列没有混合类型,只有字符串,仍然会出现警告。

我使用的是.pandas 0.13.1和.tables 3.1.1

In [75]: d1 = {1:{'Mix': 'Hello', 'Good': 'Hello'}}

In [76]: d2 = {2:{'Good':'Goodbye'}}

In [77]: d2_df = pd.DataFrame.from_dict(d2,orient='index')

In [78]: d_df = pd.DataFrame.from_dict(d1,orient='index')

In [80]: d = pd.concat([d_df,d2_df])

In [81]: d
Out[81]:
      Good    Mix
1    Hello  Hello
2  Goodbye    NaN

[2 rows x 2 columns]

In [84]: d.to_hdf('test_.h5','d')
/home/cschwalbach/venv/lib/python2.7/site-packages/pandas-0.13.1-py2.7-linux-x86_64.egg/pandas/io/pytables.py:2446: PerformanceWarning:
your performance may suffer as PyTables will pickle object types that it cannot
map directly to c-types [inferred_type->mixed,key->block0_values] [items->['Good', 'Mix']]

  warnings.warn(ws, PerformanceWarning)

2 个回答

0

这里的问题是NaN值。如果你能把它替换成一个空字符串,警告就会消失。

3

当你使用 fixed 格式存储数据时(如果你没有特别指定 format,它默认就是 fixed),你实际上是在存储 object 类型的数据(在 pandas 中,字符串会被存储为 object 类型)。这种格式的长度是可变的,但 PyTables 在数组类型(比如 CArray 和 EArray)中不支持这种格式,具体的警告信息可以在 这里 查看。

不过,你可以选择使用 format='table' 来存储数据;关于如何存储固定长度字符串的详细信息,可以参考 这份文档

撰写回答