Eddie.Wu
Eddie.Wu

Reputation: 61

Get TypeError: Index must be DatetimeIndex when filtering dataframe

I want to filter the dataframe by a certain datatime period like the following code.

df2 = df2['Dates'].between_time(pandas.to_datetime('5/13/2015 8:41'), pandas.to_datetime('5/13/2015 8:55'))[['Dates','Category']]

but got an error 'TypeError: Index must be DatetimeIndex' This is the dataframe

enter image description here

and I tried

df2 = pandas.read_csv(path2,parse_dates=[0])

like one of the response from other post but still got the same error. Does anyone know what happen here?

Upvotes: 6

Views: 17340

Answers (3)

SimAzz
SimAzz

Reputation: 137

In my example i had a df with different columns called "df_whole", one column had date time in this format: 2022-08-11 23:00:48:007. I loaded it from a Cassandra db so it was already formatted.

These are the steps that I followed:

1) Convert the date time column literally called 'datetime' in my df to a format that pandas likes.

df_whole['datetime'] = pd.to_datetime((df_whole['datetime']))

2) Take that column and give to the df the index

df_whole.index = df_whole['datetime']

3) Now use the filter:

df_whole.between_time('14:00', '21:00')

job done!

Upvotes: 0

ileadall42
ileadall42

Reputation: 651

As what you offer I think the index's is not the DatetimeIndex so you may try :

df2.index=pd.to_datetime(df2.index)

Upvotes: 2

Tiny.D
Tiny.D

Reputation: 6556

If you want to filter df by a certain datetime period, you can try with:

start_date = pandas.to_datetime('5/13/2015 8:41')
end_date = pandas.to_datetime('5/13/2015 8:55')
df2.loc[(df2['Dates'] > start_date) & (df2['Dates'] < end_date)]

Upvotes: 3

Related Questions