Skip to content

Commit fe9e5d0

Browse files
authored
REGR: .describe on unsigned dtypes results in object (#48473)
1 parent 7b89f3e commit fe9e5d0

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
@@ -1606,6 +1606,45 @@ def any_numpy_dtype(request):
16061606
return request.param
16071607

16081608

1609+
@pytest.fixture(
1610+
params=tm.ALL_REAL_NUMPY_DTYPES
1611+
+ tm.COMPLEX_DTYPES
1612+
+ tm.ALL_INT_EA_DTYPES
1613+
+ tm.FLOAT_EA_DTYPES
1614+
)
1615+
def any_numeric_dtype(request):
1616+
"""
1617+
Parameterized fixture for all numeric dtypes.
1618+
1619+
* int
1620+
* 'int8'
1621+
* 'uint8'
1622+
* 'int16'
1623+
* 'uint16'
1624+
* 'int32'
1625+
* 'uint32'
1626+
* 'int64'
1627+
* 'uint64'
1628+
* float
1629+
* 'float32'
1630+
* 'float64'
1631+
* complex
1632+
* 'complex64'
1633+
* 'complex128'
1634+
* 'UInt8'
1635+
* 'Int8'
1636+
* 'UInt16'
1637+
* 'Int16'
1638+
* 'UInt32'
1639+
* 'Int32'
1640+
* 'UInt64'
1641+
* 'Int64'
1642+
* 'Float32'
1643+
* 'Float64'
1644+
"""
1645+
return request.param
1646+
1647+
16091648
# categoricals are handled separately
16101649
_any_skipna_inferred_dtype = [
16111650
("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)