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)]