Home > front end >  Python Pandas split day in 5 minute increments and repeat data
Python Pandas split day in 5 minute increments and repeat data

Time:03-26

I have a Pandas DataFrame that has the following sample data:

24-02-2022,51
25-02-2022,100
...

And I would like to split it into minutes like so:

2022-02-24 00:00:00,51
2022-02-24 00:05:00,51
2022-02-24 00:10:00,51
...
2022-02-25 23:55:00,51

CodePudding user response:

You can use resample:

new_df = df.resample(on='d', rule='5T').first().ffill().drop('d', axis=1).reset_index()

Output:

>>> new_df
                      d      x
0   2022-02-24 00:00:00   51.0
1   2022-02-24 00:05:00   51.0
2   2022-02-24 00:10:00   51.0
3   2022-02-24 00:15:00   51.0
4   2022-02-24 00:20:00   51.0
..                  ...    ...
284 2022-02-24 23:40:00   51.0
285 2022-02-24 23:45:00   51.0
286 2022-02-24 23:50:00   51.0
287 2022-02-24 23:55:00   51.0
288 2022-02-25 00:00:00  100.0

[289 rows x 2 columns]

(T is for minute; if you prefer, you can use min, i.e. 5min)

  • Related