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`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/array_algos/quantile.py b/pandas/core/array_algos/quantile.py index 32c50ed38eba0..c5e96f32e261f 100644 --- a/pandas/core/array_algos/quantile.py +++ b/pandas/core/array_algos/quantile.py @@ -181,5 +181,10 @@ 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: + # GH#42626: not able to safely cast Int64 + # for floating point output + 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)