Skip to content

Commit

Permalink
Backport PR #48473 on branch 1.5.x (REGR: .describe on unsigned dtype…
Browse files Browse the repository at this point in the history
…s results in object) (#48501)

Backport PR #48473: REGR: .describe on unsigned dtypes results in object

Co-authored-by: Richard Shadrach <45562402+rhshadrach@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and rhshadrach authored Sep 11, 2022
1 parent fca63e9 commit ad087f5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
39 changes: 39 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,45 @@ def any_numpy_dtype(request):
return request.param


@pytest.fixture(
params=tm.ALL_REAL_NUMPY_DTYPES
+ tm.COMPLEX_DTYPES
+ tm.ALL_INT_EA_DTYPES
+ tm.FLOAT_EA_DTYPES
)
def any_numeric_dtype(request):
"""
Parameterized fixture for all numeric dtypes.
* int
* 'int8'
* 'uint8'
* 'int16'
* 'uint16'
* 'int32'
* 'uint32'
* 'int64'
* 'uint64'
* float
* 'float32'
* 'float64'
* complex
* 'complex64'
* 'complex128'
* 'UInt8'
* 'Int8'
* 'UInt16'
* 'Int16'
* 'UInt32'
* 'Int32'
* 'UInt64'
* 'Int64'
* 'Float32'
* 'Float64'
"""
return request.param


# categoricals are handled separately
_any_skipna_inferred_dtype = [
("string", ["a", np.nan, "c"]),
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/describe.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

from pandas.core.dtypes.common import (
is_bool_dtype,
is_complex_dtype,
is_datetime64_any_dtype,
is_numeric_dtype,
is_timedelta64_dtype,
Expand Down Expand Up @@ -240,7 +241,9 @@ def describe_numeric_1d(series: Series, percentiles: Sequence[float]) -> Series:
+ series.quantile(percentiles).tolist()
+ [series.max()]
)
return Series(d, index=stat_index, name=series.name)
# GH#48340 - always return float on non-complex numeric data
dtype = float if is_numeric_dtype(series) and not is_complex_dtype(series) else None
return Series(d, index=stat_index, name=series.name, dtype=dtype)


def describe_categorical_1d(
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/series/methods/test_describe.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import numpy as np

from pandas.core.dtypes.common import is_complex_dtype

from pandas import (
Period,
Series,
Expand Down Expand Up @@ -149,3 +151,23 @@ def test_datetime_is_numeric_includes_datetime(self):
index=["count", "mean", "min", "25%", "50%", "75%", "max"],
)
tm.assert_series_equal(result, expected)

def test_numeric_result_dtype(self, any_numeric_dtype):
# GH#48340 - describe should always return float on non-complex numeric input
ser = Series([0, 1], dtype=any_numeric_dtype)
result = ser.describe()
expected = Series(
[
2.0,
0.5,
ser.std(),
0,
0.25,
0.5,
0.75,
1.0,
],
index=["count", "mean", "std", "min", "25%", "50%", "75%", "max"],
dtype="complex128" if is_complex_dtype(ser) else None,
)
tm.assert_series_equal(result, expected)

0 comments on commit ad087f5

Please sign in to comment.