Q: Pandas: Filtering data based on information from multiple dataframes I am analyzing clinical data and trying to filter out information in one dataframe based on information from another dataframe.
One of the dataframes lists dates that patients come in for treatment 
dfTreatments = pd.DataFrame({'PatientID': [4,4,4,9,9,9,11,11,11], 'TreatmentDate': ['2016-01-01', '2016-01-15', '2016-03-25','2016-01-01','2016-01-15','2016-01-29','2016-01-01','2016-03-15','2016-03-25']})
dfTreatments['TreatmentDate'] = pd.to_datetime(dfTreatments['TreatmentDate'])

   PatientID TreatmentDate
0          4    2016-01-01
1          4    2016-01-15
2          4    2016-03-25
3          9    2016-01-01
4          9    2016-01-15
5          9    2016-01-29
6         11    2016-01-01
7         11    2016-03-15
8         11    2016-03-25

and the other dataframe lists dates that patients visit a hospital with a complication.
dfHospitalVisits = pd.DataFrame({'PatientID': [4,4,9,11], 'HospitalVisitDate': ['2016-01-14','2016-03-10','2016-01-28','2016-01-03']})
dfHospitalVisits['HospitalVisitDate'] = pd.to_datetime(dfHospitalVisits['HospitalVisitDate'])

  HospitalVisitDate  PatientID
0        2016-01-14          4
1        2016-03-10          4
2        2016-01-28          9
3        2016-01-03         11

In our study, we want to exclude hospital visits from our analysis if the patient does not receive treatment for 20 days. We start excluding them at the last treatment before the 20 day gap. E.g.: We would exclude any hospital visits for patient 4 after 2016-01-15.
In this example, Patient 4's 2nd hospital visit and Patient 11's hospital visit would be removed from dfHospitalVisits.
Edit: @Merlin, so far I have used dfTreatments.groupby('PatientID')['TreatmentDate'].diff() to get me the gaps in treatment dates grouped by patient. The part where I am stuck is that I don't know how to use a difference in treatment dates that is >20 to filter out values in dfHospitalVisits .

A: I suggest the following:
# Make a sorted dataframe to calculate the time gap before the next treatment 
dfTreatments_sorted = dfTreatments.sort_values(['PatientID','TreatmentDate'], ascending=False)

# Calculate the time gap before the next treatment
df_diff = dfTreatments_sorted.groupby('PatientID').TreatmentDate.diff(periods=1).rename('Gap_before_next_treatment')

# Add the time gaps as a new column to your existing dfTreatments dataframe
dfTreatments = pd.concat([dfTreatments, -df_diff], axis=1, join='inner').sort_index()

# Join dfTreatments and dfHospitalVisits into new dataframe (df)
df = dfHospitalVisits.set_index('PatientID').join(dfTreatments.set_index('PatientID'))

# Select combination where TreatmentDate is before corresponding HospitalVisitDate
df = df[(df.HospitalVisitDate>df.TreatmentDate)]

# The TreatmentDate that is important is latest before the HospitalVisitDate
df = df.reset_index().groupby(['PatientID','HospitalVisitDate']).max()

# Now you can filter hospital visits given the calculated time gap
df = df[df.Gap_before_next_treatment<'20 days'].reset_index()[['PatientID','HospitalVisitDate']]

