pandas将字符串转换成时间_Python pandas,将多列字符串转换为时间?

您可以遍历它们并应用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}$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值