Skip to content

REF: remove soft_convert_objects #50031

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 1 commit into from
Dec 3, 2022
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
49 changes: 0 additions & 49 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
IntCastingNaNError,
LossySetitemError,
)
from pandas.util._validators import validate_bool_kwarg

from pandas.core.dtypes.common import (
DT64NS_DTYPE,
Expand Down Expand Up @@ -952,54 +951,6 @@ def coerce_indexer_dtype(indexer, categories) -> np.ndarray:
return ensure_int64(indexer)


def soft_convert_objects(
values: np.ndarray,
*,
datetime: bool = True,
timedelta: bool = True,
period: bool = True,
copy: bool = True,
) -> ArrayLike:
"""
Try to coerce datetime, timedelta, and numeric object-dtype columns
to inferred dtype.

Parameters
----------
values : np.ndarray[object]
datetime : bool, default True
timedelta : bool, default True
period : bool, default True
copy : bool, default True

Returns
-------
np.ndarray or ExtensionArray
"""
validate_bool_kwarg(datetime, "datetime")
validate_bool_kwarg(timedelta, "timedelta")
validate_bool_kwarg(copy, "copy")

conversion_count = sum((datetime, timedelta))
if conversion_count == 0:
raise ValueError("At least one of datetime or timedelta must be True.")

# Soft conversions
if datetime or timedelta or period:
# GH 20380, when datetime is beyond year 2262, hence outside
# bound of nanosecond-resolution 64-bit integers.
converted = lib.maybe_convert_objects(
values,
convert_datetime=datetime,
convert_timedelta=timedelta,
convert_period=period,
)
if converted is not values:
return converted

return values


def convert_dtypes(
input_array: ArrayLike,
convert_string: bool = True,
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6354,9 +6354,8 @@ def infer_objects(self: NDFrameT) -> NDFrameT:
A int64
dtype: object
"""
return self._constructor(
self._mgr.convert(datetime=True, timedelta=True, copy=True)
).__finalize__(self, method="infer_objects")
new_mgr = self._mgr.convert()
return self._constructor(new_mgr).__finalize__(self, method="infer_objects")

@final
def convert_dtypes(
Expand Down
19 changes: 6 additions & 13 deletions pandas/core/internals/array_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from pandas.core.dtypes.cast import (
ensure_dtype_can_hold_na,
infer_dtype_from_scalar,
soft_convert_objects,
)
from pandas.core.dtypes.common import (
ensure_platform_int,
Expand Down Expand Up @@ -375,25 +374,19 @@ def fillna(self: T, value, limit, inplace: bool, downcast) -> T:
def astype(self: T, dtype, copy: bool = False, errors: str = "raise") -> T:
return self.apply(astype_array_safe, dtype=dtype, copy=copy, errors=errors)

def convert(
self: T,
*,
copy: bool = True,
datetime: bool = True,
timedelta: bool = True,
) -> T:
def convert(self: T) -> T:
def _convert(arr):
if is_object_dtype(arr.dtype):
# extract PandasArray for tests that patch PandasArray._typ
arr = np.asarray(arr)
return soft_convert_objects(
return lib.maybe_convert_objects(
arr,
datetime=datetime,
timedelta=timedelta,
copy=copy,
convert_datetime=True,
convert_timedelta=True,
convert_period=True,
)
else:
return arr.copy() if copy else arr
return arr.copy()

return self.apply(_convert)

Expand Down
15 changes: 5 additions & 10 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
find_result_type,
maybe_downcast_to_dtype,
np_can_hold_element,
soft_convert_objects,
)
from pandas.core.dtypes.common import (
ensure_platform_int,
Expand Down Expand Up @@ -429,7 +428,7 @@ def _maybe_downcast(self, blocks: list[Block], downcast=None) -> list[Block]:
# but ATM it breaks too much existing code.
# split and convert the blocks

return extend_blocks([blk.convert(datetime=True) for blk in blocks])
return extend_blocks([blk.convert() for blk in blocks])

if downcast is None:
return blocks
Expand All @@ -451,8 +450,6 @@ def convert(
self,
*,
copy: bool = True,
datetime: bool = True,
timedelta: bool = True,
) -> list[Block]:
"""
attempt to coerce any object types to better types return a copy
Expand Down Expand Up @@ -1967,8 +1964,6 @@ def convert(
self,
*,
copy: bool = True,
datetime: bool = True,
timedelta: bool = True,
) -> list[Block]:
"""
attempt to cast any object types to better types return a copy of
Expand All @@ -1980,11 +1975,11 @@ def convert(
# avoid doing .ravel as that might make a copy
values = values[0]

res_values = soft_convert_objects(
res_values = lib.maybe_convert_objects(
values,
datetime=datetime,
timedelta=timedelta,
copy=copy,
convert_datetime=True,
convert_timedelta=True,
convert_period=True,
)
res_values = ensure_block_shape(res_values, self.ndim)
return [self.make_block(res_values)]
Expand Down
12 changes: 2 additions & 10 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,18 +441,10 @@ def fillna(self: T, value, limit, inplace: bool, downcast) -> T:
def astype(self: T, dtype, copy: bool = False, errors: str = "raise") -> T:
return self.apply("astype", dtype=dtype, copy=copy, errors=errors)

def convert(
self: T,
*,
copy: bool = True,
datetime: bool = True,
timedelta: bool = True,
) -> T:
def convert(self: T) -> T:
return self.apply(
"convert",
copy=copy,
datetime=datetime,
timedelta=timedelta,
copy=True,
)

def replace(self: T, to_replace, value, inplace: bool) -> T:
Expand Down