'Pandas sorting and merging df by date

I have a problem with merging my data.

I have two tables. One with data about a customers order and the another one with shipment details.

An order is identified by the customer and the date the shipment has to be done (those are the keys)

the shipment can only be identified by the ordernumber and the date it is send.

I want to join both tables to see, what amount was ordered and shiped (to see if there are differences)

Here comes the clue. The dates (keys) are not the same and can be delayed by some days or be send ahead of time.

How can I join those tables? Between two orders of a customer no other shipment to that costumer is done.

Example of the data.

Indexe Customer Date       Amount
1.     12       2020-10-29    x
2.     12       2020-12-29    x
3.     14       2021-01-15    x
4.     15       2021-03-14    x
5.     16       2021-04-01    x
6.     16       2021-04-10    x

Table of shipments

Index Customer Date       Amount
1.    12       2020-11-01  x
2.    12       2021-01-05  x
3.    14       2021-03-16  x
4.    15       2016-05-12  x
5.    16       2017-03-16  x
6.`   16       2021-04-05  x
7.    16       2021-04-12  x

Outcome
Index  Customer Date_x      Date_y       Amount_x  Amount_y
1.     12       2020-10-29  2020-11-01     x        x
2.     12       2020-12-29  2021-01-05     x        x
3.     14       2021-01-15  2021-03-16     x        x
4.     16       2021-04-01  2021-04-05     x        x
5.     16       2021-04-10  2021-04-12     x        x

Many Thanks in advance.

I tried to cover all special cases.



Solution 1:[1]

I was did a not perfect solution. I cut the date so its presented as yyyy-mm and then merged both frames on key customer and date with an inner join. there are cases, where this solution is not working like row 1. in the example, but it was a quick fix for me.

I will not mark the question as answered, because this solution is nor fully accurate for the problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1