Skip to content

Commit 445c391

Browse files
authored
REF: ensure soft_convert_objects returns ArrayLike (#39961)
1 parent ccac772 commit 445c391

File tree

4 files changed

+14
-13
lines changed

4 files changed

+14
-13
lines changed

pandas/_libs/lib.pyx

+9-5
Original file line numberDiff line numberDiff line change
@@ -1986,7 +1986,7 @@ def maybe_convert_numeric(ndarray[object] values, set na_values,
19861986
19871987
Parameters
19881988
----------
1989-
values : ndarray
1989+
values : ndarray[object]
19901990
Array of object elements to convert.
19911991
na_values : set
19921992
Set of values that should be interpreted as NaN.
@@ -2007,7 +2007,8 @@ def maybe_convert_numeric(ndarray[object] values, set na_values,
20072007
20082008
Returns
20092009
-------
2010-
Array of converted object values to numerical ones.
2010+
np.ndarray
2011+
Array of converted object values to numerical ones.
20112012
"""
20122013
if len(values) == 0:
20132014
return np.array([], dtype='i8')
@@ -2159,7 +2160,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=False,
21592160
21602161
Parameters
21612162
----------
2162-
values : ndarray
2163+
values : ndarray[object]
21632164
Array of object elements to convert.
21642165
try_float : bool, default False
21652166
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,
21792180
21802181
Returns
21812182
-------
2182-
Array of converted object values to more specific dtypes if applicable.
2183+
np.ndarray or ExtensionArray
21832184
"""
21842185
cdef:
21852186
Py_ssize_t i, n
@@ -2309,7 +2310,10 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=False,
23092310
if seen.datetimetz_:
23102311
if is_datetime_with_singletz_array(objects):
23112312
from pandas import DatetimeIndex
2312-
return DatetimeIndex(objects)
2313+
dti = DatetimeIndex(objects)
2314+
2315+
# unbox to DatetimeArray
2316+
return dti._data
23132317
seen.object_ = True
23142318

23152319
if not seen.object_:

pandas/core/dtypes/cast.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ def soft_convert_objects(
12341234
numeric: bool = True,
12351235
timedelta: bool = True,
12361236
copy: bool = True,
1237-
):
1237+
) -> ArrayLike:
12381238
"""
12391239
Try to coerce datetime, timedelta, and numeric object-dtype columns
12401240
to inferred dtype.
@@ -1249,7 +1249,7 @@ def soft_convert_objects(
12491249
12501250
Returns
12511251
-------
1252-
np.ndarray
1252+
np.ndarray or ExtensionArray
12531253
"""
12541254
validate_bool_kwarg(datetime, "datetime")
12551255
validate_bool_kwarg(numeric, "numeric")

pandas/core/internals/blocks.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ class Block(PandasObject):
128128

129129
__slots__ = ["_mgr_locs", "values", "ndim"]
130130
is_numeric = False
131-
is_float = False
132131
is_bool = False
133132
is_object = False
134133
is_extension = False
@@ -1290,7 +1289,7 @@ def _interpolate(
12901289
data = self.values if inplace else self.values.copy()
12911290

12921291
# only deal with floats
1293-
if not self.is_float:
1292+
if self.dtype.kind != "f":
12941293
if self.dtype.kind not in ["i", "u"]:
12951294
return [self]
12961295
data = data.astype(np.float64)
@@ -1955,7 +1954,6 @@ def is_bool(self):
19551954

19561955
class FloatBlock(NumericBlock):
19571956
__slots__ = ()
1958-
is_float = True
19591957

19601958
def to_native_types(
19611959
self, na_rep="", float_format=None, decimal=".", quoting=None, **kwargs
@@ -2144,7 +2142,6 @@ def to_native_types(self, na_rep="NaT", **kwargs):
21442142

21452143
class DatetimeBlock(DatetimeLikeBlockMixin):
21462144
__slots__ = ()
2147-
is_datetime = True
21482145
fill_value = np.datetime64("NaT", "ns")
21492146
_dtype = fill_value.dtype
21502147
_holder = DatetimeArray

pandas/tests/reshape/concat/test_concat.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_concat_copy(self):
3939
result = concat([df, df2, df3], axis=1, copy=False)
4040

4141
for b in result._mgr.blocks:
42-
if b.is_float:
42+
if b.dtype.kind == "f":
4343
assert b.values.base is df._mgr.blocks[0].values.base
4444
elif b.dtype.kind in ["i", "u"]:
4545
assert b.values.base is df2._mgr.blocks[0].values.base
@@ -50,7 +50,7 @@ def test_concat_copy(self):
5050
df4 = DataFrame(np.random.randn(4, 1))
5151
result = concat([df, df2, df3, df4], axis=1, copy=False)
5252
for b in result._mgr.blocks:
53-
if b.is_float:
53+
if b.dtype.kind == "f":
5454
assert b.values.base is None
5555
elif b.dtype.kind in ["i", "u"]:
5656
assert b.values.base is df2._mgr.blocks[0].values.base

0 commit comments

Comments
 (0)