【问题标题】:unicode argument expected, got 'str'需要 unicode 参数,得到 'str'
【发布时间】:2020-01-06 18:24:41
【问题描述】:

我正在使用 python 2.7.12。运行 pd.__version__ 返回 u'0.19.0'。

我正在使用一个名为 dalmatian 的模块。对于其中一个功能,一旦我逐字运行文档中提供的内容,就会收到错误消息:

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

我隐约怀疑这可能是由于我使用的 pandas 或 python 版本所致。运行participant_df.index 返回:

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)`

我也尝试将其提交为.astype(unicode).index.values。我想我真的不知道如何进一步解决这个问题。有什么想法吗?

编辑以包含追溯

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'

【问题讨论】:

  • 你有错误的完整堆栈跟踪吗?
  • 我进行了编辑以添加回溯。
  • 不确定是什么问题,但请注意,'all_participants' 是字符串而不是 unicode。真的,你应该只使用 Python 3
  • @juanpa.arrivillaga 我知道。在运行之前,我尝试将 all_participants 转换为 unicode。这也不是问题。我会看看我需要的所有包是否都在 Python 3 中可用。
  • 更新有点晚,但改用 Python3 解决了这个问题。

标签: python pandas python-2.7


【解决方案1】:

我遇到了同样的错误,代码是用python 3 编写的,但我用python 2 运行它。 所以切换到python 3 对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-14
    • 2012-11-08
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    相关资源
    最近更新 更多