I have kept your initial input dataframe as d
and created a new dataframe df
. The key idea is that you need to have a common month
column in both dataframes; and, after you .resample('W')
the dataframe, you can merge back in the data
values from the initial dataframe.
If the data in your first dataframe is a dtype of period
, then you can reference this thread on how to make that a string, but once you do that the below code should work flawlessly: Python/Pandas - Convert type from pandas period to string
import numpy as np
import pandas as pd
d = pd.read_clipboard().rename({'date' : 'month'}, axis=1)
d
Out[1]:
month data
0 2019-01 95972.0
1 2019-02 95951.0
df = d.copy()
df['date'] = pd.to_datetime(df['month'])
df['date'] = np.where((df['date'] == max(df['date'])), df['date'] + pd.tseries.offsets.MonthEnd(), df['date'])
df = (df.set_index('date')
.resample('W')
.asfreq()
.reset_index()
.drop('data', axis=1))
df['month'] = df['date'].dt.strftime('%Y-%m')
df['date'] = df['date'] + pd.tseries.offsets.Day()
df = pd.merge(df,d).drop('month', axis=1)
df
Out[2]:
date data
0 2019-01-07 95972.0
1 2019-01-14 95972.0
2 2019-01-21 95972.0
3 2019-01-28 95972.0
4 2019-02-04 95951.0
5 2019-02-11 95951.0
6 2019-02-18 95951.0
7 2019-02-25 95951.0