Я предлагаю объединить other_var
и name
, а затем выполнить фильтрацию путем сравнения столбцов даты и времени:
import pandas as pd
# Reproducing your data
dfa = pd.DataFrame({"start_date": ["2018-05-01", "2018-05-02", "2018-05-10", "2018-05-04"],
"end_date": ["2018-05-01", "2018-05-04", "2018-05-11", "2018-05-09"],
"other_var": ["7H", "1H", "2H", "1H"],
"name": ["companyB", "companyC", "companyA", "companyB"]})
dfb = pd.DataFrame({"open_date": ["2018-07-06", "2018-07-13", "2018-04-23", "2018-04-05", "2018-05-03"],
"close_date": ["2019-02-17", "2018-11-04", "2018-05-08", "2018-10-01", "2018-06-01"],
"other_var": ["1H", "1H", "1H", "7H", "1H"],
"name": ["companyA", "companyB", "companyB", "companyB", "companyB"]})
df = pd.merge(dfa, dfb, on=["other_var", "name"])
df[["start_date", "end_date", "open_date", "close_date"]] = \
df[["start_date", "end_date", "open_date", "close_date"]].apply(pd.to_datetime)
df = df.loc[(df["start_date"]>=df["open_date"]) & (df["end_date"]<=df["close_date"]),:]
Результат -
start_date end_date other_var name open_date close_date
0 2018-05-01 2018-05-01 7H companyB 2018-04-05 2018-10-01
3 2018-05-04 2018-05-09 1H companyB 2018-05-03 2018-06-01