Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating new DataFrame with regular datetime from a df with non regular datetime

I have a DataFrame like this

df
           dateEntry              dataReceived  
0   2021-12-22 15:00:34.359293        0 
1   2021-12-22 15:00:56.052554        1 
2   2021-12-22 15:02:12.408687        0 
3   2021-12-22 15:02:18.764644        1 
4   2021-12-22 15:03:26.959721        0 
5   2021-12-22 15:03:38.039307        1 
6   2021-12-22 15:05:59.347346        0 
7   2021-12-22 15:06:22.955319        1 

dateEntry is type datetime64[ns].

dataReceived is always an alternation between 0 and 1. For exemple at the first row, it mean that the person is not moving (label 0) till the next row, so the person is not moving for 56-34 =22 secondes

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I want to create an other dataframe but with regular timestep for exemple it start from 2021_12_22 15:00:40 with 15 secondes timestep.

To assign a value in the new DataFrame, I consider that the new datetime takes the value of the lower limit of the interval in which it is contained:

Desired OUTPUT

df_new

        dateEntry          dataReceived
0   2021-12-22 15:00:40         0
1   2021-12-22 15:00:55         0
2   2021-12-22 15:01:10         1
3   2021-12-22 15:01:25         1
4   2021-12-22 15:01:40         1
...
    2021-12-22 15:05:55         1
    2021-12-22 15:06:10         0

How can I get it?

>Solution :

IIUC, you need resample:

df['dateEntry'] = pd.to_datetime(df['dateEntry'])

df2 = (df.set_index('dateEntry')
         .resample('15s', origin='2021-12-22 15:00:40', closed='right')
         .ffill()
         .reset_index()
      )

output:

             dateEntry  dataReceived
0  2021-12-22 15:00:40             0
1  2021-12-22 15:00:55             0
2  2021-12-22 15:01:10             1
3  2021-12-22 15:01:25             1
4  2021-12-22 15:01:40             1
5  2021-12-22 15:01:55             1
...
20 2021-12-22 15:05:40             1
21 2021-12-22 15:05:55             1
22 2021-12-22 15:06:10             0
23 2021-12-22 15:06:25             1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading