这是我从pandas.DataFrame.to_json()中获取的字符串,将其放入redis,从redis的其他位置获取,并尝试通过pandas.read_json()读取:
DFJ{“args”:{“0”:“[”,“1”:“[”,“2”:“[”,“3”:“[”,“4”:“[”,“6”:“[”,“7”:“[”,“日期”:“{“0”:138594390000000,“1”:13859434390000000,“2”:138594440000000,“3”:138594440000000000,“4”:138594440000000000,“4”:1385944000000000000,“5”:138594000000000,“6”:138594000000000,“7”:138594000000000,“Segy1.org”;“{.org:“SEGY1440000:”,“2”“2”“2”“2”“2”“3”“3”“2”“3”“3”“3”“3”“3”“3”“4”“4”“4”“4”“4”“3”“5”“5”“3”“3”“5”“3”“3”“3”“3”“5”“3”“5”“3”“3”“3”“3”“3”“3”“3”“3”“3”“3”“3”“5”“3”“3”“3”“3”“5”“3”“3”“5”“3”“3”“5”“3”“3”“3”“5”“5”“3”“5”“5”“3”“5”“5”“5”“5”“3”“5”“3”“5”“5”“5”“5”“5”“5”“5”“5”“5”“6”“6”“6”“3”“6”“6”“6”“6”“6”“6”“6”“6”“6”“3”“3”“3”“3”“6”“3”“3”“3”“3”“3”“6”“3”“3”“3”“3”“3”“3”“3”“3”“3”“3”“3”“3”“6”“3”“3”“3”“f,“6”:“deletemfg”,“7”:“gxyzinf”}3:“3”,“3”,“3”3:“10,”3:“14,”3:“14,”4:“24yyy24,”4:“4:“14yyy24”,“4:“A14yyy24”,“4:“4::::3414,”4:“14”,“4”,“4”,“4:“14”,“4:“14”,“4”,“4:::,,,,3:“4”,“4”,“14”,“4”,“4”,“14 14”,“4”,“4”,“14”,“4”,“4”,“14”,“14”,“4”,“14”,“4”,“4”,“4”,“14”,“14”,“4”,“14”,“4”,“14”,“4”,“14”,“4”,“4”,“4”,“4”,“14”,“4”,“14”,“14”,“14”,“10,”10,”10,”10,”10,”10,”10,”10,”10,”10,”10,”10,”10,”时间,”时间:::::::::,{{{00“0 0 0 0 0 0 0”时间:0“0”。0 0 0“0 0”0”0 0 0 0
它似乎没有任何unicode。但尝试.read_json()时,我得到:
回溯(最近一次呼叫最后一次):
文件“/sqlprofile.py”,第160行,在<;模块>;
可能保存数据帧(rconn、配置、结果)
文件“/sqlprofile.py”,第140行,在数据帧中
h5store.append(out_队列,df)
文件“/home/username/anaconda/lib/python2.7/site packages/pandas/io/pytables.py”,附录中第658行
self.\u写入组(键、值、表=True、追加=True、**kwargs)
文件“/home/username/anaconda/lib/python2.7/site packages/pandas/io/pytables.py”,第923行,在写入组中
s、 写入(obj=value,append=append,complib=complib,**kwargs)
写入文件“/home/username/anaconda/lib/python2.7/site packages/pandas/io/pytables.py”,第2985行
**kwargs)
文件“/home/username/anaconda/lib/python2.7/site packages/pandas/io/pytables.py”,第2717行,位于创建轴中
提高e
TypeError:[unicode]未作为表列实现
>;/home/username/anaconda/lib/python2.7/site packages/pandas/io/pytables.py(2717)create_axes()
->;提高e
(Pdb)本地人()
这就是我在locals()-似乎append\u axis(列名?)中得到的值是unicode。为什么
{'append_axis':[u'args',u'date',u'host',u'kwargs',u'operation',u'thingy',u'status',u'time'],'existing_table':无,'blocks':[FloatBlock:[time],1 x 8,dtype float64,ObjectBlock:[args,host,kwargs,operation,thingy],5 x 8,dtype对象,IntBlock:[status],1 x 8,dtype int64,DatetimeBlock:[date],1 x 8,数据类型datetime64[ns]],“axis”:1,“self”:frame_table(类型->;可追加,nrows->;None,ncols->;1,索引器->;[index]),“axes':[0],“kwargs':{},'klass':<;class'pandas.io.pytables.DataCol',“block_obj”:args日期主机kwargs操作状态时间
0[]2013-12-02 00:33:59 yy38.segm1.org{}x_gbinf a13yy38-101 0.000801
1[]2013-12-02 00:33:59 kyy1.segm1.org{}x_initobj a19kyy1 1 0.003244
2[]2013-12-02 00:34:00 yy10.segm1.org{}x_gobjParams a14yy10-101 0.002247
3[]2013-12-02 00:34:00 yy24.segm1.org{}gtfull a14yy24-101 0.002787
4[]2013-12-02 00:34:00 yy24.segm1.org{}x_gbinf a14yy24-101 0.001067
5[]2013-12-02 00:34:00 yy34.segm1.org{}gxyzinf a12yy34-101 0.002652
6[]2013-12-02 00:34:00 yy15.segm1.org{}删除制造a15yy15 1 0.004371
7[]2013-12-02 00:34:00 yy15.segm1.org{}gxyzinf a15yy15-101 0.000602,“轴标签”:[u'args',u'date',u'host',u'kwargs',u'operation',u'thingy',u'status',u'time'],'nan_rep:'nan','data_columns','obj':args date host kwargs operation thingy time
0[]2013-12-02 00:33:59 yy38.segm1.org{}x_gbinf a13yy38-101 0.000801
1[]2013-12-02 00:33:59 kyy1.segm1.org{}x_initobj a19kyy1 1 0.003244
2[]2013-12-02 00:34:00 yy10.segm1.org{}x_gobjParams a14yy10-101 0.002247
3[]2013-12-02 00:34:00 yy24.segm1.org{}gtfull a14yy24-101 0.002787
4[]2013-12-02 00:34:00 yy24.segm1.org{}x_gbinf a14yy24-101 0.001067
5[]2013-12-02 00:34:00 yy34.segm1.org{}gxyzinf a12yy34-101 0.002652
6[]2013-12-02 00:34:00 yy15.segm1.org{}删除制造a15yy15 1 0.004371
7[]2013-12-02 00:34:00 yy15.segm1.org{}gxyzinf a15yy15-101 0.000602,“验证”:True,“a”:(1,[u'args',u'date',u'host',u'kwargs',u'operation',u'thingy',u'status',u'time']),“index\u'axes\u map':{0:name->;index,cname->;index,axis->;0,pos gt;0,kind->;>;整型块:[args,host,kwargs,operation,thingy],5 x 8,数据类型对象,'e':TypeError('unicode]未实现为表列',),'name':None,'existing'col':None,'j':2,'i':1,'min'itemsize':None,'col':name->;values\u block\u\u 1,cname->;values\u block\u\u 1,dtype->;None,shape->;None}
我如何修复它?这是Pandas/pytables中的错误吗
环境:
Python 2.7
熊猫==0.12.0
表==3.0.0
似乎您的往返导致了一些unicode。不确定原因,但很容易修复。
在Python2中,您不能将unicode存储在HDFStore表中(但是在Python3中这是正确的)。如果您愿意,您可以将其作为固定格式进行存储(它将被pickle)。请参见此处
[33]on
:df=pd.read\u json
In[25]:df
Output[25]:
args日期主机kwargs操作状态thingy time
0[]2013-12-02 00:33:59 yy38.segm1.org{}x_gbinf-101 a13yy38 0.000801
1[]2013-12-02 00:33:59 kyy1.segm1.org{}x_initobj 1 a19kyy1 0.003244
2[]2013-12-02 00:34:00 yy10.segm1.org{}x_gobjParams-101 a14yy10 0.002247
3[]2013-12-02 00:34:00 yy24.segm1.org{}gtfull-101 a14yy24 0.002787
4[]2013-12-02 00:34:00 yy24.segm1.org{}x_gbinf-101 a14yy24 0.001067
5[]2013-12-02 00:34:00 yy34.segm1.org{}gxyzinf-101 a12yy34 0.002652
6[]2013-12-02 00:34:00 yy15.segm1.org{}删除制造1 a15yy15 0.004371
7[]2013-12-02 00:34:00 yy15.segm1.org{}gxyzinf-101 a15yy15 0.000602
[8行x 8列]
In[26]:df.dtypes
Output[26]:
args对象
日期时间64[ns]
宿主对象
kwargs对象
操作对象
状态int64
物体
时间浮动64
数据类型:对象
这是在推断对象的实际类型dtyped系列。只有当至少有1个字符串是unicode时,才会显示为unicode(否则将推断为字符串)
[27]on
:df.apply(lambda x:pd.lib.infere_dtype(x.values))
Output[27]:
args unicode
日期时间64
主机unicode
kwargs unicode
unicode操作
状态整数
thingy unicode
时间浮动
数据类型:对象
下面是如何“修复”它
[28]on
:types=df.apply(lambda x:pd.lib.infere\u dtype(x.values))
At[29]on:类型[types=='unicode']
Output[29]:
args unicode
主机unicode
kwargs unicode
unicode操作
thingy unicode
数据类型:对象
In[30]:对于类型[types=='unicode']on 列。索引:
..:df[col]=df[col].astype(str)
....:
看起来一样
[31]on
:df
Output[31]:
args日期主机kwargs操作状态thingy time
0[]2013-12-02 00:33:59 yy38.segm1.org{}x_gbinf-101 a13yy38 0.000801
1[]2013-12-02 00:33:59 kyy1.segm1.org{}x_initobj 1 a19kyy1 0.003244
2[]2013-12-02 00:34:00 yy10.segm1.org{}x_gobjParams-101 a14yy10 0.002247
3[]2013-12-02 00:34:00 yy24.segm1.org{}gtfull-101 a14yy24 0.002787
4[]2013-12-02 00:34:00 yy24.segm1.org{}x_gbinf-101 a14yy24 0.001067
5[]2013-12-02 00:34:00 yy34.segm1.org{}gxyzinf-101 a12yy34 0.002652
6[]2013-12-02 00:34:00 yy15.segm1.org{}删除制造1 a15yy15 0.004371
7[]2013-12-02 00:34:00 yy15.segm1.org{}gxyzinf-101 a15yy15 0.000602
[8行x 8列]
但现在的推断是正确的
[32]on
:df.apply(lambda x:pd.lib.infere_dtype(x.values))
Output[32]:
args字符串
日期时间64
主机字符串
夸尔格弦
操作字符串
状态整数
细绳
时间浮动
数据类型:对象