From 3614159063e125654f0585a3f10c7e2bdcf19434 Mon Sep 17 00:00:00 2001 From: debnathshoham Date: Sun, 8 Aug 2021 23:25:19 +0530 Subject: [PATCH 1/6] BUG:Cannot calculate quantiles from Int64Dtype Series when results are floats --- pandas/core/array_algos/quantile.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/core/array_algos/quantile.py b/pandas/core/array_algos/quantile.py index 32c50ed38eba0..30b7ddcc3929a 100644 --- a/pandas/core/array_algos/quantile.py +++ b/pandas/core/array_algos/quantile.py @@ -181,5 +181,8 @@ def _quantile_ea_fallback( assert res.ndim == 2 assert res.shape[0] == 1 res = res[0] - out = type(values)._from_sequence(res, dtype=values.dtype) + try: + out = type(values)._from_sequence(res, dtype=values.dtype) + except TypeError: + out = np.atleast_2d(np.asarray(res)) return out From 0c5a5337b39aa47a0fd062ff752f7161afaca8e0 Mon Sep 17 00:00:00 2001 From: debnathshoham Date: Sun, 8 Aug 2021 23:47:16 +0530 Subject: [PATCH 2/6] added test --- pandas/core/array_algos/quantile.py | 2 +- pandas/tests/series/methods/test_quantile.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/pandas/core/array_algos/quantile.py b/pandas/core/array_algos/quantile.py index 30b7ddcc3929a..8cc420157d8ba 100644 --- a/pandas/core/array_algos/quantile.py +++ b/pandas/core/array_algos/quantile.py @@ -184,5 +184,5 @@ def _quantile_ea_fallback( try: out = type(values)._from_sequence(res, dtype=values.dtype) except TypeError: - out = np.atleast_2d(np.asarray(res)) + out = np.atleast_2d(np.asarray(res, dtype=np.float64)) return out diff --git a/pandas/tests/series/methods/test_quantile.py b/pandas/tests/series/methods/test_quantile.py index 461c81bc3b44f..84bfe8524634b 100644 --- a/pandas/tests/series/methods/test_quantile.py +++ b/pandas/tests/series/methods/test_quantile.py @@ -217,3 +217,9 @@ def test_quantile_empty(self): res = s.quantile([0.5]) exp = Series([pd.NaT], index=[0.5]) tm.assert_series_equal(res, exp) + + @pytest.mark.parametrize("dtype", [int, float, "Int64"]) + def test_quantile_dtypes(self, dtype): + result = Series([1, 2, 3], dtype=dtype).quantile(np.arange(0, 1, 0.25)) + expected = Series(np.arange(1, 3, 0.5), index=np.arange(0, 1, 0.25)) + tm.assert_series_equal(result, expected) From bdafa932181cee9481dd2ad2bd000a04ed2368f5 Mon Sep 17 00:00:00 2001 From: debnathshoham Date: Mon, 9 Aug 2021 21:41:38 +0530 Subject: [PATCH 3/6] added whatsnew --- doc/source/whatsnew/v1.3.2.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.3.2.rst b/doc/source/whatsnew/v1.3.2.rst index e68f5d9d6bc56..ef611589cea2e 100644 --- a/doc/source/whatsnew/v1.3.2.rst +++ b/doc/source/whatsnew/v1.3.2.rst @@ -23,6 +23,7 @@ Fixed regressions - Fixed regression where :meth:`pandas.read_csv` raised a ``ValueError`` when parameters ``names`` and ``prefix`` were both set to None (:issue:`42387`) - Fixed regression in comparisons between :class:`Timestamp` object and ``datetime64`` objects outside the implementation bounds for nanosecond ``datetime64`` (:issue:`42794`) - Fixed regression in :meth:`.Styler.highlight_min` and :meth:`.Styler.highlight_max` where ``pandas.NA`` was not successfully ignored (:issue:`42650`) +- Fixed regression in :meth:`pandas.Series.quantile` with :class:`pandas.Int64Dtype` (:issue:`42626`) .. --------------------------------------------------------------------------- From c1857bf34c10f5ea093957e8281cdc8d85e3109e Mon Sep 17 00:00:00 2001 From: debnathshoham Date: Tue, 10 Aug 2021 12:15:59 +0530 Subject: [PATCH 4/6] made float64 default return --- pandas/core/array_algos/quantile.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pandas/core/array_algos/quantile.py b/pandas/core/array_algos/quantile.py index 8cc420157d8ba..fdfa846172056 100644 --- a/pandas/core/array_algos/quantile.py +++ b/pandas/core/array_algos/quantile.py @@ -181,8 +181,5 @@ def _quantile_ea_fallback( assert res.ndim == 2 assert res.shape[0] == 1 res = res[0] - try: - out = type(values)._from_sequence(res, dtype=values.dtype) - except TypeError: - out = np.atleast_2d(np.asarray(res, dtype=np.float64)) + out = np.atleast_2d(np.asarray(res, dtype=np.float64)) return out From 265a8fabc06da29a42c983de480374f860579aa1 Mon Sep 17 00:00:00 2001 From: debnathshoham Date: Tue, 10 Aug 2021 13:02:14 +0530 Subject: [PATCH 5/6] Revert "made float64 default return" This reverts commit c1857bf34c10f5ea093957e8281cdc8d85e3109e. --- pandas/core/array_algos/quantile.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/core/array_algos/quantile.py b/pandas/core/array_algos/quantile.py index fdfa846172056..8cc420157d8ba 100644 --- a/pandas/core/array_algos/quantile.py +++ b/pandas/core/array_algos/quantile.py @@ -181,5 +181,8 @@ def _quantile_ea_fallback( assert res.ndim == 2 assert res.shape[0] == 1 res = res[0] - out = np.atleast_2d(np.asarray(res, dtype=np.float64)) + try: + out = type(values)._from_sequence(res, dtype=values.dtype) + except TypeError: + out = np.atleast_2d(np.asarray(res, dtype=np.float64)) return out From 106f685952146a9ac7534e9eec575de859a77228 Mon Sep 17 00:00:00 2001 From: debnathshoham Date: Wed, 11 Aug 2021 00:12:40 +0530 Subject: [PATCH 6/6] added comment pointing issue --- pandas/core/array_algos/quantile.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/core/array_algos/quantile.py b/pandas/core/array_algos/quantile.py index 8cc420157d8ba..c5e96f32e261f 100644 --- a/pandas/core/array_algos/quantile.py +++ b/pandas/core/array_algos/quantile.py @@ -184,5 +184,7 @@ def _quantile_ea_fallback( try: out = type(values)._from_sequence(res, dtype=values.dtype) except TypeError: + # GH#42626: not able to safely cast Int64 + # for floating point output out = np.atleast_2d(np.asarray(res, dtype=np.float64)) return out