0

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?

1
  • That looks like a list. What is the output of: type(df) Commented Apr 5, 2021 at 11:20

3 Answers 3

2

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()
Sign up to request clarification or add additional context in comments.

3 Comments

I can't use that because I have many values in the dataframe (I think 100 columns and rows) and to_datetime does not work for a reason
Please see the update, it basically applies to_datetime column-wise, then takes min for each column, and then takes min among column minimums
And if only some columns of your dataframe are dates in string format, then you can, of course, do df[['col1', 'col2']].apply(pd.to_datetime).min().min() (assuming the dates are in col1 and col2
1

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

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.