Reputation: 4807
I have a dataframe as follows:
Month Col1 Col2
1/1/2019 2 ca
2/1/2019 10 bg
I want to get the following:
Month Col1 Col2
1/1/2019 2 ca
1/2/2019 2 ca
......rest days from 1/3 to 1/30 are here
1/31/2019 2 ca
2/1/2019 10 bg
2/2/2019 10 bg
-------
2/28/2019 10 bg
I am not sure how to implement this pythonically.
Edit: Month is not in index
Upvotes: 1
Views: 118
Reputation: 2417
try:
from pandas.tseries.offsets import MonthEnd
df.Month = pd.to_datetime(df.Month)
df = df.set_index(['Month'])[['Col1', 'Col2']]
def add_index(s):
m = s.name
index = pd.date_range(m, m + MonthEnd(n=1))
o = s.to_frame().T.reindex(index)
return o.ffill()
pd.concat([add_index(s) for d, s in df.iterrows()]).reset_index()
Upvotes: 1