From d7d40a0ffbc75040a3b57c1ace748b04f422da9d Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 19 Feb 2021 07:50:47 -0800 Subject: [PATCH 1/2] CLN: remove Block.is_float, Block.is_datetime --- pandas/core/internals/blocks.py | 5 +---- pandas/tests/reshape/concat/test_concat.py | 4 ++-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 2eb5be01c932c..4d4c134f71ea6 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -127,7 +127,6 @@ class Block(PandasObject): __slots__ = ["_mgr_locs", "values", "ndim"] is_numeric = False - is_float = False is_bool = False is_object = False is_extension = False @@ -1253,7 +1252,7 @@ def _interpolate( data = self.values if inplace else self.values.copy() # only deal with floats - if not self.is_float: + if self.dtype.kind != "f": if self.dtype.kind not in ["i", "u"]: return [self] data = data.astype(np.float64) @@ -1929,7 +1928,6 @@ def is_bool(self): class FloatBlock(NumericBlock): __slots__ = () - is_float = True def to_native_types( self, na_rep="", float_format=None, decimal=".", quoting=None, **kwargs @@ -2116,7 +2114,6 @@ def to_native_types(self, na_rep="NaT", **kwargs): class DatetimeBlock(DatetimeLikeBlockMixin): __slots__ = () - is_datetime = True fill_value = np.datetime64("NaT", "ns") _dtype = fill_value.dtype _holder = DatetimeArray diff --git a/pandas/tests/reshape/concat/test_concat.py b/pandas/tests/reshape/concat/test_concat.py index 8f18f87f2decc..a125f85efc8d3 100644 --- a/pandas/tests/reshape/concat/test_concat.py +++ b/pandas/tests/reshape/concat/test_concat.py @@ -39,7 +39,7 @@ def test_concat_copy(self): result = concat([df, df2, df3], axis=1, copy=False) for b in result._mgr.blocks: - if b.is_float: + if b.dtype.kind == "f": assert b.values.base is df._mgr.blocks[0].values.base elif b.dtype.kind in ["i", "u"]: assert b.values.base is df2._mgr.blocks[0].values.base @@ -50,7 +50,7 @@ def test_concat_copy(self): df4 = DataFrame(np.random.randn(4, 1)) result = concat([df, df2, df3, df4], axis=1, copy=False) for b in result._mgr.blocks: - if b.is_float: + if b.dtype.kind == "f": assert b.values.base is None elif b.dtype.kind in ["i", "u"]: assert b.values.base is df2._mgr.blocks[0].values.base From 63170cd61d5fbb108493c5d239e82c0b3030a263 Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 21 Feb 2021 14:34:35 -0800 Subject: [PATCH 2/2] REF: ensure soft_convert_objects returns ArrayLike --- pandas/_libs/lib.pyx | 14 +++++++++----- pandas/core/dtypes/cast.py | 4 ++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 9f2c82d760785..dc8b36a3898b7 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -1986,7 +1986,7 @@ def maybe_convert_numeric(ndarray[object] values, set na_values, Parameters ---------- - values : ndarray + values : ndarray[object] Array of object elements to convert. na_values : set Set of values that should be interpreted as NaN. @@ -2007,7 +2007,8 @@ def maybe_convert_numeric(ndarray[object] values, set na_values, Returns ------- - Array of converted object values to numerical ones. + np.ndarray + Array of converted object values to numerical ones. """ if len(values) == 0: return np.array([], dtype='i8') @@ -2159,7 +2160,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=False, Parameters ---------- - values : ndarray + values : ndarray[object] Array of object elements to convert. try_float : bool, default False If an array-like object contains only float or NaN values is @@ -2179,7 +2180,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=False, Returns ------- - Array of converted object values to more specific dtypes if applicable. + np.ndarray or ExtensionArray """ cdef: Py_ssize_t i, n @@ -2309,7 +2310,10 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=False, if seen.datetimetz_: if is_datetime_with_singletz_array(objects): from pandas import DatetimeIndex - return DatetimeIndex(objects) + dti = DatetimeIndex(objects) + + # unbox to DatetimeArray + return dti._data seen.object_ = True if not seen.object_: diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 669bfe08d42b0..b80d985f219d5 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1234,7 +1234,7 @@ def soft_convert_objects( numeric: bool = True, timedelta: bool = True, copy: bool = True, -): +) -> ArrayLike: """ Try to coerce datetime, timedelta, and numeric object-dtype columns to inferred dtype. @@ -1249,7 +1249,7 @@ def soft_convert_objects( Returns ------- - np.ndarray + np.ndarray or ExtensionArray """ validate_bool_kwarg(datetime, "datetime") validate_bool_kwarg(numeric, "numeric")