Skip to content

BUG: Series.quantile emitting RuntimeWarning for all NA case #50685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
-
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/array_algos/quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 0 additions & 7 deletions pandas/tests/frame/methods/test_quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,15 +897,8 @@ 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")
if expected.dtype == "Int64":
expected = expected.astype("Float64")
expected = type(obj)(expected)
tm.assert_equal(result, expected)

Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/series/methods/test_quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)