I have a df like, which has such rows:
p_id m_id x_id g_id u_id
0 2 NaN 1408 7 121
1 3 1259 117 23 315
2 3 1259 221 9 718
3 3 1259 397 76 367
and two datetime objects:
start_date:
datetime.datetime(2021, 5, 25, 0, 0)
end_date:
datetime.datetime(2021, 5, 29, 0, 0)
how do I get a df like, basically (adding the week-dates from start_date to end_date with each row):
p_id m_id x_id g_id u_id s_date
0 2 NaN 1408 7 121 2021-05-25
1 2 NaN 1408 7 121 2021-05-26
2 2 NaN 1408 7 121 2021-05-27
3 2 NaN 1408 7 121 2021-05-28
4 2 NaN 1408 7 121 2021-05-29
5 3 1259 117 23 315 2021-05-25
6 3 1259 117 23 315 2021-05-26
7 3 1259 117 23 315 2021-05-27
8 3 1259 117 23 315 2021-05-28
9 3 1259 117 23 315 2021-05-29
.
.
15 3 1259 397 76 367 2021-05-25
16 3 1259 397 76 367 2021-05-26
17 3 1259 397 76 367 2021-05-27
18 3 1259 397 76 367 2021-05-28
19 3 1259 397 76 367 2021-05-29
date_range and cross merge1.2x, to perform a cross merge we can now pass an optional parameter how='cross' to the merge functiondates = pd.date_range(start_date, end_date)
df.merge(dates.to_series(name='s_date'), how='cross')
1.2x we have to create a temporary merge key in order to perform the cross mergedates = pd.date_range(start_date, end_date)
df.assign(k=1).merge(dates.to_frame(name='s_date').assign(k=1), on='k').drop('k', 1)
p_id m_id x_id g_id u_id s_date
0 2 NaN 1408 7 121 2021-05-25
1 2 NaN 1408 7 121 2021-05-26
2 2 NaN 1408 7 121 2021-05-27
3 2 NaN 1408 7 121 2021-05-28
4 2 NaN 1408 7 121 2021-05-29
5 3 1259.0 117 23 315 2021-05-25
6 3 1259.0 117 23 315 2021-05-26
7 3 1259.0 117 23 315 2021-05-27
8 3 1259.0 117 23 315 2021-05-28
9 3 1259.0 117 23 315 2021-05-29
10 3 1259.0 221 9 718 2021-05-25
11 3 1259.0 221 9 718 2021-05-26
12 3 1259.0 221 9 718 2021-05-27
13 3 1259.0 221 9 718 2021-05-28
14 3 1259.0 221 9 718 2021-05-29
15 3 1259.0 397 76 367 2021-05-25
16 3 1259.0 397 76 367 2021-05-26
17 3 1259.0 397 76 367 2021-05-27
18 3 1259.0 397 76 367 2021-05-28
19 3 1259.0 397 76 367 2021-05-29
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With