Skip to content

Example fill value #22970

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

Closed
wants to merge 5 commits into from
Closed
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
37 changes: 36 additions & 1 deletion doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
-
7 changes: 4 additions & 3 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ def is_scalar(val: object) -> bint:
- instances of decimal.Decimal
- Interval
- DateOffset

- Fraction
- Number
"""

return (cnp.PyArray_IsAnyScalar(val)
Expand All @@ -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:
"""
Expand Down
29 changes: 22 additions & 7 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
-------
Expand Down