Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas dataframe : TypeError: unorderable types: str() >= datetime.date()

I am reading a .csv into a pandas dataframe (CorpActionsDf). the head of which is:

                       date  factor_value reference             factor
unique_id                                                             
BBG.XAMS.ASML.S  24/04/2015          0.70    Annual       Regular Cash
BBG.XAMS.ASML.S  25/04/2014          0.61    Annual       Regular Cash
BBG.XAMS.ASML.S  26/04/2013          0.53    Annual       Regular Cash
BBG.XAMS.ASML.S  26/11/2012          9.18      None  Return of Capital
BBG.XAMS.ASML.S  27/04/2012          0.46    Annual       Regular Cash

I am then trying to filter the dataframe so I only keep the data between two dates.

startDate=02-01-2008
endDate=20-02-2008

But I get the following error:

TypeError: <class 'datetime.date'> type object 2008-01-02

I have another process that uses the startDate and endDate to filter information with sucess but for some reason this time I can't get the filtering to work. My code is as follows:

def getCorpActionsData(rawStaticDataPath,startDate,endDate):
    pattern = 'CorporateActions'+ '.csv'
    staticPath = rawStaticDataPath
    
    with open(staticPath+pattern,'rt') as f:
      
         CorpActionsDf = pd.read_csv(f,engine='c',header=None,usecols=[0,1,2,3,4],parse_dates=[1], 
                                     dayfirst=True,index_col=[1],names=['unique_id', 'date','factor_value','reference','factor'])       
         print(CorpActionsDf.head())
       
         CorpActionsDf = CorpActionsDf[(CorpActionsDf.index >= startDate) & (CorpActionsDf.index <= endDate)]
    

I set parse_dates equal to column 1 so I'm not sure what I've done wrong.

like image 501
Stacey Avatar asked Mar 06 '26 03:03

Stacey


1 Answers

UPDATE:

i guess your index is of string (object) type - because of that the following condition (CorpActionsDf.index >= startDate) gives you str() >= datetime.date() error message.

what does CorpActionsDf.index.dtype give as an output?

OLD answer:

make sure that your startDate and endDate have correct data type:

startDate=pd.to_datetime('02-01-2008')
endDate=pd.to_datetime('20-02-2008')
like image 154
MaxU - stop WAR against UA Avatar answered Mar 08 '26 16:03

MaxU - stop WAR against UA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!