unicode argument expected, got 'str'

Question:

I’m using python 2.7.12. Running pd.__version__ returns u’0.19.0′.

I’m using a module called dalmatian. For one of the functions, once I run verbatim what is provided in the documentation, I get the error:

wm.update_participant_set('all_participants', participant_df.index)
TypeError: unicode argument expected, got 'str'

I have an inkling suspicion that this might be due to the version of pandas or python that I’m using. Running participant_df.index returns:

Index([u'100_2', u'101_3', u'102_2', u'103_3', u'104_2', u'105_2', u'106_4',
   u'107_4', u'108_4', u'109_1',
   ...
   u'91_2', u'92_3', u'93_2', u'94_4', u'95_4', u'96_3', u'97_2', u'98_3',
   u'99_1', u'9_1'],
  dtype='object', name=u'participant_id', length=523)`

I’ve also tried submitting this as .astype(unicode) and as .index.values. I suppose I don’t really know how to troubleshoot this further. Any thoughts?

EDIT TO INCLUDE TRACEBACK

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/gpfs/fs1/home/jrouhana/jrouhana/lib/python2.7/site-packages/dalmatian/wmanager.py", line 1045, in update_participant_set
    self.update_entity_set('participant', participant_set_id, participant_ids)
  File "/gpfs/fs1/home/jrouhana/jrouhana/lib/python2.7/site-packages/dalmatian/wmanager.py", line 1030, in update_entity_set
    self.upload_entities('{}_set'.format(etype), set_df, index=False)
  File "/gpfs/fs1/home/jrouhana/jrouhana/lib/python2.7/site-packages/dalmatian/wmanager.py", line 164, in upload_entities
    df.to_csv(buf, sep='t', index=index)
  File "/modules/EasyBuild/software/pandas/0.19.0-foss-2016b-Python-2.7.12/lib/python2.7/site-packages/pandas-0.19.0-py2.7-linux-x86_64.egg/pandas/core/frame.py", line 1381, in to_csv
    formatter.save()
  File "/modules/EasyBuild/software/pandas/0.19.0-foss-2016b-Python-2.7.12/lib/python2.7/site-packages/pandas-0.19.0-py2.7-linux-x86_64.egg/pandas/formats/format.py", line 1475, in save
    self._save()
  File "/modules/EasyBuild/software/pandas/0.19.0-foss-2016b-Python-2.7.12/lib/python2.7/site-packages/pandas-0.19.0-py2.7-linux-x86_64.egg/pandas/formats/format.py", line 1562, in _save
    self._save_header()
  File "/modules/EasyBuild/software/pandas/0.19.0-foss-2016b-Python-2.7.12/lib/python2.7/site-packages/pandas-0.19.0-py2.7-linux-x86_64.egg/pandas/formats/format.py", line 1530, in _save_header
    writer.writerow(encoded_labels)
TypeError: unicode argument expected, got 'str'
Asked By: John Rouhana

||

Answers:

I facced same error, the code was written in python 3, but i was running it with python 2.
so swiching to python 3 worked for me.

Answered By: yaya

Add the following at the beginning of the file:

from __future__ import unicode_literals
Answered By: Eftal Gezer
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.