pandas.to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False, utc=None, box=True, format=None, exact=True, unit=None, infer_datetime_format=False, origin='unix', cache=True)
参数 | 描述 |
---|---|
arg | integer, float, string, datetime, list, tuple, 1-d array, Series |
errors | {‘ignore’, ‘raise’, ‘coerce’}, default ‘raise’ |
dayfirst | boolean, default False |
yearfirst | boolean, default False |
utc | boolean, default None |
box | boolean, default True |
format | string, default None |
exact | boolean, True by default |
unit | string, default ‘ns’ |
infer_datetime_format | boolean, default False |
origin | scalar, default is ‘unix’ |
cache | boolean, default True |
import pandas as pd
import numpy as np
s = pd.Series(['apple', '1.0', '2','2019-01-02',1, False,None,pd.Timestamp('2018-01-05')])
pd.to_datetime(s, errors='raise') # 只转换时间字符串
pd.to_datetime(s, errors='ignore') # 只转换时间字符串
pd.to_datetime(s, errors='coerce') # 只转换时间字符串
a = pd.to_datetime(s, errors='coerce') # 只转换时间字符串
b = pd.to_datetime(s, errors='coerce') # 只转换时间字符串
a == b
a[a==pd.NaT] # 索引不到pd.NaT
a[a.isnull()] = None # 能索引到,但是不能赋值为None
a[a.isna()] =None # 能索引到,但是不能赋值为None
a = a.where(a.notnull(), None) # 不能转换为None
b = a.where(a.notnull(), None) # 不能转换为None
a == b
a = a.where(a.notnull(), np.datetime64("1949-10-1")) # 转换为默认值
b = a.where(a.notnull(), np.datetime64("1949-10-1")) # 转换为默认值进行比较
a == b
参考:
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html#pandas.to_datetime