Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filtering a pandas dataframe for the max date and symbol

I asked a question here and I don't get an error but an empty dataframe with columns. What am I doing wrong ?

My original data frame is below

               Date    Symbol
     49     2018-11-27  0
     50     2018-12-10  0
     51     2018-12-17  0
     52     2018-12-27  XLK
     53     2018-12-27  XLV
     54     2018-12-28  VTV
     55     2019-01-09  0
     56     2019-01-09  0
     57     2019-01-16  0
     58     2019-02-04  0
     59     2019-02-04  0
     61     2019-02-05  SPY
     62     2019-02-05  0
     60     2019-02-05  TLT
     63     2019-02-07  TLT
     64     2019-02-09  0

The statement following statement works but gives me an empty dataframe :

df.loc[(df['Symbol'] == "TLT") & (df['Date'] == df['Date'].max())] 

link unsupported operand type(s) for &: 'str' and 'Timestamp'

like image 814
Paul Avatar asked Sep 08 '25 07:09

Paul


2 Answers

Convert column to datetimes by to_datetime, also loc is not necessare, so should be removed:

df['Date'] = pd.to_datetime(df['Date'])
df[(df['Symbol'] == "TLT") & (df['Date'] == df['Date'].max())] 
like image 111
jezrael Avatar answered Sep 10 '25 03:09

jezrael


Just to add some ideas. You can also use idxmax() or idxmin()

df.loc[df.groupby('Date')['Symbol'].idxmax()]

This will give you all maximum Date of each Symbol categories.

like image 20
dyang Avatar answered Sep 10 '25 02:09

dyang