I have two datasets. One with supplies, other with sales. They have different dates and time.
Suppplies
Year Month Day Hour Item
0 2023 05 17 10 8
1 2023 06 01 12 8
2 2023 06 10 16 3
3 2023 06 17 10 8
4 2023 07 01 10 8
5 2023 08 17 10 8
Sales
Year Month Day Hour Sale
0 2023 05 17 16 3
1 2023 05 18 12 3
2 2023 05 24 16 3
3 2023 05 27 10 1
4 2023 06 02 10 2
5 2023 06 03 10 3
I need both information so i merged them
Year Month Day Hour Item Year Month Day Hour Item
0 2023 05 17 10 8 2023 05 17 16 3
1 2023 06 01 12 8 2023 05 18 12 3
2 2023 06 10 16 3 2023 05 24 16 3
3 2023 06 17 10 8 2023 05 27 10 1
4 2023 07 01 10 8 2023 06 02 10 2
5 2023 08 17 10 8 2023 06 03 10 3
I want that if there was no delivery on that day, then the date on the right is duplicated and the quantity is 0. Until a new delivery
I desire to get
Year Month Day Hour Item Year Month Day Hour Item
0 2023 05 17 10 8 2023 05 17 16 3
1 2023 NaN NaN NaN 0 2023 05 18 12 3
2 2023 NaN NaN NaN 0 2023 05 20 16 3
3 2023 NaN NaN NaN 0 2023 05 27 10 1
4 2023 06 01 12 8 2023 06 02 10 2
5 2023 NaN NaN NaN 0 2023 06 03 10 3
I would like to get this result, if the date value on the left is less than the date value on the right, and if the value in the column is 0 then it will be replaced by nan.
There are two ways how the two datasets can be combined in the desired way:
SQL-like join on the supplies and sales dates. In particular, a particular sales record will be joined with the current supply record when the sales date is equal to/after the date of the current supply record, where the date of the sales record must not be greater than the date of the subsequent supply record.To the best of my knowledge, conditional joins (as they exist in
SQL) cannot be done inpandassee this SO post hereBelow you can find the code showing how both approaches can be done, where the first approach also requires the
sqlite3module. I personally would recommend you the first approach as performing a cross join can be computationally very expensive.Code for first approach:
Code for second approach: