在ipython notebook

时间:2016-07-28 15:32:03

标签: python pandas unicode

我跑了 Windows 7 64位

Python 2.7.9 | Anaconda 2.2.0(64位)| [MSC v.1500 64 bit(AMD64)]

熊猫版本0.15.2

Jypiter笔记本 IPython 3.0.0版。

这是我面临的问题:

当我跑步时

import pandas as pd
df = pd.DataFrame({'col': [u'α']})
df

其中α是希腊语'a'字符,我收到错误

        

UnicodeDecodeError Traceback(最近一次调用   最后)in()         2 test_df = pd.DataFrame({'col':[u'α']})         3#test_df = pd.DataFrame([u'Hello \ u2013 World'])   ----> 4 test_df

     

C:\ Anaconda \ lib \ site-packages \ IPython \ core \ displayhook.pyc in   致电(自我,结果)       236 self.write_format_data(format_dict,md_dict)       237 self.log_output(format_dict)    - > 238 self.finish_displayhook()       239       240 def cull_cache(self):

     

C:\ Anaconda \ lib \ site-packages \ IPython \ kernel \ zmq \ displayhook.pyc in   finish_displayhook(个体经营)        70 sys.stderr.flush()        71如果self.msg ['content'] ['data']:   ---> 72 self.session.send(self.pub_socket,self.msg,ident = self.topic)        73 self.msg =无        74

     

C:\ Anaconda \ lib \ site-packages \ IPython \ kernel \ zmq \ session.pyc in   发送(self,stream,msg_or_type,content,parent,ident,buffers,   轨道,标题,元数据)       647如果self.adapt_version:       648 msg = adapt(msg,self.adapt_version)    - > 649 to_send = self.serialize(msg,ident)       650 to_send.extend(缓冲区)       651 longest = max([to(s)for s in to_send))

     

C:\ Anaconda \ lib \ site-packages \ IPython \ kernel \ zmq \ session.pyc in   序列化(self,msg,ident)       551 content = self.none       552 elif isinstance(content,dict):    - > 553 content = self.pack(content)       554 elif isinstance(内容,字节):       已经打包了555#内容,如在中继消息中

     

C:\ Anaconda \ lib \ site-packages \ IPython \ kernel \ zmq \ session.pyc in   (OBJ)        83#disallow nan,因为它实际上并不是有效的JSON        84 json_packer = lambda obj:jsonapi.dumps(obj,default = date_default,   ---> 85 ensure_ascii = False,allow_nan = False,        86)        87 json_unpacker = lambda s:jsonapi.loads(s)

     转储中的C:\ Anaconda \ lib \ site-packages \ zmq \ utils \ jsonapi.pyc(o,   ** kwargs)        38 kwargs ['separators'] =(',',':')        39   ---> 40 s = jsonmod.dumps(o,** kwargs)        41        42如果是实例(s,unicode):

     

C:\ Anaconda \ lib \ json__init __。pyc in dumps(obj,skipkeys,   ensure_ascii,check_circular,allow_nan,cls,indent,separator,   encoding,default,sort_keys,** kw)       248 check_circular = check_circular,allow_nan = allow_nan,indent = indent,       249 separators = separators,encoding = encoding,default = default,    - > 250 sort_keys = sort_keys,** kw).encode(obj)       251       252

     

C:\ Anaconda \ lib \ json \ encoder.pyc编码(self,o)       208如果不是isinstance(块,(列表,元组)):       209 chunks = list(chunk)    - > 210 return''。join(chunk)       211       212 def iterencode(self,o,_one_shot = False):

     

UnicodeDecodeError:'ascii'编解码器无法解码位置的字节0xce   12:序数不在范围内(128)

但是当我只运行df = pd.DataFrame({'col': [u'α']})

时,我不会收到错误

在网上搜索了几个小时后,我发现有些建议

reload(sys)
sys.setdefaultencoding("utf-8")

作为一种解决方案,而其他人建议永远不要使用它。这段代码确实显示了数据框,但它打破了print 其他建议是升级到Python3。

我想问的是,除了升级到Python 3以及打破print的代码之外,是否有显示数据帧的解决方案。

0 个答案:

没有答案