Я удивлен, что ваш первый случай работает. Но это может помочь, если вы дадите простую версию своего фрейма данных.
Поскольку вы этого не сделали, мне придётся составить один :(
In [321]: df = pd.DataFrame([[1,'foo'],[2,'bar']])
In [322]: df
Out[322]:
0 1
0 1 foo
1 2 bar
Сначала посмотрите на index
:
In [323]: df.index.values
Out[323]: array([0, 1]) # numeric in my case
In [324]: df.index.values.astype(str)
Out[324]: array(['0', '1'], dtype='<U21') # numpy dtype
In [325]: df.index.values.astype(str)+'_'
---------------------------------------------------------------------------
UFuncTypeError Traceback (most recent call last)
<ipython-input-325-230387b2895a> in <module>
----> 1 df.index.values.astype(str)+'_'
UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U21'), dtype('<U21')) -> dtype('<U21')
+/add
не определено для numpy строк
Теперь посмотрите на строковый столбец:
In [330]: df[1].values
Out[330]: array(['foo', 'bar'], dtype=object) # pandas uses python strings
, преобразующий этот массив to numpy str выдает ту же ошибку:
In [331]: df[1].values.astype(str)
Out[331]: array(['foo', 'bar'], dtype='<U3')
In [332]: df.index.values.astype(str)+df[1].values.astype(str)
---------------------------------------------------------------------------
UFuncTypeError Traceback (most recent call last)
<ipython-input-332-7bc2436a1bf8> in <module>
----> 1 df.index.values.astype(str)+df[1].values.astype(str)
UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U21'), dtype('<U21')) -> dtype('<U21')
Вот почему я задаюсь вопросом, почему запускается ваш первый случай.
Если я оставлю строки dtype объекта как:
In [333]: df.index.values.astype(str)+df[1].values
Out[333]: array(['0foo', '1bar'], dtype=object)
numpy
преобразует массив index
в объект dtype (общий тип d) и выполняет элемент за элементом +
, что для строк python является конкатенацией.
Применение этой идеи к случай с '_':
In [334]: df.index.values.astype(str).astype(object)+'_'+df[1].values
Out[334]: array(['0_foo', '1_bar'], dtype=object)