0

I want to match the information in two dataframes conditional on dates. For instance, my first dataframe has a column date:

DATE
2022-10-01
2022-10-22
2022-11-14

My second dataframe (dates may not coincide with start and end of month):

CATEGORY START END
1 2022-09-01 2022-09-30
2 2022-10-01 2022-10-31
3 2022-11-01 2022-11-30

The resulting dataframe should look like:

DATE CATEGORY START END
2022-10-01 2 2022-09-01 2022-09-30
2022-10-22 2 2022-10-01 2022-10-31
2022-11-14 3 2022-11-01 2022-11-30

Notice that DATE must be between START and END. I managed to find the result I wanted by merging the two dataframes (using other variables). However, merging generates additional rows that must be filtered afterwards, and this can be a problem if the data size is large.

Mikael Jagan
  • 9,012
  • 2
  • 17
  • 48
  • You might have to get creative and generate a shared id between the two dataframes based on your desired join condition. Maybe something using a pandas date_range could work? Sorry if this doesn't help much. – meowmeow Mar 01 '23 at 18:42
  • 1
    Are you sure that the first row of the expected result dataframe is right (especially start and end date)? – Michael Butscher Mar 01 '23 at 18:46
  • Duplicate? https://stackoverflow.com/questions/30627968/merge-pandas-dataframes-where-one-value-is-between-two-others – DobbyTheElf Mar 01 '23 at 19:22

0 Answers0