figs_and_nuts
figs_and_nuts

Reputation: 5753

Incompatibility of relativedelta

Python 3.8.6

It is surprising that im not able to find the exact same errors in any google results but, it seems datetime's timedelta and relativedelta from relativedelta module cannot be compared. Here is the code to replicate:

from datetime import timedelta
from dateutil import relativedelta

timedelta(days=1) < relativedelta.relativedelta(days=1)

Output:

TypeError                                 Traceback (most recent call last)
<ipython-input-65-1444c9e4d5b8> in <module>
      2 from dateutil import relativedelta
      3 
----> 4 timedelta(days=1) < relativedelta.relativedelta(days=1)

TypeError: '<' not supported between instances of 'datetime.timedelta' and 'relativedelta'

I am also noticing similar incompatibility between timedelta64[ns] and relativedelta

Code to reproduce

import datetime
from dateutil import relativedelta
r1 = pd.date_range(start = datetime.datetime(2018,1,1),end=datetime.datetime(2018,4,1),periods=4)
r2 = pd.date_range(start = datetime.datetime(2018,2,1),end=datetime.datetime(2018,5,1),periods=4)

(r1-r1) < relativedelta.relativedelta(days=1)

Output:

InvalidComparison                         Traceback (most recent call last)
~/miniconda/envs/nyraml386/lib/python3.8/site-packages/pandas/core/arrays/datetimelike.py in wrapper(self, other)
    115         try:
--> 116             other = _validate_comparison_value(self, other)
    117         except InvalidComparison:

~/miniconda/envs/nyraml386/lib/python3.8/site-packages/pandas/core/arrays/datetimelike.py in _validate_comparison_value(self, other)
     95         elif not is_list_like(other):
---> 96             raise InvalidComparison(other)
     97 

InvalidComparison: relativedelta(days=+1)

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-75-57e9f57d40b9> in <module>
      4 r2 = pd.date_range(start = datetime.datetime(2018,2,1),end=datetime.datetime(2018,5,1),periods=4)
      5 
----> 6 (r1-r1) < relativedelta.relativedelta(days=1)

~/miniconda/envs/nyraml386/lib/python3.8/site-packages/pandas/core/indexes/extension.py in wrapper(self, other)
    127 
    128         op = getattr(self._data, opname)
--> 129         return op(other)
    130 
    131     wrapper.__name__ = opname

~/miniconda/envs/nyraml386/lib/python3.8/site-packages/pandas/core/ops/common.py in new_method(self, other)
     63         other = item_from_zerodim(other)
     64 
---> 65         return method(self, other)
     66 
     67     return new_method

~/miniconda/envs/nyraml386/lib/python3.8/site-packages/pandas/core/arrays/datetimelike.py in wrapper(self, other)
    116             other = _validate_comparison_value(self, other)
    117         except InvalidComparison:
--> 118             return invalid_comparison(self, other, op)
    119 
    120         dtype = getattr(other, "dtype", None)

~/miniconda/envs/nyraml386/lib/python3.8/site-packages/pandas/core/ops/invalid.py in invalid_comparison(left, right, op)
     32     else:
     33         typ = type(right).__name__
---> 34         raise TypeError(f"Invalid comparison between dtype={left.dtype} and {typ}")
     35     return res_values
     36 

TypeError: Invalid comparison between dtype=timedelta64[ns] and relativedelta

Have I misunderstood something very basic? It seems relativedelta is a no go territory. Is it? What is the best way to deal with timedeltas in python, pandas and numpy?

Upvotes: 2

Views: 437

Answers (0)

Related Questions