diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index fe40bc42887c4..27c5527536057 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -99,7 +99,6 @@ from pandas import Series from pandas.core.arrays import ExtensionArray from pandas.core.indexes.base import Index - from pandas.core.indexes.datetimes import DatetimeIndex _int8_max = np.iinfo(np.int8).max _int16_max = np.iinfo(np.int16).max @@ -1121,57 +1120,6 @@ def astype_nansafe( return arr.view(dtype) -def maybe_convert_objects( - values: np.ndarray, convert_numeric: bool = True -) -> Union[np.ndarray, "DatetimeIndex"]: - """ - If we have an object dtype array, try to coerce dates and/or numbers. - - Parameters - ---------- - values : ndarray - convert_numeric : bool, default True - - Returns - ------- - ndarray or DatetimeIndex - """ - validate_bool_kwarg(convert_numeric, "convert_numeric") - - orig_values = values - - # convert dates - if is_object_dtype(values.dtype): - values = lib.maybe_convert_objects(values, convert_datetime=True) - - # convert timedeltas - if is_object_dtype(values.dtype): - values = lib.maybe_convert_objects(values, convert_timedelta=True) - - # convert to numeric - if is_object_dtype(values.dtype): - if convert_numeric: - try: - new_values = lib.maybe_convert_numeric( - values, set(), coerce_numeric=True - ) - except (ValueError, TypeError): - pass - else: - # if we are all nans then leave me alone - if not isna(new_values).all(): - values = new_values - - else: - # soft-conversion - values = lib.maybe_convert_objects(values) - - if values is orig_values: - values = values.copy() - - return values - - def soft_convert_objects( values: np.ndarray, datetime: bool = True, diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 244c47cd1f1ea..b9226732d5a69 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -37,7 +37,6 @@ find_common_type, maybe_cast_result, maybe_cast_result_dtype, - maybe_convert_objects, maybe_downcast_numeric, ) from pandas.core.dtypes.common import ( @@ -1867,8 +1866,9 @@ def _recast_datetimelike_result(result: DataFrame) -> DataFrame: # See GH#26285 for n in obj_cols: - converted = maybe_convert_objects( - result.iloc[:, n].values, convert_numeric=False + values = result.iloc[:, n].values + converted = lib.maybe_convert_objects( + values, convert_datetime=True, convert_timedelta=True ) result.iloc[:, n] = converted diff --git a/pandas/tests/dtypes/cast/test_convert_objects.py b/pandas/tests/dtypes/cast/test_convert_objects.py deleted file mode 100644 index a28d554acd312..0000000000000 --- a/pandas/tests/dtypes/cast/test_convert_objects.py +++ /dev/null @@ -1,12 +0,0 @@ -import numpy as np -import pytest - -from pandas.core.dtypes.cast import maybe_convert_objects - - -@pytest.mark.parametrize("data", [[1, 2], ["apply", "banana"]]) -def test_maybe_convert_objects_copy(data): - arr = np.array(data) - out = maybe_convert_objects(arr) - - assert arr is not out