Below is the Sales
table which has following data:
Sales:
S_ID S_QTY S_PRD S_ST_DT S_END_DT
1 223 AA 2018-06-02 2018-06-04
2 442 FO 2018-05-10 2018-05-12
3 771 WA 2018-07-07 2018-07-10
4 663 AAG 2018-03-02 2018-03-03
I am trying to get the dates between the S_ST_DT
and S_END_DT
.
Expecting the following O/P in DB2-SQL
and Pandas
:
Expected O/P:
S_ID S_QTY S_PRD S_DT
1 223 AA 2018-06-02
1 223 AA 2018-06-03
1 223 AA 2018-06-04
2 442 FO 2018-05-10
2 442 FO 2018-05-11
2 442 FO 2018-05-12
3 771 WA 2018-07-07
3 771 WA 2018-07-08
3 771 WA 2018-07-09
3 771 WA 2018-07-10
4 663 AAG 2018-03-02
4 663 AAG 2018-03-03
Any suggestions here?
pop
and extract the last two columnspd.date_range
np.repeat
df
from itertools import chain
v = [pd.date_range(x, y)
for x, y in zip(df.pop('S_ST_DT'), df.pop('S_END_DT'))]
df = (pd.DataFrame(df.values.repeat([len(u) for u in v], axis=0),
columns=df.columns)
.assign(S_DT=list(chain.from_iterable(v))))
print(df)
S_ID S_QTY S_PRD S_DT
0 1 223 AA 2018-06-02
1 1 223 AA 2018-06-03
2 1 223 AA 2018-06-04
3 2 442 FO 2018-05-10
4 2 442 FO 2018-05-11
5 2 442 FO 2018-05-12
6 3 771 WA 2018-07-07
7 3 771 WA 2018-07-08
8 3 771 WA 2018-07-09
9 3 771 WA 2018-07-10
10 4 663 AAG 2018-03-02
11 4 663 AAG 2018-03-03
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