Spooked
Spooked

Reputation: 596

Getting the maximum date in a dataframe Pandas does not return the maximum

I am trying to extract the most recent date from a dataframe pandas.

I have a fairly simple line of code which seems to be the recommended way from many sources online

maxdate = df['DATE'].max()
maxdate
'31/12/2021'

However I know my dataframe contains a record of '11/09/2022'. Is the issue to do with the date format? I don't want to change the date format as it will impact on my other things

Any help appreciated

Upvotes: 1

Views: 122

Answers (1)

ThePyGuy
ThePyGuy

Reputation: 18416

Pass the column values to pd.to_datetime function, then take the index for maximum value, and get the DATE value at that index, this way, you'll get the maximum DATE without having to modify existing column.

df.loc[pd.to_datetime(df['DATE'], dayfirst=True).idxmax(), 'DATE']

Upvotes: 1

Related Questions