Skip to content

Commit 64129d1

Browse files
jbrockmendeljreback
authored andcommitted
Create ABCDateOffset (#17165)
1 parent 7bef6d8 commit 64129d1

File tree

6 files changed

+25
-13
lines changed

6 files changed

+25
-13
lines changed

pandas/core/dtypes/generic.py

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ def _check(cls, inst):
5252
ABCCategorical = create_pandas_abc_type("ABCCategorical", "_typ",
5353
("categorical"))
5454
ABCPeriod = create_pandas_abc_type("ABCPeriod", "_typ", ("period", ))
55+
ABCDateOffset = create_pandas_abc_type("ABCDateOffset", "_typ",
56+
("dateoffset",))
5557

5658

5759
class _ABCGeneric(type):

pandas/core/indexes/base.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
from pandas import compat
1414

1515

16-
from pandas.core.dtypes.generic import ABCSeries, ABCMultiIndex, ABCPeriodIndex
16+
from pandas.core.dtypes.generic import (
17+
ABCSeries,
18+
ABCMultiIndex,
19+
ABCPeriodIndex,
20+
ABCDateOffset)
1721
from pandas.core.dtypes.missing import isna, array_equivalent
1822
from pandas.core.dtypes.common import (
1923
_ensure_int64,
@@ -3814,8 +3818,6 @@ def _validate_for_numeric_binop(self, other, op, opstr):
38143818
38153819
internal method called by ops
38163820
"""
3817-
from pandas.tseries.offsets import DateOffset
3818-
38193821
# if we are an inheritor of numeric,
38203822
# but not actually numeric (e.g. DatetimeIndex/PeriodInde)
38213823
if not self._is_numeric_dtype:
@@ -3843,7 +3845,7 @@ def _validate_for_numeric_binop(self, other, op, opstr):
38433845
if other.dtype.kind not in ['f', 'i', 'u']:
38443846
raise TypeError("cannot evaluate a numeric op "
38453847
"with a non-numeric dtype")
3846-
elif isinstance(other, (DateOffset, np.timedelta64,
3848+
elif isinstance(other, (ABCDateOffset, np.timedelta64,
38473849
Timedelta, datetime.timedelta)):
38483850
# higher up to handle
38493851
pass
@@ -3862,12 +3864,10 @@ def _add_numeric_methods_binary(cls):
38623864

38633865
def _make_evaluate_binop(op, opstr, reversed=False, constructor=Index):
38643866
def _evaluate_numeric_binop(self, other):
3865-
3866-
from pandas.tseries.offsets import DateOffset
38673867
other = self._validate_for_numeric_binop(other, op, opstr)
38683868

38693869
# handle time-based others
3870-
if isinstance(other, (DateOffset, np.timedelta64,
3870+
if isinstance(other, (ABCDateOffset, np.timedelta64,
38713871
Timedelta, datetime.timedelta)):
38723872
return self._evaluate_with_timedelta_like(other, op, opstr)
38733873
elif isinstance(other, (Timestamp, np.datetime64)):

pandas/core/ops.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@
3535
is_scalar,
3636
_ensure_object)
3737
from pandas.core.dtypes.cast import maybe_upcast_putmask, find_common_type
38-
from pandas.core.dtypes.generic import ABCSeries, ABCIndex, ABCPeriodIndex
38+
from pandas.core.dtypes.generic import (
39+
ABCSeries,
40+
ABCIndex,
41+
ABCPeriodIndex,
42+
ABCDateOffset)
3943

4044
# -----------------------------------------------------------------------------
4145
# Functions that add arithmetic methods to objects, given arithmetic factory
@@ -605,10 +609,10 @@ def f(x):
605609

606610
def _is_offset(self, arr_or_obj):
607611
""" check if obj or all elements of list-like is DateOffset """
608-
if isinstance(arr_or_obj, pd.DateOffset):
612+
if isinstance(arr_or_obj, ABCDateOffset):
609613
return True
610614
elif is_list_like(arr_or_obj) and len(arr_or_obj):
611-
return all(isinstance(x, pd.DateOffset) for x in arr_or_obj)
615+
return all(isinstance(x, ABCDateOffset) for x in arr_or_obj)
612616
return False
613617

614618

pandas/core/tools/datetimes.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
is_numeric_dtype)
1818
from pandas.core.dtypes.generic import (
1919
ABCIndexClass, ABCSeries,
20-
ABCDataFrame)
20+
ABCDataFrame, ABCDateOffset)
2121
from pandas.core.dtypes.missing import notna
2222
from pandas.core import algorithms
2323

@@ -720,8 +720,7 @@ def parse_time_string(arg, freq=None, dayfirst=None, yearfirst=None):
720720
if not isinstance(arg, compat.string_types):
721721
return arg
722722

723-
from pandas.tseries.offsets import DateOffset
724-
if isinstance(freq, DateOffset):
723+
if isinstance(freq, ABCDateOffset):
725724
freq = freq.rule_code
726725

727726
if dayfirst is None:

pandas/tests/dtypes/test_generic.py

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ def test_abc_types(self):
4040
assert isinstance(self.categorical, gt.ABCCategorical)
4141
assert isinstance(pd.Period('2012', freq='A-DEC'), gt.ABCPeriod)
4242

43+
assert isinstance(pd.DateOffset(), gt.ABCDateOffset)
44+
assert isinstance(pd.Period('2012', freq='A-DEC').freq,
45+
gt.ABCDateOffset)
46+
assert not isinstance(pd.Period('2012', freq='A-DEC'),
47+
gt.ABCDateOffset)
48+
4349

4450
def test_setattr_warnings():
4551
# GH5904 - Suggestion: Warning for DataFrame colname-methodname clash

pandas/tseries/offsets.py

+1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ def __add__(date):
184184
)
185185
_use_relativedelta = False
186186
_adjust_dst = False
187+
_typ = "dateoffset"
187188

188189
# default for prior pickles
189190
normalize = False

0 commit comments

Comments
 (0)