按一天中特定的 EST 时间过滤 Pandas 时间序列
我试图通过执行以下操作来匹配 pandas 数据框中的行,其中 DatetimeIndex 位于美国/东部时区的美国/东部时间每天 15:30:00:
check_time = pd.to_datetime("15:30:00").time()
last_30m_mask = df.index.time == check_time
up_df = df[last_30m_mask]
但是我得到的行如下:
w1 w2
timestamp
2021-08-04 15:30:00-04:00 382.37 388.27
2021-08-05 15:30:00-04:00 395.65 400.78
2021-08-09 15:30:00-04:00 434.79 437.04
...
我是否正确认为这反而给了我 15:30 UTC,即 11:30 EST(或一年中大部分时间 10:30 EST)?
如果是这样,我将如何重写 check_time
变量以始终为我提供 15:30 EST(美国/东部)?
I am trying to match rows in a pandas dataframe where the DatetimeIndex is in US/Eastern timezone at exactly 15:30:00 each day US/Eastern time by doing the following:
check_time = pd.to_datetime("15:30:00").time()
last_30m_mask = df.index.time == check_time
up_df = df[last_30m_mask]
However the rows I get back are as follows:
w1 w2
timestamp
2021-08-04 15:30:00-04:00 382.37 388.27
2021-08-05 15:30:00-04:00 395.65 400.78
2021-08-09 15:30:00-04:00 434.79 437.04
...
Am I correct in thinking that this is instead giving me 15:30 UTC which is 11:30 EST (or 10:30 EST for most of the year)?
If so, how would I re-write the check_time
variable to give me 15:30 EST (US/Eastern) at all times?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 FObersteiner 正确指出的那样,时间戳确实是本地的,并且偏移量为您提供了 UTC 的增量。
我犯的错误是在另一端。当从源转换我的数据时,我没有为其提供正确的上下文。
我有:
time_col = pd.to_datetime(source_data["time"]).tz_localize("US/Eastern")
而我需要:
time_col = pd.to_datetime(source_data["time"]).tz_localize("UTC").tz_convert("US/Eastern")
这样我现在就可以正确地将本地时间与
进行比较pd.to_datetime("XX:XX:XX").time()
根据需要。As FObersteiner correctly pointed out the timestamp is indeed local and the offset gives you the delta to UTC.
The error I was committing was on the other end. When converting my data from the source I wasn't giving it the proper context.
I had:
time_col = pd.to_datetime(source_data["time"]).tz_localize("US/Eastern")
Whereas I needed to have:
time_col = pd.to_datetime(source_data["time"]).tz_localize("UTC").tz_convert("US/Eastern")
This way I can now correctly compare my local times with
pd.to_datetime("XX:XX:XX").time()
as desired.