Python Pandas如何获取最接近给定日期的日期

0  09-2019
1  10-2019
2  11-2019
4  01-2020
5  02-2020
7  04-2020

I have a pandas dataframe dates like the above - this isn't ordered in any way. I set the dates column as datetime type and select the month and year only like so:

dates['date'] = pd.to_datetime(dates['date'], errors='ignore')
dates['date'] = dates['date'].dt.strftime("%m-%Y")


latest_date = max(dates['date'])
latest_date = latest_date.strftime("%m-%Y")

This gives me 05-2020. How do I use the nearest method to select the date from dates which is nearest or equal to latest_date? The expected output from the example should be 04-2020.



But I get a AttributeError: type object 'datetime.datetime' has no attribute 'datetime'. Does this mean that I'm not correctly converting the dates column?