Reputation: 112
I am working with currently with a csv file that contains datetimes and timestamps. The dataframe look like this:
print(df[:10])
[0 '2019-10-10 21:59:17.074007' '2015-10-13 00:55:55.544607'
'2017-05-24 06:00:15.959202' '2016-12-07 09:01:04.729686'
'2019-05-29 11:16:44.130063' '2017-01-19 16:06:37.625964'
'2018-04-07 19:42:43.708620' '2016-06-28 03:13:58.266977'
'2015-03-21 00:03:07.704446']
and now I want to convert those strings into datetime and find the earliest date out of it. I don't have much experience in datetime dataframes so I am not sure how to do it. Any suggestions?
Upvotes: 0
Views: 67
Reputation: 24322
Convert these value to datetime by using to_datetime()
method:
df=pd.to_datetime(df,errors='coerce')
Now find earliest date by using min()
method:
earliest_date=df.min()
OR you can also find earliest date by using nsmallest()
method(This works on Series):
earliest_date=df.nsmallest(1)
Upvotes: 1
Reputation: 9941
You can convert strings to_datetime
, then take min
:
dates = ['2019-10-10 21:59:17.074007', '2015-10-13 00:55:55.544607',
'2017-05-24 06:00:15.959202', '2016-12-07 09:01:04.729686',
'2019-05-29 11:16:44.130063', '2017-01-19 16:06:37.625964',
'2018-04-07 19:42:43.708620', '2016-06-28 03:13:58.266977',
'2015-03-21 00:03:07.704446']
pd.to_datetime(dates).min()
Output:
Timestamp('2015-03-21 00:03:07.704446')
Update
If you want to do it across all columns of the dataframe:
df.apply(pd.to_datetime).min().min()
Upvotes: 2
Reputation: 354
Lets call the list you mentioned l
, you can iterate on it and parse dates using datetime.strptime
, aggregate them in a new list and return the earliest:
from datetime import datetime
parsed_dates = []
for d in l:
parsed_dates.append(datetime.strptime(d, "%Y-%m-%d %H:%M:%S.%f"))
print(min(parsed_dates))
Upvotes: 1