diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 3e1711edb0f27..9203e98736924 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -238,6 +238,40 @@ For situations where you need an ``ndarray`` of ``Interval`` objects, use np.asarray(idx) idx.values.astype(object) + +.. _whatsnew_0240.api.types.is_scalar: + +Support for PEP 3141 numbers +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The `is_scalar` function now returns True when a `Number` or `Fraction` is passed. + +Previous Behavior: + +.. code-block:: ipython + + In [1]: pandas.api.types.is_scalar(fractions.Fraction(1)) + Out[1]: + False + + In [2]: pandas.api.types.is_scalar(numbers.Number(1)) + Out[2]: + False + +New Behavior: + +.. code-block:: ipython + + In [1]: pandas.api.types.is_scalar(fractions.Fraction(1)) + Out[1]: + True + + In [2]: pandas.api.types.is_scalar(numbers.Number(1)) + Out[2]: + True + +This mirrors ``numpy.isscalar``, which already supports PEP 3141 and is a requirement for `pandas`. + .. _whatsnew_0240.api.timezone_offset_parsing: Parsing Datetime Strings with Timezone Offsets @@ -829,4 +863,5 @@ Other - :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`) - :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. - Logical operations ``&, |, ^`` between :class:`Series` and :class:`Index` will no longer raise ``ValueError`` (:issue:`22092`) -- +- Support PEP 3141 numbers in `pandas.api.types.is_scalar` function +- \ No newline at end of file diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 0b9793a6ef97a..1b84d09ae6a85 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -119,7 +119,8 @@ def is_scalar(val: object) -> bint: - instances of decimal.Decimal - Interval - DateOffset - + - Fraction + - Number """ return (cnp.PyArray_IsAnyScalar(val) @@ -134,8 +135,8 @@ def is_scalar(val: object) -> bint: or util.is_period_object(val) or is_decimal(val) or is_interval(val) - or util.is_offset_object(val)) - + or util.is_offset_object(val) + or np.isscalar(val)) def item_from_zerodim(val: object) -> object: """ diff --git a/pandas/core/series.py b/pandas/core/series.py index 83f80c305c5eb..9bc9777c59d8f 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2266,30 +2266,41 @@ def _binop(self, other, func, level=None, fill_value=None): def combine(self, other, func, fill_value=None): """ + Combine the Series with a `Series` or `Scalar` according to `func`. + Perform elementwise binary operation on two Series using given function with optional fill value when an index is missing from one Series or - the other + the other. Parameters ---------- other : Series or scalar value + The value(s) to be combined with the `Series`. func : function - Function that takes two scalars as inputs and return a scalar + Function that takes two scalars as inputs and return a scalar. fill_value : scalar value + The optional value to assume when an index + is missing from one Series or the other, The default specifies to use the appropriate NaN value for - the underlying dtype of the Series + the underlying dtype of the Series. Returns ------- - result : Series + result : the combined `Series` object Examples -------- >>> s1 = pd.Series([1, 2]) - >>> s2 = pd.Series([0, 3]) + >>> s2 = pd.Series([0, 3, 4]) >>> s1.combine(s2, lambda x1, x2: x1 if x1 < x2 else x2) 0 0 1 2 + 2 4 + dtype: int64 + >>> s1.combine(s2, lambda x1, x2: x1 if x1 > x2 else x2,fill_value=787) + 0 1 + 1 3 + 2 787 dtype: int64 See Also @@ -2333,12 +2344,16 @@ def combine(self, other, func, fill_value=None): def combine_first(self, other): """ - Combine Series values, choosing the calling Series's values - first. Result index will be the union of the two indexes + Combine Series values, choosing the calling Series's values first. + + Notes + ----- + Result index will be the union of the two indexes. Parameters ---------- other : Series + The value(s) to be combined with the `Series`. Returns -------