In [29]: import pandas as pd
In [30]: pd.to_datetime(pd.Series(["02-04-1995","02/04/1996", "12/31/1996"]))
Out[30]:
0 1995-02-04
1 1996-02-04
2 1996-12-31
dtype: datetime64[ns]
我给出了 2 种不同的日期时间格式。 Pandas 可以自动检测格式。在值 12/31/1996
我给出的格式是 mm/dd/yyyy
.它仍然可以检测到正确的格式而无需明确给出。那么,所有格式或值都可以 Pandas
to_datetime()
可以自动检测吗?
最佳答案
我会开始调查 dateutil
模块(例如 here ),该模块如何解析日期时间的字符串:
JUMP = [" ", ".", ",", ";", "-", "/", "'",
"at", "on", "and", "ad", "m", "t", "of",
"st", "nd", "rd", "th"]
据此,您可以执行以下操作:print( pd.to_datetime(pd.Series(["02and04and1995","02'04'1996", "12;31;1996"])) )
打印:0 1995-02-04
1 1996-02-04
2 1996-12-31
dtype: datetime64[ns]
关于python - Pandas 可以自动检测不同的日期时间格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63097846/