2015-10-22 49 views
0

问题写入大熊猫数据帧(时间序列),以使用pytables/tstables HDF5:PyTables ValueError异常上字符串列与较新的熊猫

import pandas 
import tables 
import tstables 

# example dataframe 
valfloat = [512.3, 918.8] 
valstr = ['abc','cba'] 
tstamp = [1445464064, 1445464013] 
df = pandas.DataFrame(data = zip(valfloat, valstr, tstamp), columns = ['colfloat', 'colstr', 'timestamp']) 

df.set_index(pandas.to_datetime(df['timestamp'].astype(int), unit='s'), inplace=True) 
df.index = df.index.tz_localize('UTC') 

colsel = ['colfloat', 'colstr'] 
dftoadd = df[colsel].sort_index() 

# try string conversion from object-type (no type mixing here ?) 
##dftoadd.loc[:,'colstr'] = dftoadd['colstr'].map(str) 

h5fname = 'df.h5' 
# class to use as tstable description 
class TsExample(tables.IsDescription): 
    timestamp = tables.Int64Col(pos=0) 
    colfloat = tables.Float64Col(pos=1) 
    colstr = tables.StringCol(itemsize=8, pos=2) 
# create new time series 
h5f = tables.open_file(h5fname, 'a') 
ts = h5f.create_ts('/','example',TsExample) 

# append to HDF5 
ts.append(dftoadd, convert_strings=True) 

# save data and close file 
h5f.flush() 
h5f.close() 

例外:

ValueError: rows parameter cannot be converted into a recarray object compliant with table tstables.tstable.TsTable instance at ... The error was: cannot view Object as non-Object type

虽然此特定错误与TsTables时,会发生代码块负责它是相同的PyTables try-section here

发生错误后,我升级熊猫0.17.0;相同的代码与0.16.2一起运行时没有错误。

注意:如果一个字符串列被排除然后一切工作正常,所以这个问题必须与数据框中的字符串列类型表示相关。

该问题可能与this question有关。我缺少数据帧的'colstr'列是否需要转换?

回答

0

这不会是一个较新的大熊猫工作,因为该指数是时区知道,看here

您可以:

  • 转换为类型PyTables理解,这需要本地化
  • 使用HDFStore编写帧

请注意,您所做的是HDFStore首先存在的原因,以使读/ w引导pyTables对熊猫物体友好。做这个'手动'是充满陷阱。

+0

我首先想到问题出在tz增强索引中,但为什么一切工作都没有字符串列(即具有相同的tz-aware索引,但只有数据框中的数字列)? – Pavel