Skip to content

Commit 59710bc

Browse files
authored
CLN: remove unnecesary cast.maybe_convert_objects (#38144)
1 parent 1cb5f69 commit 59710bc

File tree

3 files changed

+3
-67
lines changed

3 files changed

+3
-67
lines changed

pandas/core/dtypes/cast.py

-52
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@
9999
from pandas import Series
100100
from pandas.core.arrays import ExtensionArray
101101
from pandas.core.indexes.base import Index
102-
from pandas.core.indexes.datetimes import DatetimeIndex
103102

104103
_int8_max = np.iinfo(np.int8).max
105104
_int16_max = np.iinfo(np.int16).max
@@ -1121,57 +1120,6 @@ def astype_nansafe(
11211120
return arr.view(dtype)
11221121

11231122

1124-
def maybe_convert_objects(
1125-
values: np.ndarray, convert_numeric: bool = True
1126-
) -> Union[np.ndarray, "DatetimeIndex"]:
1127-
"""
1128-
If we have an object dtype array, try to coerce dates and/or numbers.
1129-
1130-
Parameters
1131-
----------
1132-
values : ndarray
1133-
convert_numeric : bool, default True
1134-
1135-
Returns
1136-
-------
1137-
ndarray or DatetimeIndex
1138-
"""
1139-
validate_bool_kwarg(convert_numeric, "convert_numeric")
1140-
1141-
orig_values = values
1142-
1143-
# convert dates
1144-
if is_object_dtype(values.dtype):
1145-
values = lib.maybe_convert_objects(values, convert_datetime=True)
1146-
1147-
# convert timedeltas
1148-
if is_object_dtype(values.dtype):
1149-
values = lib.maybe_convert_objects(values, convert_timedelta=True)
1150-
1151-
# convert to numeric
1152-
if is_object_dtype(values.dtype):
1153-
if convert_numeric:
1154-
try:
1155-
new_values = lib.maybe_convert_numeric(
1156-
values, set(), coerce_numeric=True
1157-
)
1158-
except (ValueError, TypeError):
1159-
pass
1160-
else:
1161-
# if we are all nans then leave me alone
1162-
if not isna(new_values).all():
1163-
values = new_values
1164-
1165-
else:
1166-
# soft-conversion
1167-
values = lib.maybe_convert_objects(values)
1168-
1169-
if values is orig_values:
1170-
values = values.copy()
1171-
1172-
return values
1173-
1174-
11751123
def soft_convert_objects(
11761124
values: np.ndarray,
11771125
datetime: bool = True,

pandas/core/groupby/generic.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
find_common_type,
3838
maybe_cast_result,
3939
maybe_cast_result_dtype,
40-
maybe_convert_objects,
4140
maybe_downcast_numeric,
4241
)
4342
from pandas.core.dtypes.common import (
@@ -1867,8 +1866,9 @@ def _recast_datetimelike_result(result: DataFrame) -> DataFrame:
18671866

18681867
# See GH#26285
18691868
for n in obj_cols:
1870-
converted = maybe_convert_objects(
1871-
result.iloc[:, n].values, convert_numeric=False
1869+
values = result.iloc[:, n].values
1870+
converted = lib.maybe_convert_objects(
1871+
values, convert_datetime=True, convert_timedelta=True
18721872
)
18731873

18741874
result.iloc[:, n] = converted

pandas/tests/dtypes/cast/test_convert_objects.py

-12
This file was deleted.

0 commit comments

Comments
 (0)