Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement is_offsetlike #18823

Merged
merged 4 commits into from
Dec 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from .generic import (ABCCategorical, ABCPeriodIndex,
ABCDatetimeIndex, ABCSeries,
ABCSparseArray, ABCSparseSeries, ABCCategoricalIndex,
ABCIndexClass)
from .inference import is_string_like
ABCIndexClass, ABCDateOffset)
from .inference import is_string_like, is_list_like
from .inference import * # noqa


Expand Down Expand Up @@ -266,6 +266,37 @@ def is_datetimetz(arr):
is_datetime64tz_dtype(arr))


def is_offsetlike(arr_or_obj):
"""
Check if obj or all elements of list-like is DateOffset

Parameters
----------
arr_or_obj : object

Returns
-------
boolean : Whether the object is a DateOffset or listlike of DatetOffsets

Examples
--------
>>> is_offsetlike(pd.DateOffset(days=1))
True
>>> is_offsetlike('offset')
False
>>> is_offsetlike([pd.offsets.Minute(4), pd.offsets.MonthEnd()])
True
>>> is_offsetlike(np.array([pd.DateOffset(months=3), pd.Timestamp.now()]))
False
"""
if isinstance(arr_or_obj, ABCDateOffset):
return True
elif (is_list_like(arr_or_obj) and len(arr_or_obj) and
is_object_dtype(arr_or_obj)):
return all(isinstance(x, ABCDateOffset) for x in arr_or_obj)
return False


def is_period(arr):
"""
Check whether an array-like is a periodical index.
Expand Down
20 changes: 5 additions & 15 deletions pandas/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@
is_object_dtype, is_timedelta64_dtype,
is_datetime64_dtype, is_datetime64tz_dtype,
is_bool_dtype, is_datetimetz,
is_list_like,
is_list_like, is_offsetlike,
is_scalar,
_ensure_object)
from pandas.core.dtypes.cast import maybe_upcast_putmask, find_common_type
from pandas.core.dtypes.generic import (
ABCSeries,
ABCDataFrame,
ABCIndex,
ABCPeriodIndex,
ABCDateOffset)
ABCPeriodIndex)

# -----------------------------------------------------------------------------
# Functions that add arithmetic methods to objects, given arithmetic factory
Expand Down Expand Up @@ -363,7 +362,7 @@ def __init__(self, left, right, name, na_op):
rvalues = self._convert_to_array(right, name=name, other=lvalues)

# left
self.is_offset_lhs = self._is_offset(left)
self.is_offset_lhs = is_offsetlike(left)
self.is_timedelta_lhs = is_timedelta64_dtype(lvalues)
self.is_datetime64_lhs = is_datetime64_dtype(lvalues)
self.is_datetime64tz_lhs = is_datetime64tz_dtype(lvalues)
Expand All @@ -373,7 +372,7 @@ def __init__(self, left, right, name, na_op):
self.is_floating_lhs = left.dtype.kind == 'f'

# right
self.is_offset_rhs = self._is_offset(right)
self.is_offset_rhs = is_offsetlike(right)
self.is_datetime64_rhs = is_datetime64_dtype(rvalues)
self.is_datetime64tz_rhs = is_datetime64tz_dtype(rvalues)
self.is_datetime_rhs = (self.is_datetime64_rhs or
Expand Down Expand Up @@ -515,7 +514,7 @@ def _convert_to_array(self, values, name=None, other=None):
values = np.empty(values.shape, dtype=other.dtype)
values[:] = iNaT
return values
elif self._is_offset(values):
elif is_offsetlike(values):
return values
else:
raise TypeError("incompatible type [{dtype}] for a "
Expand Down Expand Up @@ -618,15 +617,6 @@ def f(x):

return lvalues, rvalues

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


def _align_method_SERIES(left, right, align_asobject=False):
""" align lhs and rhs Series """
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,19 @@ def test_is_complex_dtype():
assert com.is_complex_dtype(np.array([1 + 1j, 5]))


def test_is_offsetlike():
assert com.is_offsetlike(np.array([pd.DateOffset(month=3),
pd.offsets.Nano()]))
assert com.is_offsetlike(pd.offsets.MonthEnd())
assert com.is_offsetlike(pd.Index([pd.DateOffset(second=1)]))

assert not com.is_offsetlike(pd.Timedelta(1))
assert not com.is_offsetlike(np.array([1 + 1j, 5]))

# mixed case
assert not com.is_offsetlike(np.array([pd.DateOffset(), pd.Timestamp(0)]))


@pytest.mark.parametrize('input_param,result', [
(int, np.dtype(int)),
('int32', np.dtype('int32')),
Expand Down