Skip to content

Commit

Permalink
Backport PR #42936: BUG:Can't calculate quantiles from Int64Dtype Ser…
Browse files Browse the repository at this point in the history
…ies when results are floats (#42974)

Co-authored-by: Shoham Debnath <debnathshoham@gmail.com>
  • Loading branch information
meeseeksmachine and debnathshoham committed Aug 10, 2021
1 parent 3e887a9 commit 8a19457
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.2.rst
Expand Up @@ -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`)

.. ---------------------------------------------------------------------------
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/array_algos/quantile.py
Expand Up @@ -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
6 changes: 6 additions & 0 deletions pandas/tests/series/methods/test_quantile.py
Expand Up @@ -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)

0 comments on commit 8a19457

Please sign in to comment.