Videgain
Videgain

Reputation: 195

Select some rows from a dataframe with date index

I have a DataFrame as the one in the image and I want to select the rows that are in a certain period of time, for example from the begining of 2005 to July 2007.

I have tried what is said in this question as

#Enero 2005-Julio 2007
AntCris=df.loc['2005-01-02 00:00:00':'2007-07-01 00:00:00']

or

AntCris=df.loc['2005-01-02':'2007-07-01 00:00:00']

even

AntCris=Datos.loc[Timestamp('2005-01-02 00:00:00'):Timestamp('2007-07-01 00:00:00')]

But I got the error TypeError: '<' not supported between instances of 'Timestamp' and 'str'.

This error is discussed here, but I can not solve my issue. I have also tried reindexing my df with

A=pd.to_datetime(df.index)
df.set_index(A)

But I got TypeError: 'DatetimeIndex' object is not callable.

I hope someone can bring some light over this issue as I have no idea what I am messing up. enter image description here

Upvotes: 0

Views: 1550

Answers (1)

jezrael
jezrael

Reputation: 862641

It seems there are mixed values - datetimes with strings, if need create DatetimeIndex use:

df.index= pd.to_datetime(df.index)

AntCris=df.loc['2005-01-02':'2007-07-01']

Upvotes: 1

Related Questions