Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 :)

like image 313
tyleroki Avatar asked Dec 17 '20 23:12

tyleroki


People also ask

What is the use of date range in pandas?

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

How do you find the range of a date?

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.

How many days are there in a date range?

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:

How do you create a date range in Excel?

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.


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())
like image 167
A DUBEY Avatar answered Oct 22 '22 02:10

A DUBEY