Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: resample a dataframe to match a DatetimeIndex of a different dataframe

I have a two time series in separate pandas.dataframe, the first one - series1 has less entries and different start datatime from the second - series2:

index1 = pd.date_range(start='2020-06-16 23:16:00', end='2020-06-16 23:40:30', freq='1T')
series1 = pd.Series(range(len(index1)), index=index1)
index2 = pd.date_range('2020-06-16 23:15:00', end='2020-06-16 23:50:30', freq='30S')
series2 = pd.Series(range(len(index2)), index=index2)

How can I resample series2 to match the DatetimeIndex of series1?

like image 259
Ohm Avatar asked Aug 09 '20 11:08

Ohm


1 Answers

Use reindex:

series2.reindex(series1.index)

Output:

2020-06-16 23:16:00     2
2020-06-16 23:17:00     4
2020-06-16 23:18:00     6
2020-06-16 23:19:00     8
2020-06-16 23:20:00    10
2020-06-16 23:21:00    12
2020-06-16 23:22:00    14
2020-06-16 23:23:00    16
2020-06-16 23:24:00    18
2020-06-16 23:25:00    20
2020-06-16 23:26:00    22
2020-06-16 23:27:00    24
2020-06-16 23:28:00    26
2020-06-16 23:29:00    28
2020-06-16 23:30:00    30
2020-06-16 23:31:00    32
2020-06-16 23:32:00    34
2020-06-16 23:33:00    36
2020-06-16 23:34:00    38
2020-06-16 23:35:00    40
2020-06-16 23:36:00    42
2020-06-16 23:37:00    44
2020-06-16 23:38:00    46
2020-06-16 23:39:00    48
2020-06-16 23:40:00    50
Freq: T, dtype: int64
like image 191
Scott Boston Avatar answered Sep 18 '22 10:09

Scott Boston