向HDFStore追加时失败,提示"无法匹配现有表结构
最后的解决方案是使用read_csv的“converters”参数,在把每个值添加到DataFrame之前先检查一下。最终,在超过80GB的原始数据中,只有2个值出现了问题。
这个参数看起来是这样的:
converters={'XXXXX': self.parse_xxxxx}
还有一个小的静态辅助方法是这样的:
@staticmethod
def parse_xxxxx(input):
if not isinstance(input, float):
try:
return float(input)
except ValueError:
print "Broken Value: ", input
return float(0.0)
else:
return input
在尝试把大约40GB的csv数据读入HDF文件时,我遇到了一个让人困惑的问题。在读取了大约1GB后,整个过程失败了,并出现了以下错误:
File "/usr/lib/python2.7/dist-packages/pandas/io/pytables.py", line 658, in append
self._write_to_group(key, value, table=True, append=True, **kwargs)
File "/usr/lib/python2.7/dist-packages/pandas/io/pytables.py", line 923, in write_to_group
s.write(obj = value, append=append, complib=complib, **kwargs)
File "/usr/lib/python2.7/dist-packages/pandas/io/pytables.py", line 2985, in write **kwargs)
File "/usr/lib/python2.7/dist-packages/pandas/io/pytables.py", line 2675, in create_axes
raise ValueError("cannot match existing table structure for [%s] on appending data" % items)
ValueError: cannot match existing table structure for [Date] on appending data
我使用的read_csv调用是这样的:
pd.io.parsers.read_csv(filename, sep=";|\t", compression='bz2', index_col=False, header=None, names=['XX', 'XXXX', 'Date', 'XXXXX'], parse_dates=[2], date_parser=self.parse_date, low_memory=False, iterator=True, chunksize=self.input_chunksize, dtype={'Date': np.int64})
为什么新读取的数据块中的“日期”列与现有的列不匹配,尽管我明确将数据类型设置为int64呢?
谢谢你的帮助!
这是解析日期的函数:
@staticmethod
def parse_date(input_date):
import datetime as dt
import re
if not re.match('\d{12}', input_date):
input_date = '200101010101'
timestamp = dt.datetime.strptime(input_date, '%Y%m%d%H%M')
return timestamp
在遵循了一些Jeff的建议后,我可以提供更多关于我问题的细节。这是我用来加载bz2编码文件的完整代码:
iterator_data = pd.io.parsers.read_csv(filename, sep=";|\t", compression='bz2', index_col=False, header=None,
names=['XX', 'XXXX', 'Date', 'XXXXX'], parse_dates=[2],
date_parser=self.parse_date, iterator=True,
chunksize=self.input_chunksize, dtype={'Date': np.int64})
for chunk in iterator_data:
self.data_store.append('huge', chunk, data_columns=True)
self.data_store.flush()
这个csv文件的格式是:{字符串};{字符串};{字符串}\t{整数}
对输出文件运行ptdump -av的结果如下:
ptdump -av datastore.h5
/ (RootGroup) ''
/._v_attrs (AttributeSet), 4 attributes:
[CLASS := 'GROUP',
PYTABLES_FORMAT_VERSION := '2.0',
TITLE := '',
VERSION := '1.0']
/huge (Group) ''
/huge._v_attrs (AttributeSet), 14 attributes:
[CLASS := 'GROUP',
TITLE := '',
VERSION := '1.0',
data_columns := ['XX', 'XXXX', 'Date', 'XXXXX'],
encoding := None,
index_cols := [(0, 'index')],
info := {'index': {}},
levels := 1,
nan_rep := 'nan',
non_index_axes := [(1, ['XX', 'XXXX', 'Date', 'XXXXX'])],
pandas_type := 'frame_table',
pandas_version := '0.10.1',
table_type := 'appendable_frame',
values_cols := ['XX', 'XXXX', 'Date', 'XXXXX']]
/huge/table (Table(167135401,), shuffle, blosc(9)) ''
description := {
"index": Int64Col(shape=(), dflt=0, pos=0),
"XX": StringCol(itemsize=16, shape=(), dflt='', pos=1),
"XXXX": StringCol(itemsize=16, shape=(), dflt='', pos=2),
"Date": Int64Col(shape=(), dflt=0, pos=3),
"XXXXX": Int64Col(shape=(), dflt=0, pos=4)}
byteorder := 'little'
chunkshape := (2340,)
autoIndex := True
colindexes := {
"Date": Index(6, medium, shuffle, zlib(1)).is_CSI=False,
"index": Index(6, medium, shuffle, zlib(1)).is_CSI=False,
"XXXX": Index(6, medium, shuffle, zlib(1)).is_CSI=False,
"XXXXX": Index(6, medium, shuffle, zlib(1)).is_CSI=False,
"XX": Index(6, medium, shuffle, zlib(1)).is_CSI=False}
/huge/table._v_attrs (AttributeSet), 23 attributes:
[XXXXX_dtype := 'int64',
XXXXX_kind := ['XXXXX'],
XX_dtype := 'string128',
XX_kind := ['XX'],
CLASS := 'TABLE',
Date_dtype := 'datetime64',
Date_kind := ['Date'],
FIELD_0_FILL := 0,
FIELD_0_NAME := 'index',
FIELD_1_FILL := '',
FIELD_1_NAME := 'XX',
FIELD_2_FILL := '',
FIELD_2_NAME := 'XXXX',
FIELD_3_FILL := 0,
FIELD_3_NAME := 'Date',
FIELD_4_FILL := 0,
FIELD_4_NAME := 'XXXXX',
NROWS := 167135401,
TITLE := '',
XXXX_dtype := 'string128',
XXXX_kind := ['XXXX'],
VERSION := '2.6',
index_kind := 'integer']
经过大量额外的调试,我得到了以下错误:
ValueError: invalid combinate of [values_axes] on appending data [name->XXXX,cname->XXXX,dtype->int64,shape->(1, 10)] vs current table [name->XXXX,cname->XXXX,dtype->string128,shape->None]
然后我尝试通过修改read_csv调用来强制XXXX列使用正确的数据类型,但仍然收到了相同的错误:
dtype={'XXXX': 's64', 'Date': dt.datetime})
难道read_csv忽略了数据类型设置,还是我漏掉了什么?
当以10为块大小读取数据时,最后两个chunk.info()调用的输出如下:
Int64Index: 10 entries, 0 to 9
Data columns (total 4 columns):
XX 10 non-null values
XXXX 10 non-null values
Date 10 non-null values
XXXXX 10 non-null values
dtypes: datetime64[ns](1), int64(1), object(2)<class 'pandas.core.frame.DataFrame'>
Int64Index: 10 entries, 0 to 9
Data columns (total 4 columns):
XX 10 non-null values
XXXX 10 non-null values
Date 10 non-null values
XXXXX 10 non-null values
dtypes: datetime64[ns](1), int64(2), object(1)
我使用的pandas版本是0.12.0。
1 个回答
好的,你遇到了一些问题:
在给
read_csv
指定数据类型时,必须使用 numpy 的数据类型;而字符串类型会被转换成object
类型(所以s64
其实没什么用)。datetime
也是如此,这个需要用parse_dates
来处理。你在不同的数据块中指定的数据类型是不同的。比如在第一个数据块里,你有 2 列是
int64
类型和 1 列是object
类型,而在第二个数据块里,只有 1 列是int64
,还有 2 列是object
。这就是你的问题所在。(我觉得错误信息可能有点让人困惑,不过我记得在 pandas 的后续版本中这个问题已经修复了。)
所以,你需要确保每个数据块中的数据类型都是一样的。可能是某一列的数据混合了不同类型。解决这个问题的一种方法是指定 dtype = { column_that_is_bad : 'object' }
。另一种方法是对那一列使用 convert_objects(convert_numeric=True)
,这样可以把所有非数字的值转换成 nan
(这也会把那一列的数据类型改成 float64
)。