Skip to content

Commit ad087f5

Browse files
Backport PR #48473 on branch 1.5.x (REGR: .describe on unsigned dtypes results in object) (#48501)
Backport PR #48473: REGR: .describe on unsigned dtypes results in object Co-authored-by: Richard Shadrach <[email protected]>
1 parent fca63e9 commit ad087f5

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

pandas/conftest.py

+39
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,45 @@ def any_numpy_dtype(request):
16031603
return request.param
16041604

16051605

1606+
@pytest.fixture(
1607+
params=tm.ALL_REAL_NUMPY_DTYPES
1608+
+ tm.COMPLEX_DTYPES
1609+
+ tm.ALL_INT_EA_DTYPES
1610+
+ tm.FLOAT_EA_DTYPES
1611+
)
1612+
def any_numeric_dtype(request):
1613+
"""
1614+
Parameterized fixture for all numeric dtypes.
1615+
1616+
* int
1617+
* 'int8'
1618+
* 'uint8'
1619+
* 'int16'
1620+
* 'uint16'
1621+
* 'int32'
1622+
* 'uint32'
1623+
* 'int64'
1624+
* 'uint64'
1625+
* float
1626+
* 'float32'
1627+
* 'float64'
1628+
* complex
1629+
* 'complex64'
1630+
* 'complex128'
1631+
* 'UInt8'
1632+
* 'Int8'
1633+
* 'UInt16'
1634+
* 'Int16'
1635+
* 'UInt32'
1636+
* 'Int32'
1637+
* 'UInt64'
1638+
* 'Int64'
1639+
* 'Float32'
1640+
* 'Float64'
1641+
"""
1642+
return request.param
1643+
1644+
16061645
# categoricals are handled separately
16071646
_any_skipna_inferred_dtype = [
16081647
("string", ["a", np.nan, "c"]),

pandas/core/describe.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
from pandas.core.dtypes.common import (
3434
is_bool_dtype,
35+
is_complex_dtype,
3536
is_datetime64_any_dtype,
3637
is_numeric_dtype,
3738
is_timedelta64_dtype,
@@ -240,7 +241,9 @@ def describe_numeric_1d(series: Series, percentiles: Sequence[float]) -> Series:
240241
+ series.quantile(percentiles).tolist()
241242
+ [series.max()]
242243
)
243-
return Series(d, index=stat_index, name=series.name)
244+
# GH#48340 - always return float on non-complex numeric data
245+
dtype = float if is_numeric_dtype(series) and not is_complex_dtype(series) else None
246+
return Series(d, index=stat_index, name=series.name, dtype=dtype)
244247

245248

246249
def describe_categorical_1d(

pandas/tests/series/methods/test_describe.py

+22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import numpy as np
22

3+
from pandas.core.dtypes.common import is_complex_dtype
4+
35
from pandas import (
46
Period,
57
Series,
@@ -149,3 +151,23 @@ def test_datetime_is_numeric_includes_datetime(self):
149151
index=["count", "mean", "min", "25%", "50%", "75%", "max"],
150152
)
151153
tm.assert_series_equal(result, expected)
154+
155+
def test_numeric_result_dtype(self, any_numeric_dtype):
156+
# GH#48340 - describe should always return float on non-complex numeric input
157+
ser = Series([0, 1], dtype=any_numeric_dtype)
158+
result = ser.describe()
159+
expected = Series(
160+
[
161+
2.0,
162+
0.5,
163+
ser.std(),
164+
0,
165+
0.25,
166+
0.5,
167+
0.75,
168+
1.0,
169+
],
170+
index=["count", "mean", "std", "min", "25%", "50%", "75%", "max"],
171+
dtype="complex128" if is_complex_dtype(ser) else None,
172+
)
173+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)