Skip to content

Commit 476ce03

Browse files
Backport PR #50685 on branch 1.5.x (BUG: Series.quantile emitting RuntimeWarning for all NA case) (#50718)
Backport PR #50685: BUG: Series.quantile emitting RuntimeWarning for all NA case Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 1ffb79c commit 476ce03

File tree

5 files changed

+23
-7
lines changed

5 files changed

+23
-7
lines changed

doc/source/whatsnew/v1.5.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Fixed regressions
2727
Bug fixes
2828
~~~~~~~~~
2929
- Bug in :meth:`.Styler.to_excel` leading to error when unrecognized ``border-style`` (e.g. ``"hair"``) provided to Excel writers (:issue:`48649`)
30+
- Bug in :meth:`Series.quantile` emitting warning from NumPy when :class:`Series` has only ``NA`` values (:issue:`50681`)
3031
- Bug when chaining several :meth:`.Styler.concat` calls, only the last styler was concatenated (:issue:`49207`)
3132
- Fixed bug when instantiating a :class:`DataFrame` subclass inheriting from ``typing.Generic`` that triggered a ``UserWarning`` on python 3.11 (:issue:`49649`)
3233
-

pandas/core/array_algos/quantile.py

+2
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,10 @@ def _nanpercentile(
204204
result = np.array(result, copy=False).T
205205
if (
206206
result.dtype != values.dtype
207+
and not mask.all()
207208
and (result == result.astype(values.dtype, copy=False)).all()
208209
):
210+
# mask.all() will never get cast back to int
209211
# e.g. values id integer dtype and result is floating dtype,
210212
# only cast back to integer dtype if result values are all-integer.
211213
result = result.astype(values.dtype, copy=False)

pandas/core/arrays/masked.py

+5
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,11 @@ def _quantile(
10261026
raise NotImplementedError
10271027
elif self.isna().all():
10281028
out_mask = np.ones(res.shape, dtype=bool)
1029+
1030+
if is_integer_dtype(self.dtype):
1031+
# We try to maintain int dtype if possible for not all-na case
1032+
# as well
1033+
res = np.zeros(res.shape, dtype=self.dtype.numpy_dtype)
10291034
else:
10301035
out_mask = np.zeros(res.shape, dtype=bool)
10311036
else:

pandas/tests/frame/methods/test_quantile.py

-7
Original file line numberDiff line numberDiff line change
@@ -929,15 +929,8 @@ def test_quantile_ea_all_na(self, request, obj, index):
929929
qs = [0.5, 0, 1]
930930
result = self.compute_quantile(obj, qs)
931931

932-
if np_version_under1p21 and index.dtype == "timedelta64[ns]":
933-
msg = "failed on Numpy 1.20.3; TypeError: data type 'Int64' not understood"
934-
mark = pytest.mark.xfail(reason=msg, raises=TypeError)
935-
request.node.add_marker(mark)
936-
937932
expected = index.take([-1, -1, -1], allow_fill=True, fill_value=index._na_value)
938933
expected = Series(expected, index=qs, name="A")
939-
if expected.dtype == "Int64":
940-
expected = expected.astype("Float64")
941934
expected = type(obj)(expected)
942935
tm.assert_equal(result, expected)
943936

pandas/tests/series/methods/test_quantile.py

+15
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,18 @@ def test_quantile_dtypes(self, dtype):
225225
if dtype == "Int64":
226226
expected = expected.astype("Float64")
227227
tm.assert_series_equal(result, expected)
228+
229+
def test_quantile_all_na(self, any_int_ea_dtype):
230+
# GH#50681
231+
ser = Series([pd.NA, pd.NA], dtype=any_int_ea_dtype)
232+
with tm.assert_produces_warning(None):
233+
result = ser.quantile([0.1, 0.5])
234+
expected = Series([pd.NA, pd.NA], dtype=any_int_ea_dtype, index=[0.1, 0.5])
235+
tm.assert_series_equal(result, expected)
236+
237+
def test_quantile_dtype_size(self, any_int_ea_dtype):
238+
# GH#50681
239+
ser = Series([pd.NA, pd.NA, 1], dtype=any_int_ea_dtype)
240+
result = ser.quantile([0.1, 0.5])
241+
expected = Series([1, 1], dtype=any_int_ea_dtype, index=[0.1, 0.5])
242+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)