计算日期值是否出现在两个不同的时间之间python pandas [英] calculate if date value occurs between two different times python pandas
问题描述
我正在尝试创建一个新列,以确定行值是否在营业时间之间".为此,我尝试使用时间间隔功能. 如果有更简单的方法,我不需要使用它.
I am trying to create a new column to determine if a row value is "between business hours". To do this I am trying to use the between time function. I dont need to use it if there is an easier way.
我有一个数据框,其中包含日期" ,开始时间" ,结束时间" 的列.
I have a dataframe with columns for 'Date', 'StartHour', 'End Hour'.
问题:
如果"日期"列中的时间在"开始时间"和""之间,我想给出"True"或"False"结束时间的时间.
I would like to give a 'True' or 'False' if the time in the 'Date' column is between the 'StartHour' and 'EndHour' time.
import pandas as pd
import numpy as np
#create dataframe with dates
d = {'Date': ['2016-11-17 05:01:45','2011-01-04 16:34:00','2011-01-05 09:25:45',
'2011-01-10 12:00:45','2011-01-14 07:05:45','2011-01-15 10:19:00',
'2011-01-17 13:59:45','2011-01-19 18:39:45','2011-01-22 06:19:45'],
'StartHour': ['16:00','16:00','16:00','16:00','16:00','16:00','16:00','16:00','16:00'],
'EndHour': ['10:00','10:00','10:00','10:00','10:00','10:00','10:00','10:00','10:00'],
'Station_ID': ['A','A','A','A','B','B','B','B','B']}
df = pd.DataFrame(data=d)
#convert date column to datetime
df['Date'] = df['Date'].values.astype('datetime64[ns]')
#************************
# set index to Date (need for 'between_time')
df = df.set_index('Date')
# run calculation for between time
df['between_business_hours'] = df.index.isin(df.between_time('16:00', '10:00', include_start=True, include_end=True).index)
df
我已经使用between_time函数计算了一个列,但这仅允许我将硬编码值用于开始时间和结束时间.我想使用"StartTime"和"EndTime"列中的值.通过使用ween_time函数,我可能会比需要做的更加困难.
I have calculated a column using the between_time function but this only lets me use hard coded values for the start and end time. I would like to use the values in the 'StartTime' and 'EndTime' columns. I am probably making this more difficult than it needs to be by using the between_time function.
我希望输出看起来像这样.
I would like the output to looks something like this.
EndHour StartHour Station_ID between_business_hours
Date
2016-11-17 05:01:45 10:00 16:00 A True
2011-01-04 16:34:00 10:00 16:00 A True
2011-01-05 09:25:45 10:00 16:00 A True
2011-01-10 12:00:45 10:00 16:00 A False
2011-01-14 07:05:45 10:00 16:00 B True
2011-01-15 10:19:00 10:00 16:00 B False
2011-01-17 13:59:45 10:00 16:00 B False
2011-01-19 18:39:45 10:00 16:00 B True
2011-01-22 06:19:45 10:00 16:00 B True
感谢您的帮助
推荐答案
您不需要设置index
df.Date.dt.strftime('%H:%M').between(df.StartHour,df.EndHour)
Out[297]:
0 False
1 True
2 True
3 True
4 False
5 True
6 True
7 True
8 False
dtype: bool
更新
l=[df.loc[[y],:].index.indexer_between_time(df.loc[y,'StartHour'],df.loc[y,'EndHour'])==0 for y in df.index]
df['New']=l
df.New=df.New.str[0].fillna(False)
df
EndHour StartHour Station_ID New
Date
2016-11-17 05:01:45 10:00 16:00 A True
2011-01-04 16:34:00 10:00 16:00 A True
2011-01-05 09:25:45 10:00 16:00 A True
2011-01-10 12:00:45 10:00 16:00 A False
2011-01-14 07:05:45 10:00 16:00 B True
2011-01-15 10:19:00 10:00 16:00 B False
2011-01-17 13:59:45 10:00 16:00 B False
2011-01-19 18:39:45 10:00 16:00 B True
2011-01-22 06:19:45 10:00 16:00 B True
这篇关于计算日期值是否出现在两个不同的时间之间python pandas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!