0

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.

1
  • 2
    Instead of dt.time you could consider using dt.strftime("%H:%M:%S") to store the time values as strings instead of datetime.time Commented Jan 11, 2023 at 16:47

2 Answers 2

0

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)
Sign up to request clarification or add additional context in comments.

Comments

0

Try using df.index = df.index.time.

2 Comments

that would give an error, fyi
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.