Skip to content

REF: ensure soft_convert_objects returns ArrayLike #39961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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_:
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
Expand Down
5 changes: 1 addition & 4 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ class Block(PandasObject):

__slots__ = ["_mgr_locs", "values", "ndim"]
is_numeric = False
is_float = False
is_bool = False
is_object = False
is_extension = False
Expand Down Expand Up @@ -1290,7 +1289,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)
Expand Down Expand Up @@ -1955,7 +1954,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
Expand Down Expand Up @@ -2144,7 +2142,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
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/reshape/concat/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down