【问题标题】:Error message "Exception: cannot find the correct atom type" when executing pandas to_hdf执行 pandas to_hdf 时出现错误消息“异常:找不到正确的原子类型”
【发布时间】:2015-07-18 00:39:17
【问题描述】:

我想将数据框 df 保存到 .h5 文件 MainDataFile.h5 中:

df.to_hdf ("c:/Temp/MainDataFile.h5", "MainData", mode = "w", format = "table", data_columns=['_FirstDayOfPeriod','Category','ChannelId'])

并得到以下错误:

*** 异常:找不到正确的原子类型 -> > [dtype->object,items->Index(['Libellé_Article', 'Libellé_segment'], dtype='object')]

现在,如果我从 df(它是一个字符串列)中删除列“Libellé_Article”,我将不再收到错误消息。

这个专栏有什么问题?我怀疑其中有一个特殊的、被禁止的角色,但到目前为止找不到。

更新 1

根据 Jeff 的评论,我尝试对“Libellé_Article”列进行编码:

df['Libellé_Article'] = df['Libellé_Article'].str.encode('utf-8')

该列现在如下所示:

df['Libellé_Article']
0                                               b'PAPETERIE'
2                                    b'NR CONTRIBUTION DEEE'
4                                         b'NON UTILISE 103'
7                         b"L'ENFANT SOUS TERREUR/MILLER A."
10                 b'ENERGIE VITALE ET AUTOGUERISON/CHIA M.'
12         b'ENERGIE COSMIQUE CETTE PUISSANCE QUI EST EN ...
13         b'ENERGIE COSMIQUE CETTE PUISSANCE QUI EST EN ...
18                     b"COMMENT ATTIRER L'ARGENT/MURPHY J."
19                     b"COMMENT ATTIRER L'ARGENT/MURPHY J."

当我执行命令 to_hdf 时,我得到:

*** TypeError:无法序列化列 [Libellé_Article] 因为 它的数据内容是 [mixed] object dtype

【问题讨论】:

  • 转换是否有可能抱怨 unicode?您是否尝试使用 [u'Libellé_Article', u'Libellé_segment'] ?我知道这是 python 3,但尽管如此......
  • 我刚试了下,可惜结果一样

标签: string python-3.x pandas hdf5


【解决方案1】:

这将在 py2 中工作。对于 py3,这应该在没有编码步骤的情况下工作。 这实际上是一个“混合”列,因为它包含字符串和 unicode。

In [24]: from pandas.compat import u

In [25]: df = DataFrame({'unicode':[u('\u03c3')] * 5 + list('abc') })

In [26]: df
Out[26]: 
  unicode
0       ?
1       ?
2       ?
3       ?
4       ?
5       a
6       b
7       c

In [27]: df['unicode'] = df.unicode.str.encode('utf-8')

In [28]: df.to_hdf('test.h5','df',mode='w',data_columns=['unicode'],format='table')

In [29]: pd.read_hdf('test.h5','df')
Out[29]: 
  unicode
0       ?
1       ?
2       ?
3       ?
4       ?
5       a
6       b
7       c

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-18
    • 2013-03-07
    • 1970-01-01
    • 2017-12-22
    • 2021-03-28
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多