94
94
PeriodDtype ,
95
95
)
96
96
from pandas .core .dtypes .generic import (
97
- ABCDataFrame ,
98
97
ABCExtensionArray ,
99
98
ABCSeries ,
100
99
)
@@ -232,19 +231,15 @@ def _disallow_mismatched_datetimelike(value, dtype: DtypeObj):
232
231
raise TypeError (f"Cannot cast { repr (value )} to { dtype } " )
233
232
234
233
235
- def maybe_downcast_to_dtype (result , dtype : Union [str , np .dtype ]):
234
+ def maybe_downcast_to_dtype (
235
+ result : ArrayLike , dtype : Union [str , np .dtype ]
236
+ ) -> ArrayLike :
236
237
"""
237
238
try to cast to the specified dtype (e.g. convert back to bool/int
238
239
or could be an astype of float64->float32
239
240
"""
240
241
do_round = False
241
242
242
- if is_scalar (result ):
243
- return result
244
- elif isinstance (result , ABCDataFrame ):
245
- # occurs in pivot_table doctest
246
- return result
247
-
248
243
if isinstance (dtype , str ):
249
244
if dtype == "infer" :
250
245
inferred_type = lib .infer_dtype (ensure_object (result ), skipna = False )
@@ -264,6 +259,7 @@ def maybe_downcast_to_dtype(result, dtype: Union[str, np.dtype]):
264
259
do_round = True
265
260
266
261
else :
262
+ # TODO: complex? what if result is already non-object?
267
263
dtype = "object"
268
264
269
265
dtype = np .dtype (dtype )
@@ -295,7 +291,9 @@ def maybe_downcast_to_dtype(result, dtype: Union[str, np.dtype]):
295
291
return result
296
292
297
293
298
- def maybe_downcast_numeric (result , dtype : DtypeObj , do_round : bool = False ):
294
+ def maybe_downcast_numeric (
295
+ result : ArrayLike , dtype : DtypeObj , do_round : bool = False
296
+ ) -> ArrayLike :
299
297
"""
300
298
Subset of maybe_downcast_to_dtype restricted to numeric dtypes.
301
299
0 commit comments