不同的日期时间格式 pandas 可以自动检测

Different datetime formats pandas can auto detect

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]