Skip to content

Commit 8a19457

Browse files
Backport PR #42936: BUG:Can't calculate quantiles from Int64Dtype Series when results are floats (#42974)
Co-authored-by: Shoham Debnath <[email protected]>
1 parent 3e887a9 commit 8a19457

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

doc/source/whatsnew/v1.3.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Fixed regressions
2323
- Fixed regression where :meth:`pandas.read_csv` raised a ``ValueError`` when parameters ``names`` and ``prefix`` were both set to None (:issue:`42387`)
2424
- Fixed regression in comparisons between :class:`Timestamp` object and ``datetime64`` objects outside the implementation bounds for nanosecond ``datetime64`` (:issue:`42794`)
2525
- Fixed regression in :meth:`.Styler.highlight_min` and :meth:`.Styler.highlight_max` where ``pandas.NA`` was not successfully ignored (:issue:`42650`)
26+
- Fixed regression in :meth:`pandas.Series.quantile` with :class:`pandas.Int64Dtype` (:issue:`42626`)
2627

2728
.. ---------------------------------------------------------------------------
2829

pandas/core/array_algos/quantile.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,10 @@ def _quantile_ea_fallback(
181181
assert res.ndim == 2
182182
assert res.shape[0] == 1
183183
res = res[0]
184-
out = type(values)._from_sequence(res, dtype=values.dtype)
184+
try:
185+
out = type(values)._from_sequence(res, dtype=values.dtype)
186+
except TypeError:
187+
# GH#42626: not able to safely cast Int64
188+
# for floating point output
189+
out = np.atleast_2d(np.asarray(res, dtype=np.float64))
185190
return out

pandas/tests/series/methods/test_quantile.py

+6
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,9 @@ def test_quantile_empty(self):
217217
res = s.quantile([0.5])
218218
exp = Series([pd.NaT], index=[0.5])
219219
tm.assert_series_equal(res, exp)
220+
221+
@pytest.mark.parametrize("dtype", [int, float, "Int64"])
222+
def test_quantile_dtypes(self, dtype):
223+
result = Series([1, 2, 3], dtype=dtype).quantile(np.arange(0, 1, 0.25))
224+
expected = Series(np.arange(1, 3, 0.5), index=np.arange(0, 1, 0.25))
225+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)