Home > database >  Generate time series dataframe with min and max time with the given interval pandas
Generate time series dataframe with min and max time with the given interval pandas

Time:01-17

How can I generate a time series dataset with min and max date range with the specific interval in pandas?

 min_date = 18 oct 2022
 Max_date = 20 Oct 2022
 interval = 1 hour

Min_date              Max_date
18/10/2022 00:00:00   18/10/2022 01:00:00
18/10/2022 01:00:00   18/10/2022 02:00:00
18/10/2022 02:00:00   18/10/2022 03:00:00
18/10/2022 03:00:00   18/10/2022 04:00:00
19/10/2022 22:00:00   18/10/2022 23:00:00
19/10/2022 23:00:00   18/10/2022 23:59:00

Thanks in advance

CodePudding user response:

import pandas as pd

min_date = pd.Timestamp('oct 18, 2022')
max_date = pd.Timestamp('oct 20, 2022')
interval = pd.offsets.Hour( 1)

df = pd.DataFrame(pd.date_range(min_date, max_date - interval, freq = interval), columns = ['Min_date'])
df['Max_date'] = df['Min_date']   interval

print(df)

Output:

              Min_date            Max_date
0  2022-10-18 00:00:00 2022-10-18 01:00:00
1  2022-10-18 01:00:00 2022-10-18 02:00:00
2  2022-10-18 02:00:00 2022-10-18 03:00:00
.
.
.
45 2022-10-19 21:00:00 2022-10-19 22:00:00
46 2022-10-19 22:00:00 2022-10-19 23:00:00
47 2022-10-19 23:00:00 2022-10-20 00:00:00
  • Related