Sep-14-2024, 01:53 PM
(This post was last modified: Sep-14-2024, 01:53 PM by deanhystad.)
Can you provide sample input and desired output?
Are time sequence numbers always consecutive in a segment? If so, you could use the shift command to find numbers out of sequence.
Are time sequence numbers always consecutive in a segment? If so, you could use the shift command to find numbers out of sequence.
import pandas as pd # Make some data df = pd.DataFrame({"time":[-2, -1, 0, 1, -1, 0, 1, 0, 1, 2, 3]}) # Make new series that is the time series data down 1 row + 1. As long as numbers in time # are sequential, time == shifted time. Set first value in shifted time to something that won't # match anything in time. df["shifted"] = (df.time.shift(1, fill_value=0.5) + 1) # Find where time != shifted time. This is the start of a series. df["mismatch"] = df.time != df.shifted # Count the mismatches. This is the series number. df["series"] = df.mismatch.cumsum() print(df)
Output: time shifted mismatch series
0 -2 1.5 True 1
1 -1 -1.0 False 1
2 0 0.0 False 1
3 1 1.0 False 1
4 -1 2.0 True 2
5 0 0.0 False 2
6 1 1.0 False 2
7 0 2.0 True 3
8 1 1.0 False 3
9 2 2.0 False 3
10 3 3.0 False 3
Doing it all in one line.import pandas as pd df = pd.DataFrame({"time":[-2, -1, 0, 1, -1, 0, 1, 0, 1, 2, 3]}) df["series"] = (df.time != (df.time.shift(1, fill_value=0.5) + 1)).cumsum() print(df)
Output: time series
0 -2 1
1 -1 1
2 0 1
3 1 1
4 -1 2
5 0 2
6 1 2
7 0 3
8 1 3
9 2 3
10 3 3