Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find most recent date in pandas dataframe

Tags:

I have a csv file, that I read into a pandas dataframe. The date and times are listed in a column "DateTime". I want to find the most recent and the least recent date to create an index to create a time series graph. Does pandas have a function that will return the most recent and the least recent date?

Edit:
I already tried using min and max. They give incorrect answers.

>>> f['Start Date']   Trip ID   4576       8/29/2013 14:13   4607       8/29/2013 14:42   4130       8/29/2013 10:16   4251       8/29/2013 11:29   4299       8/29/2013 12:02   4927       8/29/2013 18:54   4500       8/29/2013 13:25   4563       8/29/2013 14:02   4760       8/29/2013 17:01   4258       8/29/2013 11:33   4549       8/29/2013 13:52   4498       8/29/2013 13:23   4965       8/29/2013 19:32   4557       8/29/2013 13:57   4386       8/29/2013 12:31   ...   198757     2/28/2014 20:40   198760     2/28/2014 20:59   198761     2/28/2014 20:59   198763     2/28/2014 21:32   198764     2/28/2014 21:32   198765     2/28/2014 21:34   198766     2/28/2014 21:41   198767     2/28/2014 21:50   198768     2/28/2014 21:54   198770     2/28/2014 22:19   198771     2/28/2014 22:15   198772     2/28/2014 22:38   198773     2/28/2014 22:45   198774     2/28/2014 23:01   198775     2/28/2014 23:20   Name: Start Date, Length: 144015, dtype: object   >>> min(f['Start Date'])   '1/1/2014 0:14'   >>> max(f['Start Date'])   '9/9/2013 9:59'   
like image 843
Sarah Connors Avatar asked Apr 27 '15 03:04

Sarah Connors


People also ask

How do I sort a date column in pandas?

One thing to notice here is our DataFrame gets sorted in ascending order of dates, to sort the DataFrame in descending order we can pass an additional parameter inside the sort_values() function that will set ascending value to False and will return the DataFrame in descending order.

How do I get pandas index?

Pandas DataFrame – Get Index To get the index of a Pandas DataFrame, call DataFrame. index property. The DataFrame. index property returns an Index object representing the index of this DataFrame.


1 Answers

First convert your date column in to a datetime column using

>> df['StartDate'] = pd.to_datetime(df['StartDate']) 

You then can find the oldest date and most recent date using

>> least_recent_date = df['StartDate'].min() >> most_recent_date = df['StartDate'].max() 
like image 150
Kathirmani Sukumar Avatar answered Oct 06 '22 00:10

Kathirmani Sukumar