【发布时间】:2018-07-31 12:28:48
【问题描述】:
在进行某些查询时,Pandas 中的 HDFStore 模块中似乎存在错误,但我找不到解决方法。也就是说,pd.HDFStore.select 似乎在内部将原本有效的字符串重新格式化为错误的语法。例如,字符串"03" 将出于某种原因将where 语句输入为"0 3",这将返回syntax 错误。
样本数据:
p1 = np.random.randint(0,100,(100,2))
p2 = np.random.choice(np.array(range(1990,2010)),100).reshape((100,1))
df = pd.DataFrame(np.concatenate([p2,p1],axis=1), columns = ['year', 'value1', 'value2'])
df.year2 = df.year.astype(str).str[2:]
data_path = "C:/Users/.../some path"
with pd.HDFStore("/".join([data_path, 'sampleData.h5']), 'w') as store:
store.put('example', df, format='table')
查询工作
query1 = 'year2<={0}'.format('99')
query2 = "year2=={0} | year2=={1}".format('01', '02')
with pd.HDFStore("/".join([data_path, 'sampleData.h5']), 'r') as store:
df_load = store.select('example', where=[query1],columns = ['year', 'year2', 'value1'])
查询 - 不工作
In [111]: %paste
with pd.HDFStore("/".join([data_path, 'sampleData.h5']), 'r') as store:
df_load = store.select('example', where=[query2],columns = ['year', 'year2', 'value1'])
## -- End pasted text --
File "<unknown>", line 1
(year2 ==0 1 or year2 ==0 2 )
^
SyntaxError: invalid syntax
有谁知道不会将"01" 分成"0 1" 的锻炼方法?
【问题讨论】: