gpt4 book ai didi

python - numpy.datetime64 : how to get weekday of numpy datetime64 and check if it's between time1 and time2

转载 作者:行者123 更新时间:2023-11-28 19:22:21 29 4
gpt4 key购买 nike

如何检查 numpy 日期时间是否在时间 1 和时间 2 之间(没有日期)。假设我有一系列日期时间,我想检查它的工作日,以及它是否在 13:00 和 13:30 之间。例如

2014-03-05 22:55:00 

是星期三,不是在 13:00 到 13:30 之间

最佳答案

使用 pandas,您可以使用 DatetimeIndex.indexer_between_time 方法来查找时间介于 13:00 和 13:30 之间的日期。

例如,

import pandas as pd

dates = pd.date_range('2014-3-1 00:00:00', '2014-3-8 0:00:00', freq='50T')
dates_between = dates[dates.indexer_between_time('13:00','13:30')]
wednesdays_between = dates_between[dates_between.weekday == 2]

这些是日期中的前 5 项:

In [95]: dates.tolist()[:5]
Out[95]:
[Timestamp('2014-03-01 00:00:00', tz=None),
Timestamp('2014-03-01 00:50:00', tz=None),
Timestamp('2014-03-01 01:40:00', tz=None),
Timestamp('2014-03-01 02:30:00', tz=None),
Timestamp('2014-03-01 03:20:00', tz=None)]

请注意,这些日期都在 13:00 到 13:30 之间:

In [96]: dates_between.tolist()[:5]
Out[96]:
[Timestamp('2014-03-01 13:20:00', tz=None),
Timestamp('2014-03-02 13:30:00', tz=None),
Timestamp('2014-03-04 13:00:00', tz=None),
Timestamp('2014-03-05 13:10:00', tz=None),
Timestamp('2014-03-06 13:20:00', tz=None)]

在这些日期中,这是唯一一个星期三:

In [99]: wednesdays_between.tolist()
Out[99]: [Timestamp('2014-03-05 13:10:00', tz=None)]

关于python - numpy.datetime64 : how to get weekday of numpy datetime64 and check if it's between time1 and time2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22277264/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com