Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用于移位的数据帧_Python_Pandas_Dataframe_Datetime_Time - Fatal编程技术网

Python 用于移位的数据帧

Python 用于移位的数据帧,python,pandas,dataframe,datetime,time,Python,Pandas,Dataframe,Datetime,Time,我对时间有一个概念。我要做的是创建另一列并将其设置为Shift。 每天早上7点到晚上7点是白班,晚上7点到早上7点是夜班 例:2/11:如果时间btw 2/11上午7点到2/11下午7点是2-11天,2/11下午7点到2/12上午7点是2/11晚上。 要创建“日班”列,我首先创建了“日期”,然后创建了“日期班”列 但问题是,我的代码对每天的“白班”(早上7点到晚上7点)进行了正确分类,但没有对“夜班”进行正确分类。请检查突出显示的行 例:第21行:其白班值应为“01-07夜”,而不是“01-08

我对时间有一个概念。我要做的是创建另一列并将其设置为Shift。 每天早上7点到晚上7点是白班,晚上7点到早上7点是夜班

例:2/11:如果时间btw 2/11上午7点到2/11下午7点是2-11天,2/11下午7点到2/12上午7点是2/11晚上。

要创建“日班”列,我首先创建了“日期”,然后创建了“日期班”列

但问题是,我的代码对每天的“白班”(早上7点到晚上7点)进行了正确分类,但没有对“夜班”进行正确分类。请检查突出显示的行

例:第21行:其白班值应为“01-07夜”,而不是“01-08夜”

霉菌代码:

df2["Date"]=df2['Time'].astype(str).str[:10]
df2["Shift"] = pd.to_datetime(df2['Time'],unit='s').apply(lambda x: "Day" if x.hour >= 7 and x.hour <= 18 else "Night")
df2['Date']=df2['Date'].astype(str)
df2['Date'] = df2['Date'].str[5:]
df2["Day-Shift"]=df2["Date"]+" "+df2["Shift"]
df2.head(2)

如果小时小于中的
7
in,则可以从
Time
中减去一天,然后通过比较中的小时来创建
Shift
,并设置提取日期和月份的值by和last join列:

df['Date'] = (df['Time'].mask(df['Time'].dt.hour.lt(7), 
                              df['Time'] - pd.offsets.DateOffset(days=1)))

df["Shift"] = np.where(df['Time'].dt.hour.between(7, 18), 'Day','Night')

df["Day-Shift"] = df["Date"].dt.strftime('%m-%d') + " " + df["Shift"]


为了增加获得答案的可能性,请将数据框作为文本而不是图像提供。我将数据框作为字典提供。第一张图片只是为了识别我的问题。请检查问题的结尾。你能检查一下这个吗?
df['Date'] = (df['Time'].mask(df['Time'].dt.hour.lt(7), 
                              df['Time'] - pd.offsets.DateOffset(days=1)))

df["Shift"] = np.where(df['Time'].dt.hour.between(7, 18), 'Day','Night')

df["Day-Shift"] = df["Date"].dt.strftime('%m-%d') + " " + df["Shift"]
print (df)
                  Time                Date  Shift    Day-Shift
17 2021-01-07 23:11:53 2021-01-07 23:11:53  Night  01-07 Night
18 2021-01-07 23:11:53 2021-01-07 23:11:53  Night  01-07 Night
19 2021-01-07 23:29:13 2021-01-07 23:29:13  Night  01-07 Night
20 2021-01-07 23:29:13 2021-01-07 23:29:13  Night  01-07 Night
21 2021-01-08 00:12:23 2021-01-07 00:12:23  Night  01-07 Night
22 2021-01-08 00:12:23 2021-01-07 00:12:23  Night  01-07 Night
23 2021-01-08 00:19:43 2021-01-07 00:19:43  Night  01-07 Night
24 2021-01-08 00:19:43 2021-01-07 00:19:43  Night  01-07 Night
25 2021-01-08 00:58:13 2021-01-07 00:58:13  Night  01-07 Night
26 2021-01-08 00:58:13 2021-01-07 00:58:13  Night  01-07 Night
27 2021-01-08 01:24:13 2021-01-07 01:24:13  Night  01-07 Night
28 2021-01-08 01:24:13 2021-01-07 01:24:13  Night  01-07 Night
29 2021-01-08 06:31:09 2021-01-07 06:31:09  Night  01-07 Night
30 2021-01-08 06:31:09 2021-01-07 06:31:09  Night  01-07 Night
31 2021-01-08 06:54:39 2021-01-07 06:54:39  Night  01-07 Night
32 2021-01-08 06:54:39 2021-01-07 06:54:39  Night  01-07 Night
33 2021-01-08 06:54:49 2021-01-07 06:54:49  Night  01-07 Night
34 2021-01-08 07:00:00 2021-01-08 07:00:00    Day    01-08 Day
35 2021-01-08 07:16:29 2021-01-08 07:16:29    Day    01-08 Day
36 2021-01-08 07:17:59 2021-01-08 07:17:59    Day    01-08 Day
37 2021-01-08 07:17:59 2021-01-08 07:17:59    Day    01-08 Day
38 2021-01-08 07:28:39 2021-01-08 07:28:39    Day    01-08 Day
39 2021-01-08 07:28:39 2021-01-08 07:28:39    Day    01-08 Day
40 2021-01-08 07:48:59 2021-01-08 07:48:59    Day    01-08 Day
41 2021-01-08 07:48:59 2021-01-08 07:48:59    Day    01-08 Day
42 2021-01-08 10:04:59 2021-01-08 10:04:59    Day    01-08 Day
43 2021-01-08 10:07:59 2021-01-08 10:07:59    Day    01-08 Day
44 2021-01-08 12:19:49 2021-01-08 12:19:49    Day    01-08 Day
45 2021-01-08 12:19:49 2021-01-08 12:19:49    Day    01-08 Day
46 2021-01-08 12:24:09 2021-01-08 12:24:09    Day    01-08 Day
47 2021-01-08 12:24:09 2021-01-08 12:24:09    Day    01-08 Day
48 2021-01-08 18:19:05 2021-01-08 18:19:05    Day    01-08 Day
49 2021-01-08 18:19:05 2021-01-08 18:19:05    Day    01-08 Day