Skip to content

Commit c4de2d1

Browse files
jbrockmendelyeshsurya
authored andcommitted
REF: enforce annotation in maybe_downcast_to_dtype (pandas-dev#40982)
1 parent e25b8e4 commit c4de2d1

File tree

2 files changed

+6
-29
lines changed

2 files changed

+6
-29
lines changed

pandas/core/dtypes/cast.py

+4-24
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from __future__ import annotations
66

7-
from contextlib import suppress
87
from datetime import (
98
date,
109
datetime,
@@ -29,7 +28,6 @@
2928
NaT,
3029
OutOfBoundsDatetime,
3130
OutOfBoundsTimedelta,
32-
Period,
3331
Timedelta,
3432
Timestamp,
3533
conversion,
@@ -87,7 +85,6 @@
8785
PeriodDtype,
8886
)
8987
from pandas.core.dtypes.generic import (
90-
ABCDataFrame,
9188
ABCExtensionArray,
9289
ABCSeries,
9390
)
@@ -249,9 +246,6 @@ def maybe_downcast_to_dtype(result: ArrayLike, dtype: str | np.dtype) -> ArrayLi
249246
try to cast to the specified dtype (e.g. convert back to bool/int
250247
or could be an astype of float64->float32
251248
"""
252-
if isinstance(result, ABCDataFrame):
253-
# see test_pivot_table_doctest_case
254-
return result
255249
do_round = False
256250

257251
if isinstance(dtype, str):
@@ -278,15 +272,9 @@ def maybe_downcast_to_dtype(result: ArrayLike, dtype: str | np.dtype) -> ArrayLi
278272

279273
dtype = np.dtype(dtype)
280274

281-
elif dtype.type is Period:
282-
from pandas.core.arrays import PeriodArray
283-
284-
with suppress(TypeError):
285-
# e.g. TypeError: int() argument must be a string, a
286-
# bytes-like object or a number, not 'Period
287-
288-
# error: "dtype[Any]" has no attribute "freq"
289-
return PeriodArray(result, freq=dtype.freq) # type: ignore[attr-defined]
275+
if not isinstance(dtype, np.dtype):
276+
# enforce our signature annotation
277+
raise TypeError(dtype) # pragma: no cover
290278

291279
converted = maybe_downcast_numeric(result, dtype, do_round)
292280
if converted is not result:
@@ -295,15 +283,7 @@ def maybe_downcast_to_dtype(result: ArrayLike, dtype: str | np.dtype) -> ArrayLi
295283
# a datetimelike
296284
# GH12821, iNaT is cast to float
297285
if dtype.kind in ["M", "m"] and result.dtype.kind in ["i", "f"]:
298-
if isinstance(dtype, DatetimeTZDtype):
299-
# convert to datetime and change timezone
300-
i8values = result.astype("i8", copy=False)
301-
cls = dtype.construct_array_type()
302-
# equiv: DatetimeArray(i8values).tz_localize("UTC").tz_convert(dtype.tz)
303-
dt64values = i8values.view("M8[ns]")
304-
result = cls._simple_new(dt64values, dtype=dtype)
305-
else:
306-
result = result.astype(dtype)
286+
result = result.astype(dtype)
307287

308288
return result
309289

pandas/core/reshape/pivot.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ def pivot_table(
6464
dropna=True,
6565
margins_name="All",
6666
observed=False,
67-
sort=True,
6867
) -> DataFrame:
6968
index = _convert_by(index)
7069
columns = _convert_by(columns)
@@ -84,7 +83,6 @@ def pivot_table(
8483
dropna=dropna,
8584
margins_name=margins_name,
8685
observed=observed,
87-
sort=sort,
8886
)
8987
pieces.append(_table)
9088
keys.append(getattr(func, "__name__", func))
@@ -103,7 +101,6 @@ def pivot_table(
103101
dropna,
104102
margins_name,
105103
observed,
106-
sort,
107104
)
108105
return table.__finalize__(data, method="pivot_table")
109106

@@ -119,7 +116,6 @@ def __internal_pivot_table(
119116
dropna: bool,
120117
margins_name: str,
121118
observed: bool,
122-
sort: bool,
123119
) -> DataFrame:
124120
"""
125121
Helper of :func:`pandas.pivot_table` for any non-list ``aggfunc``.
@@ -161,7 +157,7 @@ def __internal_pivot_table(
161157
pass
162158
values = list(values)
163159

164-
grouped = data.groupby(keys, observed=observed, sort=sort)
160+
grouped = data.groupby(keys, observed=observed)
165161
agged = grouped.agg(aggfunc)
166162
if dropna and isinstance(agged, ABCDataFrame) and len(agged.columns):
167163
agged = agged.dropna(how="all")
@@ -184,6 +180,7 @@ def __internal_pivot_table(
184180
# TODO: why does test_pivot_table_doctest_case fail if
185181
# we don't do this apparently-unnecessary setitem?
186182
agged[v] = agged[v]
183+
pass
187184
else:
188185
agged[v] = maybe_downcast_to_dtype(agged[v], data[v].dtype)
189186

0 commit comments

Comments
 (0)