Skip to content

Commit 68132f3

Browse files
REGR: disallow mean of period column again (#33758)
1 parent 3c8593d commit 68132f3

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

pandas/core/frame.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@
100100
is_list_like,
101101
is_named_tuple,
102102
is_object_dtype,
103-
is_period_dtype,
104103
is_scalar,
105104
is_sequence,
106105
needs_i8_conversion,
@@ -8234,15 +8233,15 @@ def _reduce(
82348233

82358234
dtype_is_dt = np.array(
82368235
[
8237-
is_datetime64_any_dtype(values.dtype) or is_period_dtype(values.dtype)
8236+
is_datetime64_any_dtype(values.dtype)
82388237
for values in self._iter_column_arrays()
82398238
],
82408239
dtype=bool,
82418240
)
82428241
if numeric_only is None and name in ["mean", "median"] and dtype_is_dt.any():
82438242
warnings.warn(
82448243
"DataFrame.mean and DataFrame.median with numeric_only=None "
8245-
"will include datetime64, datetime64tz, and PeriodDtype columns in a "
8244+
"will include datetime64 and datetime64tz columns in a "
82468245
"future version.",
82478246
FutureWarning,
82488247
stacklevel=3,

pandas/core/nanops.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from pandas._config import get_option
99

10-
from pandas._libs import NaT, Period, Timedelta, Timestamp, iNaT, lib
10+
from pandas._libs import NaT, Timedelta, Timestamp, iNaT, lib
1111
from pandas._typing import ArrayLike, Dtype, Scalar
1212
from pandas.compat._optional import import_optional_dependency
1313

@@ -353,14 +353,6 @@ def _wrap_results(result, dtype: Dtype, fill_value=None):
353353
else:
354354
result = result.astype("m8[ns]").view(dtype)
355355

356-
elif isinstance(dtype, PeriodDtype):
357-
if is_float(result) and result.is_integer():
358-
result = int(result)
359-
if is_integer(result):
360-
result = Period._from_ordinal(result, freq=dtype.freq)
361-
else:
362-
raise NotImplementedError(type(result), result)
363-
364356
return result
365357

366358

@@ -516,6 +508,7 @@ def nansum(
516508
return _wrap_results(the_sum, dtype)
517509

518510

511+
@disallow(PeriodDtype)
519512
@bottleneck_switch()
520513
def nanmean(values, axis=None, skipna=True, mask=None):
521514
"""
@@ -547,7 +540,12 @@ def nanmean(values, axis=None, skipna=True, mask=None):
547540
)
548541
dtype_sum = dtype_max
549542
dtype_count = np.float64
550-
if is_integer_dtype(dtype) or needs_i8_conversion(dtype):
543+
# not using needs_i8_conversion because that includes period
544+
if (
545+
is_integer_dtype(dtype)
546+
or is_datetime64_any_dtype(dtype)
547+
or is_timedelta64_dtype(dtype)
548+
):
551549
dtype_sum = np.float64
552550
elif is_float_dtype(dtype):
553551
dtype_sum = dtype

pandas/tests/frame/test_analytics.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -885,16 +885,20 @@ def test_mean_datetimelike_numeric_only_false(self):
885885
"A": np.arange(3),
886886
"B": pd.date_range("2016-01-01", periods=3),
887887
"C": pd.timedelta_range("1D", periods=3),
888-
"D": pd.period_range("2016", periods=3, freq="A"),
889888
}
890889
)
891890

891+
# datetime(tz) and timedelta work
892892
result = df.mean(numeric_only=False)
893-
expected = pd.Series(
894-
{"A": 1, "B": df.loc[1, "B"], "C": df.loc[1, "C"], "D": df.loc[1, "D"]}
895-
)
893+
expected = pd.Series({"A": 1, "B": df.loc[1, "B"], "C": df.loc[1, "C"]})
896894
tm.assert_series_equal(result, expected)
897895

896+
# mean of period is not allowed
897+
df["D"] = pd.period_range("2016", periods=3, freq="A")
898+
899+
with pytest.raises(TypeError, match="reduction operation 'mean' not allowed"):
900+
df.mean(numeric_only=False)
901+
898902
def test_stats_mixed_type(self, float_string_frame):
899903
# don't blow up
900904
float_string_frame.std(1)

0 commit comments

Comments
 (0)