vp7
vp7

Reputation: 388

Generate date data for a dataframe

I have a dataframe with total 30 entries for date_1 (01/09/2019 to 30/09/2019). I want to generate another dataframe such that :

  1. each date_1, we have 29 entries and each date_1 corresponds to date_2 such that date_2 is of the range (date_1 - 30 to date_1 - 1).

For example, 01/09/2019 would actually correspond to date_2 from (02/08/2019 - 31/08/2019) 02/09/2019 would correspond to date_2 from (03/08/2019 - 01/09/2019).

and so on

The final dataset columns would look like :

Date_1 Date_2

Can anyone help with the same.

EDIT

Expected Output :

Date_1  Date_2
01/09/2019  02/08/2019
01/09/2019  03/08/2019
01/09/2019  04/08/2019
01/09/2019  05/08/2019
.
.
.
.
01/09/2019  29/08/2019
01/09/2019  30/08/2019
02/09/2019  03/08/2019
.
.
.
02/09/2019  31/08/2019

Thanks

Upvotes: 0

Views: 64

Answers (1)

jlesueur
jlesueur

Reputation: 326

Does this code give the result you expect ?

import pandas as pd
import numpy as np

df = pd.DataFrame({'Date_1' : pd.period_range('2019/09/01', '2019/09/30', freq='D').repeat(30)})

df['Date_2'] = np.tile(np.arange(30, 0, -1), df.Date_1.unique().size)

df.Date_2 = pd.to_timedelta(df.Date_2, 'D')
df.Date_2 = df.Date_1 - df.Date_2

print(df)

First answer :

"Here is a way to do it (not exactly the format you have asked for "Date_2", but it is the idea) :

import pandas as pd
import numpy as np

df = pd.DataFrame({'Date_1' : pd.period_range('2019/09/01', '2019/09/30', freq='D')})

df['Date_2_1'] = df.Date_1 - np.timedelta64(30, 'D') # First date for Date_2
df['Date_2_2'] = df.Date_1 - np.timedelta64(1, 'D') # Second date for Date_2

print(df)

"

Upvotes: 1

Related Questions