0

I have tried multiple ways to filter a dataframe by another dataframe's date. I keep ending up with this error:

TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_34196/3383335030.py in <module>
      9 print(forecast_start_date, forecast_end_date)
     10 print(type(forecast_start_date), type(forecast_end_date))
---> 11 df_test[(df_test.index.get_level_values(0) >= forecast_start_date) & 
     12         (df_test.index.get_level_values(0) <= forecast_end_date)]
     13 

/opt/conda/lib/python3.7/site-packages/pandas/core/ops/common.py in new_method(self, other)
     67         other = item_from_zerodim(other)
     68 
---> 69         return method(self, other)
     70 
     71     return new_method

/opt/conda/lib/python3.7/site-packages/pandas/core/arraylike.py in __ge__(self, other)
     50     @unpack_zerodim_and_defer("__ge__")
     51     def __ge__(self, other):
---> 52         return self._cmp_method(other, operator.ge)
     53 
     54     # -------------------------------------------------------------

/opt/conda/lib/python3.7/site-packages/pandas/core/indexes/range.py in _cmp_method(self, other, op)
    877             # Both are immutable so if ._range attr. are equal, shortcut is possible
    878             return super()._cmp_method(self, op)
--> 879         return super()._cmp_method(other, op)
    880 
    881     def _arith_method(self, other, op):

/opt/conda/lib/python3.7/site-packages/pandas/core/indexes/base.py in _cmp_method(self, other, op)
   6059         else:
   6060             with np.errstate(all="ignore"):
-> 6061                 result = ops.comparison_op(self._values, other, op)
   6062 
   6063         return result

/opt/conda/lib/python3.7/site-packages/pandas/core/ops/array_ops.py in comparison_op(left, right, op)
    268     ):
    269         # Call the method on lvalues
--> 270         res_values = op(lvalues, rvalues)
    271 
    272     elif is_scalar(rvalues) and isna(rvalues):

TypeError: '>=' not supported between instances of 'numpy.ndarray' and 'Timestamp'

I have tried solutions from the following threads:

I'm not sure how to filter my multilevel index (with level 0 being the date) and these two dates I want to use dynamically. My code is as follows:

forecast_start_date = (df.index.get_level_values(0).max()+pd.DateOffset(months=1))
forecast_end_date = (df.index.get_level_values(0).max() + pd.DateOffset(months=12))
df_test[(df_test.index.get_level_values(0) >= forecast_start_date) & 
        (df_test.index.get_level_values(0) <= forecast_end_date)]
wjandrea
  • 28,235
  • 9
  • 60
  • 81
JordanBH
  • 47
  • 4
  • It seems like you're comparing 'numpy.ndarray' and 'Timestamp', i.e. different data structures. Is `df_test.index` at level 0 a [DatetimeIndex](https://pandas.pydata.org/docs/reference/api/pandas.DatetimeIndex.html)? – FObersteiner Feb 23 '23 at 17:11
  • Yes. It is a level(0) – JordanBH Feb 24 '23 at 18:44
  • Please make a [mre] including inputs and desired output. For specifics, see [How to make good reproducible pandas examples](/q/20109391/4518341). – wjandrea Feb 26 '23 at 15:42

0 Answers0