Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from
Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,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)