您可以遍历它们并应用to_datetime函数或
当您将其作为CSV读取时,请使用parse_dates参数:parse_dates : boolean, list of ints or names, list of lists, or dict
If True -> try parsing the index. If [1, 2, 3] -> try parsing columns
1, 2, 3 each as a separate date column. If [[1, 3]] -> combine columns
1 and 3 and parse as a single date column. {‘foo’ : [1, 3]} -> parse
columns 1, 3 as date and call result ‘foo’ A fast-path exists for
iso8601-formatted dates.
更新:
如果需要它的速度,最好使用下面的函数将列转换为datetime。在def lookup(s):
"""
This is an extremely fast approach to datetime parsing.
For large data, the same dates are often repeated. Rather than
re-parse these, we store all unique dates, parse them, and
use a lookup to convert all dates.
"""
dates = {date:pd.to_datetime(date) for date in s.unique()}
return s.apply(lambda v: dates[v])
这样做的好处是,重复的日期将被lookedup,而不是一次又一次地转换为datetime。似乎存在明显的速度差异:
^{pr2}$