【问题标题】:grouping data using pandas datetime inot 12 hour shifts 7am - 7pm and 7pm - 7am the next day使用 pandas 日期时间将数据分组为 12 小时班次,早上 7 点 - 晚上 7 点和晚上 7 点 - 第二天早上 7 点
【发布时间】:2020-12-01 04:07:24
【问题描述】:

我有一个包含两列的数据熊猫数据框。它有一个名为“DateAndTime”的列(datetime64[ns]) 和一个名为“完成”(布尔)的列。大约有 5000 多行,所有行都有不同的日期和时间,并且“已完成”列为 True。

我要做的是将数据分组为上午 7 点至晚上 7 点和晚上 7 点至上午 7 点的“班次”,并总结在 12 小时期间发生了多少真。

df.head()

DateAndTime                   Finished
109 2020-07-28 14:36:07.983     True
110 2020-07-28 14:36:34.547     True
111 2020-07-28 14:39:38.187     True
112 2020-07-28 14:41:10.547     True
113 2020-07-28 14:41:32.250     True

df.describe()

       DateAndTime                    Finished
count   5915                            5915
unique  5915                            2
top     2020-07-29 07:34:25.360000      True
freq    1                               5914
first   2020-07-28 14:36:07.983000      NaN
last    2020-08-05 04:57:10.657000      NaN

【问题讨论】:

  • 使用df.between_time
  • 查看类似:stackoverflow.com/questions/63289803/… 您可以使用offset 重新采样。
  • 带偏移量的重采样正是我想要的。谢谢。
  • 最后我使用了这个df.resample('12h', base=7).count(),因为当我使用loffset时它没有正确分组

标签: python pandas datetime


【解决方案1】:

你应该试试这个

import numpy as np

#in case columns are in String Format 
df = df.astype({'DateAndTime': np.datetime64, 'Finished':np.bool}) 

# 7AM : 7PM Shift
shift_1 = df[df.DateAndTime.apply(lambda t: (t.hour in range(7, 19)) or (t.hour==19 and (t.second+t.minute==0)))]

# 7PM : 7AM Shift
shift_2 = df[df.DateAndTime.apply(lambda t: not ((t.hour in range(7, 19)) or (t.hour==19 and (t.second+t.minute==0))))]

shift_1_TruedCount = shift_1.Finished.to_list().count(True)
shift_2_TruedCount = shift_2.Finished.to_list().count(True)

print(shift_1_TruedCount, shift_2_TruedCount)

【讨论】:

    【解决方案2】:

    假设您的 DateAndTime 列已经是 Timestamp 类型:

    # Move DateAndTime back by 7 hours
    # Now shift 1 is 0:00 to 12:00, shift 2 is 12:00 - 24:00
    h = (df['DateAndTime'] - pd.Timedelta(hours=7)).dt.hour < 12
    df['Shift'] = h.map({True: '7am-7pm', False: '7pm-7am'})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      相关资源
      最近更新 更多