From 7ce5b390be0568e090b1f01253f23eefbaf14ec7 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Wed, 7 Sep 2022 20:08:16 +0200 Subject: [PATCH 1/2] Backport PR #48380: DOC: Clarify that objects dtype takes precedence in where --- doc/source/whatsnew/v1.5.0.rst | 1 + pandas/core/generic.py | 3 +++ 2 files changed, 4 insertions(+) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index c479c59082464..af0ba1bd75124 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -308,6 +308,7 @@ Other enhancements - Implemented a complex-dtype :class:`Index`, passing a complex-dtype array-like to ``pd.Index`` will now retain complex dtype instead of casting to ``object`` (:issue:`45845`) - :class:`Series` and :class:`DataFrame` with :class:`IntegerDtype` now supports bitwise operations (:issue:`34463`) - Add ``milliseconds`` field support for :class:`.DateOffset` (:issue:`43371`) +- :meth:`DataFrame.where` tries to maintain dtype of :class:`DataFrame` if fill value can be cast without loss of precision (:issue:`45582`) - :meth:`DataFrame.reset_index` now accepts a ``names`` argument which renames the index names (:issue:`6878`) - :func:`concat` now raises when ``levels`` is given but ``keys`` is None (:issue:`46653`) - :func:`concat` now raises when ``levels`` contains duplicate values (:issue:`46653`) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 7b345a58bda88..080ee7ce9a4d0 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -9876,6 +9876,9 @@ def where( For further details and examples see the ``{name}`` documentation in :ref:`indexing `. + The dtype of the object takes precedence. The fill value is casted to + the objects dtype, if this can be done losslessly. + Examples -------- >>> s = pd.Series(range(5)) From 3b927f603e34fae20248b00d157d3c7c306991f8 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Wed, 7 Sep 2022 22:43:46 +0200 Subject: [PATCH 2/2] Update generic.py --- pandas/core/generic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 080ee7ce9a4d0..20c5592071e2c 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -9877,7 +9877,7 @@ def where( :ref:`indexing `. The dtype of the object takes precedence. The fill value is casted to - the objects dtype, if this can be done losslessly. + the object's dtype, if this can be done losslessly. Examples --------