Home > Mobile >  Getting the maximum date in a dataframe Pandas does not return the maximum
Getting the maximum date in a dataframe Pandas does not return the maximum

Time:09-18

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

CodePudding user response:

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']
  • Related