diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 92cc7b04496bc..3db616e9b8cea 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -293,6 +293,7 @@ Removal of prior version deprecations/changes - Changed the behavior of :func:`to_datetime` with argument "now" with ``utc=False`` to match ``Timestamp("now")`` (:issue:`18705`) - Changed behavior of :meth:`SparseArray.astype` when given a dtype that is not explicitly ``SparseDtype``, cast to the exact requested dtype rather than silently using a ``SparseDtype`` instead (:issue:`34457`) - Changed behavior of :class:`DataFrame` constructor given floating-point ``data`` and an integer ``dtype``, when the data cannot be cast losslessly, the floating point dtype is retained, matching :class:`Series` behavior (:issue:`41170`) +- Changed behavior of :class:`Index` constructor when given a ``np.ndarray`` with object-dtype containing numeric entries; this now retains object dtype rather than inferring a numeric dtype, consistent with :class:`Series` behavior (:issue:`42870`) - Changed behavior of :class:`DataFrame` constructor when passed a ``dtype`` (other than int) that the data cannot be cast to; it now raises instead of silently ignoring the dtype (:issue:`41733`) - Changed the behavior of :class:`Series` constructor, it will no longer infer a datetime64 or timedelta64 dtype from string entries (:issue:`41731`) - Changed behavior of :class:`Timestamp` constructor with a ``np.datetime64`` object and a ``tz`` passed to interpret the input as a wall-time as opposed to a UTC time (:issue:`42288`) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index d8300bb29c274..8829ab0fa6805 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -654,9 +654,7 @@ def _with_infer(cls, *args, **kwargs): Constructor that uses the 1.0.x behavior inferring numeric dtypes for ndarray[object] inputs. """ - with warnings.catch_warnings(): - warnings.filterwarnings("ignore", ".*the Index constructor", FutureWarning) - result = cls(*args, **kwargs) + result = cls(*args, **kwargs) if result.dtype == _dtype_obj and not result._is_multi: # error: Argument 1 to "maybe_convert_objects" has incompatible type @@ -7220,13 +7218,8 @@ def _maybe_cast_data_without_dtype( if not cast_numeric_deprecated: # i.e. we started with a list, not an ndarray[object] return result + return subarr - warnings.warn( - "In a future version, the Index constructor will not infer numeric " - "dtypes when passed object-dtype sequences (matching Series behavior)", - FutureWarning, - stacklevel=find_stack_level(), - ) result = ensure_wrapped_if_datetimelike(result) return result diff --git a/pandas/tests/indexes/numeric/test_numeric.py b/pandas/tests/indexes/numeric/test_numeric.py index dd62ad8b31fae..4a6fc3a42b3ee 100644 --- a/pandas/tests/indexes/numeric/test_numeric.py +++ b/pandas/tests/indexes/numeric/test_numeric.py @@ -139,8 +139,8 @@ def test_constructor_coerce(self, mixed_index, float_index): self.check_coerce(mixed_index, Index([1.5, 2, 3, 4, 5])) self.check_coerce(float_index, Index(np.arange(5) * 2.5)) - with tm.assert_produces_warning(FutureWarning, match="will not infer"): - result = Index(np.array(np.arange(5) * 2.5, dtype=object)) + result = Index(np.array(np.arange(5) * 2.5, dtype=object)) + assert result.dtype == object # as of 2.0 to match Series self.check_coerce(float_index, result.astype("float64")) def test_constructor_explicit(self, mixed_index, float_index): @@ -479,12 +479,13 @@ def test_constructor_corner(self, dtype): assert index.values.dtype == index.dtype if dtype == np.int64: - msg = "will not infer" - with tm.assert_produces_warning(FutureWarning, match=msg): - without_dtype = Index(arr) + without_dtype = Index(arr) + # as of 2.0 we do not infer a dtype when we get an object-dtype + # ndarray of numbers, matching Series behavior + assert without_dtype.dtype == object exact = True if index_cls is Int64Index else "equiv" - tm.assert_index_equal(index, without_dtype, exact=exact) + tm.assert_index_equal(index, without_dtype.astype(np.int64), exact=exact) # preventing casting arr = np.array([1, "2", 3, "4"], dtype=object) diff --git a/pandas/tests/indexes/ranges/test_constructors.py b/pandas/tests/indexes/ranges/test_constructors.py index c4f26220f87d1..74bcaa8529ffc 100644 --- a/pandas/tests/indexes/ranges/test_constructors.py +++ b/pandas/tests/indexes/ranges/test_constructors.py @@ -148,8 +148,7 @@ def test_constructor_corner(self): arr = np.array([1, 2, 3, 4], dtype=object) index = RangeIndex(1, 5) assert index.values.dtype == np.int64 - with tm.assert_produces_warning(FutureWarning, match="will not infer"): - expected = Index(arr).astype("int64") + expected = Index(arr).astype("int64") tm.assert_index_equal(index, expected, exact="equiv")