Skip to content

Commit f94be89

Browse files
committed
BUG: Series.quantile emitting RuntimeWarning for all NA case (pandas-dev#50685)
* BUG: Series.quantile emitting RuntimeWarning for all NA case * Remove xfail
1 parent e0d60ef commit f94be89

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
@@ -29,6 +29,7 @@ Bug fixes
2929
~~~~~~~~~
3030
- Bug in the Copy-on-Write implementation losing track of views when indexing a :class:`DataFrame` with another :class:`DataFrame` (:issue:`50630`)
3131
- Bug in :meth:`.Styler.to_excel` leading to error when unrecognized ``border-style`` (e.g. ``"hair"``) provided to Excel writers (:issue:`48649`)
32+
- Bug in :meth:`Series.quantile` emitting warning from NumPy when :class:`Series` has only ``NA`` values (:issue:`50681`)
3233
- Bug when chaining several :meth:`.Styler.concat` calls, only the last styler was concatenated (:issue:`49207`)
3334
- Fixed bug when instantiating a :class:`DataFrame` subclass inheriting from ``typing.Generic`` that triggered a ``UserWarning`` on python 3.11 (:issue:`49649`)
3435
-

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
@@ -1039,6 +1039,11 @@ def _quantile(
10391039
raise NotImplementedError
10401040
if self.isna().all():
10411041
out_mask = np.ones(res.shape, dtype=bool)
1042+
1043+
if is_integer_dtype(self.dtype):
1044+
# We try to maintain int dtype if possible for not all-na case
1045+
# as well
1046+
res = np.zeros(res.shape, dtype=self.dtype.numpy_dtype)
10421047
else:
10431048
out_mask = np.zeros(res.shape, dtype=bool)
10441049
else:

pandas/tests/frame/methods/test_quantile.py

-7
Original file line numberDiff line numberDiff line change
@@ -897,15 +897,8 @@ def test_quantile_ea_all_na(self, request, obj, index):
897897
qs = [0.5, 0, 1]
898898
result = self.compute_quantile(obj, qs)
899899

900-
if np_version_under1p21 and index.dtype == "timedelta64[ns]":
901-
msg = "failed on Numpy 1.20.3; TypeError: data type 'Int64' not understood"
902-
mark = pytest.mark.xfail(reason=msg, raises=TypeError)
903-
request.node.add_marker(mark)
904-
905900
expected = index.take([-1, -1, -1], allow_fill=True, fill_value=index._na_value)
906901
expected = Series(expected, index=qs, name="A")
907-
if expected.dtype == "Int64":
908-
expected = expected.astype("Float64")
909902
expected = type(obj)(expected)
910903
tm.assert_equal(result, expected)
911904

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)