python - Pandas 可以自动检测不同的日期时间格式

标签 python python-3.x pandas python-datetime

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/

相关文章:

python - 如何找到最接近给定值的值并返回相应的键

python - 根据某些行/条件将表格格式化为多列

django - 如何修复/admin/login/处的编程错误(1146, "Table 'torquedb.showroom_customuser'不存在”)

python - 嵌套的 for-while 循环在第一次运行后停止迭代

python - 如何将列内容解压到由单元格值确定的新列

python - 转换oracle日期格式

python - Numpy:为什么 'a += a.T' 不起作用?

python - 解析 svg :path d attribute

python - 如何使用 python pandas 的 read_html 读取具有多个 tbodies 的 html 表?

python - sympy 不够简化