Sal*_*man 5 python datetime pandas
data['DATE'] = pd.to_datetime(data['DATE'], unit = 'ns', origin = "1899-12-30")
Run Code Online (Sandbox Code Playgroud)
给出
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-23-d33b780eb61c> in <module>
----> 1 data['DATE'] = pd.to_datetime(data['DATE'], unit = 'ns', origin = "1899-12-30")
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/tools/datetimes.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, format, exact, unit, infer_datetime_format, origin, cache)
700
701 if origin != "unix":
--> 702 arg = _adjust_to_origin(arg, origin, unit)
703
704 tz = "utc" if utc else None
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/tools/datetimes.py in _adjust_to_origin(arg, origin, unit)
499 ):
500 raise ValueError(
--> 501 f"'{arg}' is not compatible with origin='{origin}'; "
502 "it must be numeric with a unit specified"
503 )
ValueError: '0 1970-01-01 00:00:00.000043390
2 1970-01-01 00:00:00.000043605
3 1970-01-01 00:00:00.000043329
6 1970-01-01 00:00:00.000043601
8 1970-01-01 00:00:00.000043332
...
264815 1970-01-01 00:00:00.000043408
264816 1970-01-01 00:00:00.000043614
264826 1970-01-01 00:00:00.000043549
264830 1970-01-01 00:00:00.000043416
264834 1970-01-01 00:00:00.000043461
Name: DATE, Length: 74570, dtype: datetime64[ns]' is not compatible with origin='1899-12-30'; it must be numeric with a unit specified
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏。
小智 6
Name: DATE, Length: 74570, dtype: datetime64[ns]' is not compatible with origin='1899-12-30'; it must be numeric with a unit specified
解决方案是将您的 DATE 转换为数字。
Dataframe['new_column']=pd.to_datetime(pd.to_numeric(Dataframe['column'],errors='coerce'),errors='coerce',origin='1899-12-30',unit='D')
Dataframe['new_column']
如文档中所述,如果您有类似于julian 的格式(自...以来的天数),您还需要提供unit='D'(“天数”)。前任:
import pandas as pd
pd.to_datetime(1, unit='D', origin='1899-12-30')
>>> Timestamp('1899-12-31 00:00:00')
Run Code Online (Sandbox Code Playgroud)