Skip to content

Commit 4d6ca9d

Browse files
authored
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
1 parent ce7522b commit 4d6ca9d

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
@@ -8,6 +8,7 @@
88
TYPE_CHECKING,
99
Callable,
1010
)
11+
import warnings
1112

1213
import numpy as np
1314

@@ -171,9 +172,11 @@ def var(
171172
if not values.size or mask.all():
172173
return libmissing.NA
173174

174-
return _reductions(
175-
np.var, values=values, mask=mask, skipna=skipna, axis=axis, ddof=ddof
176-
)
175+
with warnings.catch_warnings():
176+
warnings.simplefilter("ignore", RuntimeWarning)
177+
return _reductions(
178+
np.var, values=values, mask=mask, skipna=skipna, axis=axis, ddof=ddof
179+
)
177180

178181

179182
def std(
@@ -187,6 +190,8 @@ def std(
187190
if not values.size or mask.all():
188191
return libmissing.NA
189192

190-
return _reductions(
191-
np.std, values=values, mask=mask, skipna=skipna, axis=axis, ddof=ddof
192-
)
193+
with warnings.catch_warnings():
194+
warnings.simplefilter("ignore", RuntimeWarning)
195+
return _reductions(
196+
np.std, values=values, mask=mask, skipna=skipna, axis=axis, ddof=ddof
197+
)

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)