Skip to content

Commit

Permalink
Create ABCDateOffset (pandas-dev#17165)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and alanbato committed Nov 10, 2017
1 parent c00293f commit c09265e
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 13 deletions.
2 changes: 2 additions & 0 deletions pandas/core/dtypes/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def _check(cls, inst):
ABCCategorical = create_pandas_abc_type("ABCCategorical", "_typ",
("categorical"))
ABCPeriod = create_pandas_abc_type("ABCPeriod", "_typ", ("period", ))
ABCDateOffset = create_pandas_abc_type("ABCDateOffset", "_typ",
("dateoffset",))


class _ABCGeneric(type):
Expand Down
14 changes: 7 additions & 7 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
from pandas import compat


from pandas.core.dtypes.generic import ABCSeries, ABCMultiIndex, ABCPeriodIndex
from pandas.core.dtypes.generic import (
ABCSeries,
ABCMultiIndex,
ABCPeriodIndex,
ABCDateOffset)
from pandas.core.dtypes.missing import isna, array_equivalent
from pandas.core.dtypes.common import (
_ensure_int64,
Expand Down Expand Up @@ -3814,8 +3818,6 @@ def _validate_for_numeric_binop(self, other, op, opstr):
internal method called by ops
"""
from pandas.tseries.offsets import DateOffset

# if we are an inheritor of numeric,
# but not actually numeric (e.g. DatetimeIndex/PeriodInde)
if not self._is_numeric_dtype:
Expand Down Expand Up @@ -3843,7 +3845,7 @@ def _validate_for_numeric_binop(self, other, op, opstr):
if other.dtype.kind not in ['f', 'i', 'u']:
raise TypeError("cannot evaluate a numeric op "
"with a non-numeric dtype")
elif isinstance(other, (DateOffset, np.timedelta64,
elif isinstance(other, (ABCDateOffset, np.timedelta64,
Timedelta, datetime.timedelta)):
# higher up to handle
pass
Expand All @@ -3862,12 +3864,10 @@ def _add_numeric_methods_binary(cls):

def _make_evaluate_binop(op, opstr, reversed=False, constructor=Index):
def _evaluate_numeric_binop(self, other):

from pandas.tseries.offsets import DateOffset
other = self._validate_for_numeric_binop(other, op, opstr)

# handle time-based others
if isinstance(other, (DateOffset, np.timedelta64,
if isinstance(other, (ABCDateOffset, np.timedelta64,
Timedelta, datetime.timedelta)):
return self._evaluate_with_timedelta_like(other, op, opstr)
elif isinstance(other, (Timestamp, np.datetime64)):
Expand Down
10 changes: 7 additions & 3 deletions pandas/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
is_scalar,
_ensure_object)
from pandas.core.dtypes.cast import maybe_upcast_putmask, find_common_type
from pandas.core.dtypes.generic import ABCSeries, ABCIndex, ABCPeriodIndex
from pandas.core.dtypes.generic import (
ABCSeries,
ABCIndex,
ABCPeriodIndex,
ABCDateOffset)

# -----------------------------------------------------------------------------
# Functions that add arithmetic methods to objects, given arithmetic factory
Expand Down Expand Up @@ -605,10 +609,10 @@ def f(x):

def _is_offset(self, arr_or_obj):
""" check if obj or all elements of list-like is DateOffset """
if isinstance(arr_or_obj, pd.DateOffset):
if isinstance(arr_or_obj, ABCDateOffset):
return True
elif is_list_like(arr_or_obj) and len(arr_or_obj):
return all(isinstance(x, pd.DateOffset) for x in arr_or_obj)
return all(isinstance(x, ABCDateOffset) for x in arr_or_obj)
return False


Expand Down
5 changes: 2 additions & 3 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
is_numeric_dtype)
from pandas.core.dtypes.generic import (
ABCIndexClass, ABCSeries,
ABCDataFrame)
ABCDataFrame, ABCDateOffset)
from pandas.core.dtypes.missing import notna
from pandas.core import algorithms

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

from pandas.tseries.offsets import DateOffset
if isinstance(freq, DateOffset):
if isinstance(freq, ABCDateOffset):
freq = freq.rule_code

if dayfirst is None:
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/dtypes/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ def test_abc_types(self):
assert isinstance(self.categorical, gt.ABCCategorical)
assert isinstance(pd.Period('2012', freq='A-DEC'), gt.ABCPeriod)

assert isinstance(pd.DateOffset(), gt.ABCDateOffset)
assert isinstance(pd.Period('2012', freq='A-DEC').freq,
gt.ABCDateOffset)
assert not isinstance(pd.Period('2012', freq='A-DEC'),
gt.ABCDateOffset)


def test_setattr_warnings():
# GH5904 - Suggestion: Warning for DataFrame colname-methodname clash
Expand Down
1 change: 1 addition & 0 deletions pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ def __add__(date):
)
_use_relativedelta = False
_adjust_dst = False
_typ = "dateoffset"

# default for prior pickles
normalize = False
Expand Down

0 comments on commit c09265e

Please sign in to comment.