'Selecting panda dataframe between timestamps

I want to select all rows with timestamps between 12 hours before and 3 hours after a specific timestamp.

My code:

yyyy = "2021"
mm = "12"
dd = "01"
mn = "00"
dd = "01"
hh = "00"
akttermin = datetime.datetime(int(yyyy),int(mm),int(dd),int(hh),int(mn),0)
plus3htermin = (akttermin + datetime.timedelta(hours=3))
minus12htermin = (akttermin + datetime.timedelta(hours=-12))
df2 = df.loc[(df['observation-time'] >= minus12htermin) & (dfoutind['observation-time'] <= plus3htermin)]

The error message is:

TypeError: '>=' not supported between instances of 'str' and 'datetime.datetime'

What could be the error in my code?

Any help is appreciated!



Solution 1:[1]

You have to convert observation-time column to datetime:

df['observation-time'] = pd.to_datetime(df['observation-time'])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Corralien