Home > Mobile >  Remove date from datatime index in pandas
Remove date from datatime index in pandas

Time:01-12

I want to remove the date from datetime function in pandas and the following code works just fine.

df= pd.read_csv('data.csv')
df['Value']= df.Value.astype(float)
df['Time'] = pd.to_datetime(df['Time']).dt.time
df.set_index('Time',inplace=True)

But after that when I try to select rows based on the time using .loc function it gives me the following error.

df_to_plot = df.loc['09:43:00':'13:54:00']

TypeError: '<' not supported between instances of 'datetime.time' and 'str'

But the same code works fine without .dt.time as follows:

df= pd.read_csv('data.csv')
df['Value']= df.Value.astype(float)
df['Time'] = pd.to_datetime(df['Time'])
df.set_index('Time',inplace=True)
df_to_plot = df.loc['2022-07-28 09:43':'2022-07-28 13:54']

How can I remove date and still select rows based on time? Thank you.

CodePudding user response:

The TypeError arrises because df['Time'] = pd.to_datetime(df['Time']).dt.time turns df['Time'] into a datetime.time object, whereas in your loc statement, '09:43:00':'13:54:00' is a string. Try this:

df['Time'] = pd.to_datetime(df['Time']).dt.time.astype(str)

CodePudding user response:

try using df.index = df.index.time

  • Related