Skip to content

ENH: is_scalar returns True for DateOffset objects #18982

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

Merged
merged 1 commit into from
Dec 29, 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
4 changes: 3 additions & 1 deletion pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ cpdef bint isscalar(object val):
- Period
- instances of decimal.Decimal
- Interval
- DateOffset

"""

Expand All @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions pandas/_libs/src/inference.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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())
Expand Down