From 417bb3a1b6ea72de2e457e08339c266f638b6d5a Mon Sep 17 00:00:00 2001 From: Aviroop Sen Date: Fri, 29 Dec 2017 00:43:23 -0800 Subject: [PATCH] ENH: is_scalar returns True for DateOffset objects --- doc/source/whatsnew/v0.23.0.txt | 1 + pandas/_libs/lib.pyx | 4 +++- pandas/_libs/src/inference.pyx | 2 ++ pandas/tests/dtypes/test_inference.py | 5 ++++- 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index 24f3e4433411e..0061a636cafb6 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -144,6 +144,7 @@ Other Enhancements - :class:`Interval` and :class:`IntervalIndex` have gained a ``length`` attribute (:issue:`18789`) - ``Resampler`` objects now have a functioning :attr:`~pandas.core.resample.Resampler.pipe` method. Previously, calls to ``pipe`` were diverted to the ``mean`` method (:issue:`17905`). +- :func:`~pandas.api.types.is_scalar` now returns ``True`` for ``DateOffset`` objects (:issue:`18943`). .. _whatsnew_0230.api_breaking: diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 5a62203f79642..3898f7499e85e 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -112,6 +112,7 @@ cpdef bint isscalar(object val): - Period - instances of decimal.Decimal - Interval + - DateOffset """ @@ -126,7 +127,8 @@ cpdef bint isscalar(object val): or PyTime_Check(val) or util.is_period_object(val) or is_decimal(val) - or is_interval(val)) + or is_interval(val) + or is_offset(val)) def item_from_zerodim(object val): diff --git a/pandas/_libs/src/inference.pyx b/pandas/_libs/src/inference.pyx index 5ed8828a0f122..b74b3a79fd69a 100644 --- a/pandas/_libs/src/inference.pyx +++ b/pandas/_libs/src/inference.pyx @@ -45,6 +45,8 @@ cpdef bint is_period(object val): """ Return a boolean if this is a Period object """ return util.is_period_object(val) +cdef inline bint is_offset(object val): + return getattr(val, '_typ', '_typ') == 'dateoffset' _TYPE_MAP = { 'categorical': 'categorical', diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index e8bdd2a551a34..219d1b2852938 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -18,7 +18,8 @@ from pandas._libs import tslib, lib, missing as libmissing from pandas import (Series, Index, DataFrame, Timedelta, DatetimeIndex, TimedeltaIndex, Timestamp, - Panel, Period, Categorical, isna) + Panel, Period, Categorical, isna, Interval, + DateOffset) from pandas.compat import u, PY2, PY3, StringIO, lrange from pandas.core.dtypes import inference from pandas.core.dtypes.common import ( @@ -1151,6 +1152,8 @@ def test_isscalar_pandas_scalars(self): assert is_scalar(Timestamp('2014-01-01')) assert is_scalar(Timedelta(hours=1)) assert is_scalar(Period('2014-01-01')) + assert is_scalar(Interval(left=0, right=1)) + assert is_scalar(DateOffset(days=1)) def test_lisscalar_pandas_containers(self): assert not is_scalar(Series())