【问题标题】:calculate if date value occurs between two different times python pandas计算日期值是否出现在两个不同的时间之间 python pandas
【发布时间】:2019-06-09 19:17:53
【问题描述】:

我正在尝试创建一个新列以确定行值是否为“工作时间之间”。为此,我尝试使用 between time 函数。 如果有更简单的方法,我不需要使用它。

我有一个数据框,其中包含 'Date''StartHour''End Hour' 列。

问题:

如果“Date”列中的时间介于“StartHour”和“”之间,我想给出“True”或“False” EndHour的时间。

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”列中的值。通过使用 between_time 函数,我可能使这变得比需要的更困难。

我希望输出看起来像这样。

                    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

感谢任何帮助

【问题讨论】:

    标签: python pandas time between


    【解决方案1】:

    你不需要设置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
    

    【讨论】:

    • 谢谢!这比我所做的要优雅得多。它非常适合我想做的事情。
    • 我认为您的回答有效。但是在深入研究之后,我发现了一个大问题。仅当开始时间和结束时间在同一天时,您的代码才有效。 IE,上午 10 点开始,下午 4 点结束。我遇到的问题是“开始时间”发生在一天结束时,比如说 16:00 小时,而“结束时间”发生在第二天,比如说“上午 10:00”。正如您从新示例中看到的那样,我当前的代码有效,但您的代码给出了错误的读数
    • @brandog 你想让我修复它吗>
    • 好像没有解决问题?一切都还是假的?
    • @brandog 我认为如果发生这种情况,您应该选择between_time
    猜你喜欢
    • 1970-01-01
    • 2022-08-15
    • 2010-09-17
    • 2012-01-31
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    相关资源
    最近更新 更多