1

I have a dataframe with a "Timestamp" column like this:

df[Timestamp]

0            1.341709
1            1.343688
2            1.344503
3            1.344593
4            1.344700
              ...    
1263453    413.056745
1263454    413.056836
1263455    413.056945
1263456    413.057046
1263457    413.057153
Name: Timestamp, Length: 1263458, dtype: float64

Now i have two variables to define a start and end of an interval like so:

start = 10
end = 15

To select all rows in the Dataframe where the Timestamp lies between "start" and "end" I use a query approach:

df_want = df.query("@start <= Timestamp < @end")

This gives me a Typeerror though

TypeError: '<=' not supported between instances of 'int' and 'type'

Why does this not work, shouldnt Timestamp be of type 'float64'? Why is it just 'type'?

2 Answers 2

2

You need to do the following:

df_want = df[df['Timestamp'].between(start,end)]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, this works. Any idea, why a normal query approach doesnt do it?
I am not familiar with this method in python
@L.argio It somehow confuses your Timestamp column name with <class 'pandas._libs.tslibs.timestamps.Timestamp'>, hence the error. If you change your column name to, say, timestamp, it should work. I'm not sure if there is any escaping mechanism for this in the query.
@MustafaAydın Yes, changing the column name fixes it and query method works. Thank you very much
0

With the variables

df[(df['Timestamp'] >= start) & (df['Timestamp'] <= end)]

With the values harcoded:

df[(df['Timestamp'] >= 15) & (df['Timestamp'] <= 30)]

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.