Python Forum

Full Version: Auto row increment code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All, I have been given the data

Date count
1/1/2020 1
2/1/2020 5
4/1/2020 10
6/1/2020 15

Here i need a python code to auto fill the missing dates against date & count should be previous month's value retained

Date count
1/1/2020 1
2/1/2020 5
3/1/2020 5
4/1/2020 10
5/1/2020 10
6/1/2020 15
So what have you tried?
Time being I have used Autofill was working with Range function however am not able to retrieve previous month value..
import pandas as pd

idx = pd.date_range('1/1/2020', '6/1/2020')

s = pd.Series({'1/1/2020': 1,
               '2/1/2020': 5,
               '4/1/2020': 10,
               '6/1/2020': 15})
s.index = pd.DatetimeIndex(s.index)

s = s.reindex(idx, fill_value=0)
print(s)