python - Getting UnicodeDecodeError when transposing DataFrame in iPython -
i importing excel table http://www.gapminder.org/data/ want switch columns , rows of table. , error getting: "unicodedecodeerror: 'ascii' codec can't decode byte 0xc3 in position 23389: ordinal not in range(128)"
i trying encode/decode dataframe dataframe.decode('utf-8') says dataframe not have such attribute.
the error occurs because transpose cannot convert data ascii. right? why need when table pure numbers?
thank much.
there more information on error:
--------------------------------------------------------------------------- unicodedecodeerror traceback (most recent call last) <ipython-input-190-a252f2a45657> in <module>() 1 #your code here 2 countries = countries.transpose() ----> 3 income.transpose() 4 #income = income.decode('utf-8') 5 #content = content.decode('utf-8') /users/sergey/anaconda/lib/python2.7/site-packages/ipython/core/displayhook.pyc in __call__(self, result) 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): /users/sergey/anaconda/lib/python2.7/site-packages/ipython/kernel/zmq/displayhook.pyc in finish_displayhook(self) 70 sys.stderr.flush() 71 if self.msg['content']['data']: ---> 72 self.session.send(self.pub_socket, self.msg, ident=self.topic) 73 self.msg = none 74 /users/sergey/anaconda/lib/python2.7/site-packages/ipython/kernel/zmq/session.pyc in send(self, stream, msg_or_type, content, parent, ident, buffers, track, header, metadata) 647 if self.adapt_version: 648 msg = adapt(msg, self.adapt_version) --> 649 to_send = self.serialize(msg, ident) 650 to_send.extend(buffers) 651 longest = max([ len(s) s in to_send ]) /users/sergey/anaconda/lib/python2.7/site-packages/ipython/kernel/zmq/session.pyc in serialize(self, msg, ident) 551 content = self.none 552 elif isinstance(content, dict): --> 553 content = self.pack(content) 554 elif isinstance(content, bytes): 555 # content packed, in relayed message /users/sergey/anaconda/lib/python2.7/site-packages/ipython/kernel/zmq/session.pyc in <lambda>(obj) 83 # disallow nan, because it's not valid 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) /users/sergey/anaconda/lib/python2.7/site-packages/zmq/utils/jsonapi.pyc in dumps(o, **kwargs) 38 kwargs['separators'] = (',', ':') 39 ---> 40 s = jsonmod.dumps(o, **kwargs) 41 42 if isinstance(s, unicode): /users/sergey/anaconda/lib/python2.7/json/__init__.pyc in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, 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 /users/sergey/anaconda/lib/python2.7/json/encoder.pyc in encode(self, o) 208 if not isinstance(chunks, (list, tuple)): 209 chunks = list(chunks) --> 210 return ''.join(chunks) 211 212 def iterencode(self, o, _one_shot=false): unicodedecodeerror: 'ascii' codec can't decode byte 0xc3 in position 23389: ordinal not in range(128)
after spent 40min on it, problem solved , have no idea how. thing did added code:
#encoding=utf8 import sys reload(sys) sys.setdefaultencoding('utf8')
but when delete peace, still working. know why? thank you!!!!
it's bit difficult answer authoritatively without more specific knowledge of ipython session, here educated guesses:
as why error, though data numeric, 1 of index labels (which presumably contain text).
as why still works when delete code, if working in ipython notebook, once run code setting default encoding utf-8, setting stays in effect until either
- some other code run changes setting, or
- you restart python kernel
Comments
Post a Comment