Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicating rows based on a sequence of start date in Python

Tags:

python

pandas

I would like to duplicate the rows in a data frame by creating a sequence of n dates from the start date.

My input file format.

col1 col2    date
1    5    2015-07-15
2    6    2015-07-20
3    7    2015-07-25

My expected output.

col1 col2     date
1   5       2015-07-15
1   5       2015-07-16
1   5       2015-07-17
1   5       2015-07-18
1   5       2015-07-19
2   6       2015-07-20
2   6       2015-07-21
2   6       2015-07-22
2   6       2015-07-23
2   6       2015-07-24
3   7       2015-07-25
3   7       2015-07-26
3   7       2015-07-27
3   7       2015-07-28
3   7       2015-07-29

I have to create a sequence of dates with a day difference.

Thanks in advance.

like image 284
RSK Avatar asked Dec 31 '25 07:12

RSK


1 Answers

Use:

df['date'] = pd.to_datetime(df['date'])

n = 15
#create date range by periods
idx = pd.date_range(df['date'].iat[0], periods=n)
#create DatetimeIndex with reindex and forward filling values
df = (df.set_index('date')
        .reindex(idx, method='ffill')
        .reset_index()
        .rename(columns={'index':'date'}))
print (df)

         date  col1  col2
0  2015-07-15     1     5
1  2015-07-16     1     5
2  2015-07-17     1     5
3  2015-07-18     1     5
4  2015-07-19     1     5
5  2015-07-20     2     6
6  2015-07-21     2     6
7  2015-07-22     2     6
8  2015-07-23     2     6
9  2015-07-24     2     6
10 2015-07-25     3     7
11 2015-07-26     3     7
12 2015-07-27     3     7
13 2015-07-28     3     7
14 2015-07-29     3     7
like image 144
jezrael Avatar answered Jan 05 '26 01:01

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!