Skip to content

Commit e8fa07d

Browse files
committed
Better doc-string and whatsnew
1 parent 8588a49 commit e8fa07d

File tree

2 files changed

+33
-48
lines changed

2 files changed

+33
-48
lines changed

doc/source/whatsnew/v0.24.0.txt

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -239,39 +239,6 @@ For situations where you need an ``ndarray`` of ``Interval`` objects, use
239239
idx.values.astype(object)
240240

241241

242-
.. _whatsnew_0240.api.types.is_scalar:
243-
244-
Support for PEP 3141 numbers
245-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
246-
247-
The `is_scalar` function now returns True when a `Number` or `Fraction` is passed.
248-
249-
Previous Behavior:
250-
251-
.. code-block:: ipython
252-
253-
In [1]: pandas.api.types.is_scalar(fractions.Fraction(1))
254-
Out[1]:
255-
False
256-
257-
In [2]: pandas.api.types.is_scalar(numbers.Number(1))
258-
Out[2]:
259-
False
260-
261-
New Behavior:
262-
263-
.. code-block:: ipython
264-
265-
In [1]: pandas.api.types.is_scalar(fractions.Fraction(1))
266-
Out[1]:
267-
True
268-
269-
In [2]: pandas.api.types.is_scalar(numbers.Number(1))
270-
Out[2]:
271-
True
272-
273-
This mirrors ``numpy.isscalar``, which already supports PEP 3141 and is a requirement for `pandas`.
274-
275242
.. _whatsnew_0240.api.timezone_offset_parsing:
276243

277244
Parsing Datetime Strings with Timezone Offsets
@@ -868,5 +835,5 @@ Other
868835
- :meth:`DataFrame.nlargest` and :meth:`DataFrame.nsmallest` now returns the correct n values when keep != 'all' also when tied on the first columns (:issue:`22752`)
869836
- :meth:`~pandas.io.formats.style.Styler.bar` now also supports tablewise application (in addition to rowwise and columnwise) with ``axis=None`` and setting clipping range with ``vmin`` and ``vmax`` (:issue:`21548` and :issue:`21526`). ``NaN`` values are also handled properly.
870837
- Logical operations ``&, |, ^`` between :class:`Series` and :class:`Index` will no longer raise ``ValueError`` (:issue:`22092`)
871-
- Support PEP 3141 numbers in `pandas.api.types.is_scalar` function
838+
- Checking PEP 3141 numbers in `pandas.api.types.is_scalar` function returns ``True`` (:issue:`22903`)
872839
-

pandas/_libs/lib.pyx

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,37 @@ def memory_usage_of_objects(arr: object[:]) -> int64_t:
107107
def is_scalar(val: object) -> bint:
108108
"""
109109
Return True if given value is scalar.
110+
111+
Parameters
112+
----------
113+
val : numpy array scalar (e.g. np.int64), Python builtin numerics,
114+
Python builtin byte arrays and strings, None,
115+
instances of datetime.datetime, instances of datetime.timedelta,
116+
Period, instances of decimal.Decimal, Interval, DateOffset,
117+
Fraction, Number
118+
The value to be checked.
119+
120+
Returns
121+
-------
122+
True if the given value is scalar, False otherwise.
123+
124+
Examples
125+
--------
126+
>>> dt = pd.datetime.datetime(2018,10,3)
127+
>>> pd.is_scalar(dt)
128+
True
129+
130+
>>> from fractions import Fraction
131+
>>> from numbers import Number
132+
133+
>>> fract = Fraction(3,5)
134+
>>> pd.is_scalar(fract)
135+
True
136+
137+
>>> num = Number(2)
138+
>>> pd.is_scalar(num)
139+
True
110140

111-
This includes:
112-
- numpy array scalar (e.g. np.int64)
113-
- Python builtin numerics
114-
- Python builtin byte arrays and strings
115-
- None
116-
- instances of datetime.datetime
117-
- instances of datetime.timedelta
118-
- Period
119-
- instances of decimal.Decimal
120-
- Interval
121-
- DateOffset
122-
- Fraction
123-
- Number
124141
"""
125142

126143
return (cnp.PyArray_IsAnyScalar(val)
@@ -136,7 +153,8 @@ def is_scalar(val: object) -> bint:
136153
or is_decimal(val)
137154
or is_interval(val)
138155
or util.is_offset_object(val)
139-
or np.isscalar(val))
156+
or isinstance(val,Number)
157+
or ininstance(val,Fraction))
140158

141159
def item_from_zerodim(val: object) -> object:
142160
"""

0 commit comments

Comments
 (0)