From 527f196c1c18da11b4fc3bf629fde5a5ede0f5fd Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 15 Feb 2021 18:04:56 -0800 Subject: [PATCH 1/2] BUG: DataFrame.append --- pandas/core/internals/concat.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/pandas/core/internals/concat.py b/pandas/core/internals/concat.py index a2c930f6d9b22..01d8cde3e9af2 100644 --- a/pandas/core/internals/concat.py +++ b/pandas/core/internals/concat.py @@ -291,9 +291,7 @@ def get_reindexed_values(self, empty_dtype: DtypeObj, upcasted_na) -> ArrayLike: if len(values) and values[0] is None: fill_value = None - if is_datetime64tz_dtype(blk_dtype) or is_datetime64tz_dtype( - empty_dtype - ): + if is_datetime64tz_dtype(empty_dtype): # TODO(EA2D): special case unneeded with 2D EAs i8values = np.full(self.shape[1], fill_value.value) return DatetimeArray(i8values, dtype=empty_dtype) @@ -302,9 +300,8 @@ def get_reindexed_values(self, empty_dtype: DtypeObj, upcasted_na) -> ArrayLike: elif is_extension_array_dtype(blk_dtype): pass elif is_extension_array_dtype(empty_dtype): - missing_arr = empty_dtype.construct_array_type()._from_sequence( - [], dtype=empty_dtype - ) + cls = empty_dtype.construct_array_type() + missing_arr = cls._from_sequence([], dtype=empty_dtype) ncols, nrows = self.shape assert ncols == 1, ncols empty_arr = -1 * np.ones((nrows,), dtype=np.intp) From 3706470759ea6247dd96d2c6eba425a61a90e760 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 26 Feb 2021 09:35:36 -0800 Subject: [PATCH 2/2] REF: tighten types in maybe_downcast --- pandas/core/dtypes/cast.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index b30dbe32eec4b..8e5f386ae8d43 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -94,7 +94,6 @@ PeriodDtype, ) from pandas.core.dtypes.generic import ( - ABCDataFrame, ABCExtensionArray, ABCIndex, ABCSeries, @@ -233,19 +232,15 @@ def _disallow_mismatched_datetimelike(value, dtype: DtypeObj): raise TypeError(f"Cannot cast {repr(value)} to {dtype}") -def maybe_downcast_to_dtype(result, dtype: Union[str, np.dtype]): +def maybe_downcast_to_dtype( + result: ArrayLike, dtype: Union[str, np.dtype] +) -> ArrayLike: """ try to cast to the specified dtype (e.g. convert back to bool/int or could be an astype of float64->float32 """ do_round = False - if is_scalar(result): - return result - elif isinstance(result, ABCDataFrame): - # occurs in pivot_table doctest - return result - if isinstance(dtype, str): if dtype == "infer": inferred_type = lib.infer_dtype(ensure_object(result), skipna=False) @@ -265,6 +260,7 @@ def maybe_downcast_to_dtype(result, dtype: Union[str, np.dtype]): do_round = True else: + # TODO: complex? what if result is already non-object? dtype = "object" dtype = np.dtype(dtype) @@ -296,7 +292,9 @@ def maybe_downcast_to_dtype(result, dtype: Union[str, np.dtype]): return result -def maybe_downcast_numeric(result, dtype: DtypeObj, do_round: bool = False): +def maybe_downcast_numeric( + result: ArrayLike, dtype: DtypeObj, do_round: bool = False +) -> ArrayLike: """ Subset of maybe_downcast_to_dtype restricted to numeric dtypes.