From 74f89cd90cfc31c1a4c721edce3a19d0366183dd Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Thu, 12 Jan 2023 01:01:56 +0100 Subject: [PATCH 1/2] BUG: Series.quantile emitting RuntimeWarning for all NA case --- doc/source/whatsnew/v1.5.3.rst | 1 + pandas/core/array_algos/quantile.py | 2 ++ pandas/core/arrays/masked.py | 5 +++++ pandas/tests/frame/methods/test_quantile.py | 2 -- pandas/tests/series/methods/test_quantile.py | 15 +++++++++++++++ 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.5.3.rst b/doc/source/whatsnew/v1.5.3.rst index 0cb8796e3fb5d..86f9aa36fcfb8 100644 --- a/doc/source/whatsnew/v1.5.3.rst +++ b/doc/source/whatsnew/v1.5.3.rst @@ -28,6 +28,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ - Bug in :meth:`.Styler.to_excel` leading to error when unrecognized ``border-style`` (e.g. ``"hair"``) provided to Excel writers (:issue:`48649`) +- Bug in :meth:`Series.quantile` emitting warning from NumPy when :class:`Series` has only ``NA`` values (:issue:`50681`) - Bug when chaining several :meth:`.Styler.concat` calls, only the last styler was concatenated (:issue:`49207`) - Fixed bug when instantiating a :class:`DataFrame` subclass inheriting from ``typing.Generic`` that triggered a ``UserWarning`` on python 3.11 (:issue:`49649`) - diff --git a/pandas/core/array_algos/quantile.py b/pandas/core/array_algos/quantile.py index 217fbafce719c..d3d9cb1b29b9a 100644 --- a/pandas/core/array_algos/quantile.py +++ b/pandas/core/array_algos/quantile.py @@ -204,8 +204,10 @@ def _nanpercentile( result = np.array(result, copy=False).T if ( result.dtype != values.dtype + and not mask.all() and (result == result.astype(values.dtype, copy=False)).all() ): + # mask.all() will never get cast back to int # e.g. values id integer dtype and result is floating dtype, # only cast back to integer dtype if result values are all-integer. result = result.astype(values.dtype, copy=False) diff --git a/pandas/core/arrays/masked.py b/pandas/core/arrays/masked.py index ec3fdc6db9dc9..aaa7c706d95bb 100644 --- a/pandas/core/arrays/masked.py +++ b/pandas/core/arrays/masked.py @@ -1039,6 +1039,11 @@ def _quantile( raise NotImplementedError if self.isna().all(): out_mask = np.ones(res.shape, dtype=bool) + + if is_integer_dtype(self.dtype): + # We try to maintain int dtype if possible for not all-na case + # as well + res = np.zeros(res.shape, dtype=self.dtype.numpy_dtype) else: out_mask = np.zeros(res.shape, dtype=bool) else: diff --git a/pandas/tests/frame/methods/test_quantile.py b/pandas/tests/frame/methods/test_quantile.py index 93e1bcc113765..6e3c802aeea01 100644 --- a/pandas/tests/frame/methods/test_quantile.py +++ b/pandas/tests/frame/methods/test_quantile.py @@ -904,8 +904,6 @@ def test_quantile_ea_all_na(self, request, obj, index): expected = index.take([-1, -1, -1], allow_fill=True, fill_value=index._na_value) expected = Series(expected, index=qs, name="A") - if expected.dtype == "Int64": - expected = expected.astype("Float64") expected = type(obj)(expected) tm.assert_equal(result, expected) diff --git a/pandas/tests/series/methods/test_quantile.py b/pandas/tests/series/methods/test_quantile.py index 6326d50b20ab8..0ef1e63f742db 100644 --- a/pandas/tests/series/methods/test_quantile.py +++ b/pandas/tests/series/methods/test_quantile.py @@ -225,3 +225,18 @@ def test_quantile_dtypes(self, dtype): if dtype == "Int64": expected = expected.astype("Float64") tm.assert_series_equal(result, expected) + + def test_quantile_all_na(self, any_int_ea_dtype): + # GH#50681 + ser = Series([pd.NA, pd.NA], dtype=any_int_ea_dtype) + with tm.assert_produces_warning(None): + result = ser.quantile([0.1, 0.5]) + expected = Series([pd.NA, pd.NA], dtype=any_int_ea_dtype, index=[0.1, 0.5]) + tm.assert_series_equal(result, expected) + + def test_quantile_dtype_size(self, any_int_ea_dtype): + # GH#50681 + ser = Series([pd.NA, pd.NA, 1], dtype=any_int_ea_dtype) + result = ser.quantile([0.1, 0.5]) + expected = Series([1, 1], dtype=any_int_ea_dtype, index=[0.1, 0.5]) + tm.assert_series_equal(result, expected) From c327c138f90871465279cc83abee80f812a8b040 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Thu, 12 Jan 2023 09:11:36 +0100 Subject: [PATCH 2/2] Remove xfail --- pandas/tests/frame/methods/test_quantile.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pandas/tests/frame/methods/test_quantile.py b/pandas/tests/frame/methods/test_quantile.py index 6e3c802aeea01..c4b9873449424 100644 --- a/pandas/tests/frame/methods/test_quantile.py +++ b/pandas/tests/frame/methods/test_quantile.py @@ -897,11 +897,6 @@ def test_quantile_ea_all_na(self, request, obj, index): qs = [0.5, 0, 1] result = self.compute_quantile(obj, qs) - if np_version_under1p21 and index.dtype == "timedelta64[ns]": - msg = "failed on Numpy 1.20.3; TypeError: data type 'Int64' not understood" - mark = pytest.mark.xfail(reason=msg, raises=TypeError) - request.node.add_marker(mark) - expected = index.take([-1, -1, -1], allow_fill=True, fill_value=index._na_value) expected = Series(expected, index=qs, name="A") expected = type(obj)(expected)