Skip to content

Commit 1bbd77a

Browse files
databasedavjreback
authored andcommitted
ENH: is_scalar returns True for DateOffset objects (#18982)
1 parent dedfce9 commit 1bbd77a

File tree

4 files changed

+10
-2
lines changed

4 files changed

+10
-2
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ Other Enhancements
144144
- :class:`Interval` and :class:`IntervalIndex` have gained a ``length`` attribute (:issue:`18789`)
145145
- ``Resampler`` objects now have a functioning :attr:`~pandas.core.resample.Resampler.pipe` method.
146146
Previously, calls to ``pipe`` were diverted to the ``mean`` method (:issue:`17905`).
147+
- :func:`~pandas.api.types.is_scalar` now returns ``True`` for ``DateOffset`` objects (:issue:`18943`).
147148

148149
.. _whatsnew_0230.api_breaking:
149150

pandas/_libs/lib.pyx

+3-1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ cpdef bint isscalar(object val):
112112
- Period
113113
- instances of decimal.Decimal
114114
- Interval
115+
- DateOffset
115116
116117
"""
117118

@@ -126,7 +127,8 @@ cpdef bint isscalar(object val):
126127
or PyTime_Check(val)
127128
or util.is_period_object(val)
128129
or is_decimal(val)
129-
or is_interval(val))
130+
or is_interval(val)
131+
or is_offset(val))
130132

131133

132134
def item_from_zerodim(object val):

pandas/_libs/src/inference.pyx

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ cpdef bint is_period(object val):
4545
""" Return a boolean if this is a Period object """
4646
return util.is_period_object(val)
4747

48+
cdef inline bint is_offset(object val):
49+
return getattr(val, '_typ', '_typ') == 'dateoffset'
4850

4951
_TYPE_MAP = {
5052
'categorical': 'categorical',

pandas/tests/dtypes/test_inference.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
from pandas._libs import tslib, lib, missing as libmissing
1919
from pandas import (Series, Index, DataFrame, Timedelta,
2020
DatetimeIndex, TimedeltaIndex, Timestamp,
21-
Panel, Period, Categorical, isna)
21+
Panel, Period, Categorical, isna, Interval,
22+
DateOffset)
2223
from pandas.compat import u, PY2, PY3, StringIO, lrange
2324
from pandas.core.dtypes import inference
2425
from pandas.core.dtypes.common import (
@@ -1151,6 +1152,8 @@ def test_isscalar_pandas_scalars(self):
11511152
assert is_scalar(Timestamp('2014-01-01'))
11521153
assert is_scalar(Timedelta(hours=1))
11531154
assert is_scalar(Period('2014-01-01'))
1155+
assert is_scalar(Interval(left=0, right=1))
1156+
assert is_scalar(DateOffset(days=1))
11541157

11551158
def test_lisscalar_pandas_containers(self):
11561159
assert not is_scalar(Series())

0 commit comments

Comments
 (0)