0

I would like to filter my dataframe so that I get only the rows with the latest 2 days of data (relative to the latest available date from the dataframe). So this code would work:

df[pd.to_datetime(df['date']) <= pd.to_datetime(df['date'].max()) - pd.to_timedelta('2 days')]

But now I would like to achieve this same effect but using the query method. But if I do this:

df.query("@pd.to_datetime(quote_date) <= @pd.to_datetime(quote_date.max()) - @pd.to_timedelta('2 days')")

then I get TypeError: Cannot convert input [2 days 00:00:00] of type <class 'pandas._libs.tslibs.timedeltas.Timedelta'> to Timestamp error.

I can't get this thing to work, and would love to get some feedbacks on what I'm doing wrong.

StatsNoob
  • 360
  • 1
  • 5
  • 15
  • What is it you are specifically trying to do? Giving us code that doesn't work and asking us to fix it without knowing the desired result isn't how I work!!!! – itprorh66 Apr 11 '23 at 23:49
  • My apologies, I should've been more specific in my question. I revised my question to be more specific. – StatsNoob Apr 12 '23 at 07:35
  • Your question needs a minimal reproducible example consisting of sample input, expected output, actual output, and only the relevant code necessary to reproduce the problem. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for best practices related to Pandas questions. – itprorh66 Apr 12 '23 at 19:31

0 Answers0