keep-loving-pythonのブログ

Pythonを愛し続けたいです(Pythonが流行っている限りですが。。。)

解決策。numpy.core._exceptions.UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('int64'), dtype('<U1')) -> None。pandas。

エラー

numpy.core._exceptions.UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('int64'), dtype('<U1')) -> None

エラーの全体。

Traceback (most recent call last):
  File "test_test.py", line 16, in <module>
    df['datetime'] = pd.to_datetime(df['Date']+" "+df['Time'],format='%d %H:%M:%S')
  File "C:\Users\XYZZZ\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\ops\common.py", line 69, in new_method
    return method(self, other)
  File "C:\Users\XYZZZ\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\arraylike.py", line 92, in __add__
    return self._arith_method(other, operator.add)
  File "C:\Users\XYZZZ\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\series.py", line 5526, in _arith_method
    result = ops.arithmetic_op(lvalues, rvalues, op)
  File "C:\Users\XYZZZ\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\ops\array_ops.py", line 224, in arithmetic_op
    res_values = _na_arithmetic_op(left, right, op)
  File "C:\Users\XYZZZ\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\ops\array_ops.py", line 166, in _na_arithmetic_op
    result = func(left, right)
numpy.core._exceptions.UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('int64'), dtype('<U1')) -> None

環境

windows10
python3.7
pandas 1.3.5

入力データ

Lineid   Label   Month   Date    Time    Level   Component   PID Content EventId
0   -   Jan 5   12:30:40    5   httpd   2345    get_url "asahi.com"
1   ng  Jan 5   12:30:41    5   httpd   2345    get_url asahi.com
2   -   Jan 5   12:30:42    5   httpd   2345    post_data   today,it is fine.fine.fine.Very fine.I fine thank you.today,it is fine.fine.fine.Very fine.I fine thank you.The end.
3   ng  Jan 5   12:30:43    5   wtdd    23  get_url asahi.com
4   -   Jan 5   12:31:40    5   httpd   2345    get_url asahi.com
5   -   Jan 5   12:32:40    5   httpd   2345    get_url asahi.com
6   -   Jan 5   12:33:40    5   httpd   2345    post_data   today,it is fine.fine.fine.very fine.
7   ng  Jan 5   12:34:41    5   httpd   2345    get_url asahi.com

エラーが発生するコード

import pandas as pd
#import pprint

df = pd.read_csv('test00.csv',delimiter="\t")

pd.set_option('display.max_columns', 1500)
pd.set_option('display.max_rows', 150)
pd.set_option('', 150)
pd.set_option("display.width", 1000)
pd.set_option("display.max_colwidth", 1000)

##pprint.pprint(df[0:5],width=1000)

print(df.head(5))

##df['datetime'] = pd.to_datetime(df['Date'].astype(str)+" "+df['Time'],format='%d %H:%M:%S')
df['datetime'] = pd.to_datetime(df['Date']+" "+df['Time'],format='%d %H:%M:%S')

解決策

df['Date'].astype(str)です。

##df['datetime'] = pd.to_datetime(df['Date']+" "+df['Time'],format='%d %H:%M:%S')
df['datetime'] = pd.to_datetime(df['Date'].astype(str)+" "+df['Time'],format='%d %H:%M:%S')

補足。
df['Date'].dtypeは、int64になるようです!!

「<U1」 って

>>> import numpy as np
>>> a = np.array(['A','B'])
>>> a.dtype
dtype('<U1')
>>>

コメント

少しづつ慣れるしかない。。。。