4
4
5
5
from __future__ import annotations
6
6
7
- from contextlib import suppress
8
7
from datetime import (
9
8
date ,
10
9
datetime ,
29
28
NaT ,
30
29
OutOfBoundsDatetime ,
31
30
OutOfBoundsTimedelta ,
32
- Period ,
33
31
Timedelta ,
34
32
Timestamp ,
35
33
conversion ,
87
85
PeriodDtype ,
88
86
)
89
87
from pandas .core .dtypes .generic import (
90
- ABCDataFrame ,
91
88
ABCExtensionArray ,
92
89
ABCSeries ,
93
90
)
@@ -249,9 +246,6 @@ def maybe_downcast_to_dtype(result: ArrayLike, dtype: str | np.dtype) -> ArrayLi
249
246
try to cast to the specified dtype (e.g. convert back to bool/int
250
247
or could be an astype of float64->float32
251
248
"""
252
- if isinstance (result , ABCDataFrame ):
253
- # see test_pivot_table_doctest_case
254
- return result
255
249
do_round = False
256
250
257
251
if isinstance (dtype , str ):
@@ -278,15 +272,9 @@ def maybe_downcast_to_dtype(result: ArrayLike, dtype: str | np.dtype) -> ArrayLi
278
272
279
273
dtype = np .dtype (dtype )
280
274
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
290
278
291
279
converted = maybe_downcast_numeric (result , dtype , do_round )
292
280
if converted is not result :
@@ -295,15 +283,7 @@ def maybe_downcast_to_dtype(result: ArrayLike, dtype: str | np.dtype) -> ArrayLi
295
283
# a datetimelike
296
284
# GH12821, iNaT is cast to float
297
285
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 )
307
287
308
288
return result
309
289
0 commit comments