In pandas, I can reindex()
the dataframe using multi-index to make the date range consistent for each group. Is there any way to produce the same result in polars?
See example below using pandas:
import pandas as pd
data = pd.DataFrame({
"date":pd.date_range("2022-01-01", "2022-06-01", freq="MS"),
"group":["A", "A", "A", "B", "B", "B"],
"value":[10,20,30,40,50,60]
}).set_index(["group", "date"])
new_index = pd.MultiIndex.from_product([data.index.levels[0].tolist(), data.index.levels[1].tolist()], names=["group", "date"])
data.reindex(new_index)
which transform data from:
value
group date
A 2022-01-01 10
2022-02-01 20
2022-03-01 30
B 2022-04-01 40
2022-05-01 50
2022-06-01 60
to below where both groups are having the same date range:
value
group date
A 2022-01-01 10.0
2022-02-01 20.0
2022-03-01 30.0
2022-04-01 NaN
2022-05-01 NaN
2022-06-01 NaN
B 2022-01-01 NaN
2022-02-01 NaN
2022-03-01 NaN
2022-04-01 40.0
2022-05-01 50.0
2022-06-01 60.0