您可以使用
to_datetime:
第一种格式(
YYYY-MM-DD):
print (df) dates0 13/11/20161 21/01/20172 22/01/20173 2017-02-024 2016-12-115 13/11/20166 2016-12-127 21/01/20178 22/01/20179 2017-02-029 2017-02-25 <- YYYY-MM-DDdates = pd.to_datetime(df.dates)print (dates)0 2016-11-131 2017-01-212 2017-01-223 2017-02-024 2016-12-115 2016-11-136 2016-12-127 2017-01-218 2017-01-229 2017-02-029 2017-02-25Name: dates, dtype: datetime64[ns]
第二种格式(
YYYY-DD-MM)
这有点问题-需要参数
format以及
errors='coerce'in
to_datetime,last
combine_first或
fillna:
print (df) dates0 13/11/20161 21/01/20172 22/01/20173 2017-02-024 2016-12-115 13/11/20166 2016-12-127 21/01/20178 22/01/20179 2017-02-029 2017-25-02 <- YYYY-DD-MMdates1 = pd.to_datetime(df.dates, format='%d/%m/%Y', errors='coerce')dates2 = pd.to_datetime(df.dates, format='%Y-%d-%m', errors='coerce')dates = dates1.combine_first(dates2)#dates = dates1.fillna(dates2)print (dates)0 2016-11-131 2017-01-212 2017-01-223 2017-02-024 2016-11-125 2016-11-136 2016-12-127 2017-01-218 2017-01-229 2017-02-029 2017-02-25Name: dates, dtype: datetime64[ns]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)