Skip to content

Commit 67294b9

Browse files
authored
Backport PR #52518 on branch 2.0.x (WARN: std and var showing RuntimeWarning for ea dtype with one element (#52539)
WARN: std and var showing RuntimeWarning for ea dtype with one element (#52518) * WARN: std and var showing RuntimeWarning for ea dtype with one element * Filter warnings * Fix test (cherry picked from commit 4d6ca9d)
1 parent 18fda97 commit 67294b9

File tree

5 files changed

+27
-16
lines changed

5 files changed

+27
-16
lines changed

doc/source/whatsnew/v2.0.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ including other versions of pandas.
1414
Fixed regressions
1515
~~~~~~~~~~~~~~~~~
1616
- Fixed regression for subclassed Series when constructing from a dictionary (:issue:`52445`)
17+
- Fixed regression in :meth:`Series.describe` showing ``RuntimeWarning`` for extension dtype :class:`Series` with one element (:issue:`52515`)
1718

1819
.. ---------------------------------------------------------------------------
1920
.. _whatsnew_201.bug_fixes:

pandas/core/array_algos/masked_reductions.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from __future__ import annotations
66

77
from typing import Callable
8+
import warnings
89

910
import numpy as np
1011

@@ -166,9 +167,11 @@ def var(
166167
if not values.size or mask.all():
167168
return libmissing.NA
168169

169-
return _reductions(
170-
np.var, values=values, mask=mask, skipna=skipna, axis=axis, ddof=ddof
171-
)
170+
with warnings.catch_warnings():
171+
warnings.simplefilter("ignore", RuntimeWarning)
172+
return _reductions(
173+
np.var, values=values, mask=mask, skipna=skipna, axis=axis, ddof=ddof
174+
)
172175

173176

174177
def std(
@@ -182,6 +185,8 @@ def std(
182185
if not values.size or mask.all():
183186
return libmissing.NA
184187

185-
return _reductions(
186-
np.std, values=values, mask=mask, skipna=skipna, axis=axis, ddof=ddof
187-
)
188+
with warnings.catch_warnings():
189+
warnings.simplefilter("ignore", RuntimeWarning)
190+
return _reductions(
191+
np.std, values=values, mask=mask, skipna=skipna, axis=axis, ddof=ddof
192+
)

pandas/tests/extension/base/dim2.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
)
1313

1414
import pandas as pd
15-
import pandas._testing as tm
1615
from pandas.core.arrays.integer import INT_STR_TO_DTYPE
1716
from pandas.tests.extension.base.base import BaseExtensionTests
1817

@@ -200,12 +199,7 @@ def test_reductions_2d_axis0(self, data, method):
200199
kwargs["ddof"] = 0
201200

202201
try:
203-
if method in ["mean", "var", "std"] and hasattr(data, "_mask"):
204-
# Empty slices produced by the mask cause RuntimeWarnings by numpy
205-
with tm.assert_produces_warning(RuntimeWarning, check_stacklevel=False):
206-
result = getattr(arr2d, method)(axis=0, **kwargs)
207-
else:
208-
result = getattr(arr2d, method)(axis=0, **kwargs)
202+
result = getattr(arr2d, method)(axis=0, **kwargs)
209203
except Exception as err:
210204
try:
211205
getattr(data, method)()

pandas/tests/frame/methods/test_describe.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,7 @@ def test_ea_with_na(self, any_numeric_ea_dtype):
388388
# GH#48778
389389

390390
df = DataFrame({"a": [1, pd.NA, pd.NA], "b": pd.NA}, dtype=any_numeric_ea_dtype)
391-
# Warning from numpy for taking std of single element
392-
with tm.assert_produces_warning(RuntimeWarning, check_stacklevel=False):
393-
result = df.describe()
391+
result = df.describe()
394392
expected = DataFrame(
395393
{"a": [1.0, 1.0, pd.NA] + [1.0] * 5, "b": [0.0] + [pd.NA] * 7},
396394
index=["count", "mean", "std", "min", "25%", "50%", "75%", "max"],

pandas/tests/series/methods/test_describe.py

+13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
)
1010

1111
from pandas import (
12+
NA,
1213
Period,
1314
Series,
1415
Timedelta,
@@ -187,3 +188,15 @@ def test_numeric_result_dtype(self, any_numeric_dtype):
187188
dtype=dtype,
188189
)
189190
tm.assert_series_equal(result, expected)
191+
192+
def test_describe_one_element_ea(self):
193+
# GH#52515
194+
ser = Series([0.0], dtype="Float64")
195+
with tm.assert_produces_warning(None):
196+
result = ser.describe()
197+
expected = Series(
198+
[1, 0, NA, 0, 0, 0, 0, 0],
199+
dtype="Float64",
200+
index=["count", "mean", "std", "min", "25%", "50%", "75%", "max"],
201+
)
202+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)