Skip to main content
 首页 » 编程设计

python之我们可以从 csv 文件中自动检测给定列的日期时间格式吗

2025年02月15日9hnrainll

假设我有一个带有 timestamp 的 csv 但格式未定义。它可以是带有任何分隔符的任何格式,例如 -

mm/dd/yyyy hh:mm 或 dd/mm/yyyy hh:mm:ss 或 mm-dd-yyyy hh:mm 或 dd-mm-yyyy hh:mm:ss 或类似这样。

我正在尝试解析任何格式的日期。

这里:

dateparse = lambda dates: datetime.strptime(dates, '%m/%d/%Y %H:%M') 

我们已定义以这种格式解析日期:%m/%d/%Y %H:%M

如果有人能提出任何有值(value)的建议,那将会很有帮助。

请您参考如下方法:

pandas.read_csv有一个 infer_datetime_format 参数:

infer_datetime_format : boolean, default False

If True and parse_dates is enabled, pandas will attempt to infer the format of the datetime strings in the columns, and if it can be inferred, switch to a faster method of parsing them. In some cases this can increase the parsing speed by ~5-10x.