【发布时间】:2021-12-08 00:35:52
【问题描述】:
我有一个 Pandas 数据框,其中包含一个“日期时间”列,其中包含一个日期时间:)
ColA Datetime ColB
---- ------------------- ----
1 2021-01-01 05:02:22 SomeVal
2 2021-01-01 01:01:22 SomeOtherVal
我想创建一个只有两条规则的新日期列:
1. If the "time" element of datetime is between 00:00:00 and 02:00:00 then make Date the "date" element of Datetime - 1 (the previous day)
2. Otherwise make Date the "date" element of Datetime as is
为了实现这一点,我将不得不对 Datetime 列进行检查。那看起来怎么样?另外,如果我不需要迭代数据框来实现这一点,则可以加分。
【问题讨论】:
-
您可以使用
apply,并定义一个自定义函数,接收datetime,并返回您想要返回的任何内容。
标签: python pandas datetime timedelta