Skip to content

Commit bfd8922

Browse files
authored
REF: tighten types in maybe_downcast_numeric, maybe_downcast_to_dtype (pandas-dev#40087)
1 parent 01c4c51 commit bfd8922

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

pandas/core/dtypes/cast.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
PeriodDtype,
9595
)
9696
from pandas.core.dtypes.generic import (
97-
ABCDataFrame,
9897
ABCExtensionArray,
9998
ABCSeries,
10099
)
@@ -232,19 +231,15 @@ def _disallow_mismatched_datetimelike(value, dtype: DtypeObj):
232231
raise TypeError(f"Cannot cast {repr(value)} to {dtype}")
233232

234233

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:
236237
"""
237238
try to cast to the specified dtype (e.g. convert back to bool/int
238239
or could be an astype of float64->float32
239240
"""
240241
do_round = False
241242

242-
if is_scalar(result):
243-
return result
244-
elif isinstance(result, ABCDataFrame):
245-
# occurs in pivot_table doctest
246-
return result
247-
248243
if isinstance(dtype, str):
249244
if dtype == "infer":
250245
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]):
264259
do_round = True
265260

266261
else:
262+
# TODO: complex? what if result is already non-object?
267263
dtype = "object"
268264

269265
dtype = np.dtype(dtype)
@@ -295,7 +291,9 @@ def maybe_downcast_to_dtype(result, dtype: Union[str, np.dtype]):
295291
return result
296292

297293

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:
299297
"""
300298
Subset of maybe_downcast_to_dtype restricted to numeric dtypes.
301299

0 commit comments

Comments
 (0)