I have a dataframe that tracks when daytime rules or nighttime rules are in effect (independent of the actual time of day). I need to track when the dataframe switches from daytime to to nighttime rules, and vice versa.
My new column would display day_1
for the first set of day
in rules_in_effect
Then, when rules_in_effect
switches from day
to night
, the new column would display the change as night_1
. Then, day_2
, night_2
and so on for each corresponding switch.
I tried categorically differencing them and cumulatively summing the categorical changes, however that just gives me a summation.
How do I add this new column? Thank you for your help in advance.
date rules_in_effect
0 2021-01-04 07:00:00 day
1 2021-01-04 08:00:00 day
2 2021-01-04 09:00:00 day
3 2021-01-04 10:00:00 day
4 2021-01-04 11:00:00 day
5 2021-01-04 12:00:00 day
6 2021-01-04 13:00:00 day
7 2021-01-04 14:00:00 day
8 2021-01-04 15:00:00 day
9 2021-01-04 16:00:00 day
10 2021-01-04 17:00:00 day
11 2021-01-04 18:00:00 night
12 2021-01-04 19:00:00 night
13 2021-01-04 20:00:00 night
14 2021-01-04 21:00:00 night
15 2021-01-04 22:00:00 night
16 2021-01-04 23:00:00 night
17 2021-01-05 00:00:00 night
18 2021-01-05 01:00:00 night
19 2021-01-05 02:00:00 night
20 2021-01-05 03:00:00 night
21 2021-01-05 04:00:00 night
22 2021-01-05 05:00:00 night
23 2021-01-05 06:00:00 night
24 2021-01-05 07:00:00 day
25 2021-01-05 08:00:00 day
26 2021-01-05 09:00:00 day
27 2021-01-05 10:00:00 day
28 2021-01-05 11:00:00 day
29 2021-01-05 12:00:00 day
30 2021-01-05 13:00:00 day
31 2021-01-05 14:00:00 day
32 2021-01-05 15:00:00 day
33 2021-01-05 16:00:00 day
34 2021-01-05 17:00:00 day
35 2021-01-05 18:00:00 night
36 2021-01-05 19:00:00 night
37 2021-01-05 20:00:00 night
38 2021-01-05 21:00:00 night
etc...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…