How to find the range of dates from a datetime column in a dataframe?

Viewed 3935

Wondering how to print the range of dates in a dataframe. Seems like it would be very simple but I can't find answers anywhere. Is there an easy way to do this with pandas datetime module?

So if this was a small version of the dataframe for example:

Date Id Value
2020-09-23 14:00:00 4752764 12212
2020-10-25 08:00:00 4752764 12298
2020-10-28 12:00:00 4752764 12291
2020-10-29 18:00:00 4752764 12295

How could I get an output like:

date_range = 2020-09-23 to 2020-10-29

OR

date_range = 23rd of September, 2020 to 29th of October, 2020

I appreciate any answers :)

1 Answers

Try this

df['Date'] = pd.to_datetime(df['Date']) # If your Date column is of the type object otherwise skip this
date_range = str(df['Date'].dt.date.min()) + ' to ' +str(df['Date'].dt.date.max())
Related