我有下面的代码
代码语言:javascript复制 import pandas as pd
pd.to_datetime(pd.DataFrame(['12/4/1982']))
但是这样,我遇到了以下错误
代码语言:javascript复制 Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.11/site-packages/pandas/core/tools/datetimes.py", line 1053, in to_datetime
result = _assemble_from_unit_mappings(arg, errors, utc)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pandas/core/tools/datetimes.py", line 1161, in _assemble_from_unit_mappings
unit = {k: f(k) for k in arg.keys()}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pandas/core/tools/datetimes.py", line 1161, in <dictcomp>
unit = {k: f(k) for k in arg.keys()}
^^^^
File "/usr/local/lib/python3.11/site-packages/pandas/core/tools/datetimes.py", line 1156, in f if value.lower() in _unit_map:
^^^^^^^^^^^
AttributeError: 'int' object has no attribute 'lower'
可以试试下面的代码:
代码语言:javascript复制 import pandas as pd
df = pd.DataFrame(["12/4/1982"])
df[0] = pd.to_datetime(df[0])print(df)
打印结果
代码语言:javascript复制 0
0 1982-12-04