gpt4 book ai didi

python - 如何根据已经制作的 pandas 日期范围系列对特定日期进行分类(排序)?

转载 作者:太空宇宙 更新时间:2023-11-03 16:40:06 25 4
gpt4 key购买 nike

我制作了一个日期系列列表:

import pandas as pd

daterange2016 = pd.date_range("2016-04-25 00:00:00", "2016-04-29 21:00:00", freq="30min")

但是,我想要一个函数来获取日期时间并知道该时间属于哪个间隔。我不想改变间隔。我怎样才能做到这一点?

类似于(这是伪代码):

def datetimesorter(givendate):
if givendate:
df['pd_time_interval_that_givendate_belongs_to'] = givendate

提前感谢您的帮助

更新:我想出了这个,它有效,但我不会删除这个问题,以防它对某人有帮助

def daterangefinder(datetime):
for intervals in daterange2016:
if intervals <= datetime < intervals+1:
return intervals
else:
pass

最佳答案

IIUC你可以尝试:

import pandas as pd

daterange2016 = pd.date_range("2016-04-25 00:00:00", "2016-04-25 05:00:00", freq="30min")
print daterange2016
DatetimeIndex(['2016-04-25 00:00:00', '2016-04-25 00:30:00',
'2016-04-25 01:00:00', '2016-04-25 01:30:00',
'2016-04-25 02:00:00', '2016-04-25 02:30:00',
'2016-04-25 03:00:00', '2016-04-25 03:30:00',
'2016-04-25 04:00:00', '2016-04-25 04:30:00',
'2016-04-25 05:00:00'],
dtype='datetime64[ns]', freq='30T')

df = pd.DataFrame(daterange2016, columns=['d1'])

df['d2']= df.d1.shift(-1)
print df
d1 d2
0 2016-04-25 00:00:00 2016-04-25 00:30:00
1 2016-04-25 00:30:00 2016-04-25 01:00:00
2 2016-04-25 01:00:00 2016-04-25 01:30:00
3 2016-04-25 01:30:00 2016-04-25 02:00:00
4 2016-04-25 02:00:00 2016-04-25 02:30:00
5 2016-04-25 02:30:00 2016-04-25 03:00:00
6 2016-04-25 03:00:00 2016-04-25 03:30:00
7 2016-04-25 03:30:00 2016-04-25 04:00:00
8 2016-04-25 04:00:00 2016-04-25 04:30:00
9 2016-04-25 04:30:00 2016-04-25 05:00:00
10 2016-04-25 05:00:00 NaT
date = pd.to_datetime('2016-04-25 02:15:08')
print date
2016-04-25 02:15:08

print (df.d1 < date) & (df.d2 > date)
0 False
1 False
2 False
3 False
4 True
5 False
6 False
7 False
8 False
9 False
10 False
dtype: bool

print df[(df.d1 < date) & (df.d2 > date)].d1
4 2016-04-25 02:00:00
Name: d1, dtype: datetime64[ns]

如果您需要将日期添加到新列d3,请使用loc :

df.loc[(df.d1 < date) & (df.d2 > date), 'd3'] =  date
print df
d1 d2 d3
0 2016-04-25 00:00:00 2016-04-25 00:30:00 NaT
1 2016-04-25 00:30:00 2016-04-25 01:00:00 NaT
2 2016-04-25 01:00:00 2016-04-25 01:30:00 NaT
3 2016-04-25 01:30:00 2016-04-25 02:00:00 NaT
4 2016-04-25 02:00:00 2016-04-25 02:30:00 2016-04-25 02:15:08
5 2016-04-25 02:30:00 2016-04-25 03:00:00 NaT
6 2016-04-25 03:00:00 2016-04-25 03:30:00 NaT
7 2016-04-25 03:30:00 2016-04-25 04:00:00 NaT
8 2016-04-25 04:00:00 2016-04-25 04:30:00 NaT
9 2016-04-25 04:30:00 2016-04-25 05:00:00 NaT
10 2016-04-25 05:00:00 NaT NaT

关于python - 如何根据已经制作的 pandas 日期范围系列对特定日期进行分类(排序)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36882895/

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