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 :)
pandas.date_range() is one of the general functions in Pandas which is used to return a fixed frequency DatetimeIndex. Syntax: pandas.date_range(start=None, end=None, periods=None, freq=None, tz=None, normalize=False
Date Range =TEXT (A2,”mmm d”) & IF (B2<> “”, “-” & TEXT (B2,”mmm d”), “”) So we can see that the above formula creates a full date range using both the dates when both are present. However, it displays only the start date in the specified format if the end date is missing. This is done with the help of an IF clause.
The result is a list of 10 days that range from the specified start date to the specified end date. The following code shows how to create a date range that has a specific number of equally-spaced periods between a certain start and end date:
For setting date ranges in Excel, we can first format the cells that have a start and end date as ‘Date’ and then use the operators: ‘+’ or ‘-‘to determine the end date or range duration. Let us see how adding a number to date creates a date range.
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With