Skip to content

Commit 446352a

Browse files
jbrockmendelphofl
authored andcommitted
DEPR: Index(ndarray[object]) inferring numeric dtype (pandas-dev#49458)
1 parent 3aaafc8 commit 446352a

File tree

4 files changed

+11
-17
lines changed

4 files changed

+11
-17
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ Removal of prior version deprecations/changes
297297
- Changed the behavior of :func:`to_datetime` with argument "now" with ``utc=False`` to match ``Timestamp("now")`` (:issue:`18705`)
298298
- 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`)
299299
- 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`)
300+
- 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`)
300301
- 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`)
301302
- Changed the behavior of :class:`Series` constructor, it will no longer infer a datetime64 or timedelta64 dtype from string entries (:issue:`41731`)
302303
- 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`)

pandas/core/indexes/base.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -653,9 +653,7 @@ def _with_infer(cls, *args, **kwargs):
653653
Constructor that uses the 1.0.x behavior inferring numeric dtypes
654654
for ndarray[object] inputs.
655655
"""
656-
with warnings.catch_warnings():
657-
warnings.filterwarnings("ignore", ".*the Index constructor", FutureWarning)
658-
result = cls(*args, **kwargs)
656+
result = cls(*args, **kwargs)
659657

660658
if result.dtype == _dtype_obj and not result._is_multi:
661659
# error: Argument 1 to "maybe_convert_objects" has incompatible type
@@ -7208,13 +7206,8 @@ def _maybe_cast_data_without_dtype(
72087206
if not cast_numeric_deprecated:
72097207
# i.e. we started with a list, not an ndarray[object]
72107208
return result
7209+
return subarr
72117210

7212-
warnings.warn(
7213-
"In a future version, the Index constructor will not infer numeric "
7214-
"dtypes when passed object-dtype sequences (matching Series behavior)",
7215-
FutureWarning,
7216-
stacklevel=find_stack_level(),
7217-
)
72187211
result = ensure_wrapped_if_datetimelike(result)
72197212
return result
72207213

pandas/tests/indexes/numeric/test_numeric.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ def test_constructor_coerce(self, mixed_index, float_index):
139139
self.check_coerce(mixed_index, Index([1.5, 2, 3, 4, 5]))
140140
self.check_coerce(float_index, Index(np.arange(5) * 2.5))
141141

142-
with tm.assert_produces_warning(FutureWarning, match="will not infer"):
143-
result = Index(np.array(np.arange(5) * 2.5, dtype=object))
142+
result = Index(np.array(np.arange(5) * 2.5, dtype=object))
143+
assert result.dtype == object # as of 2.0 to match Series
144144
self.check_coerce(float_index, result.astype("float64"))
145145

146146
def test_constructor_explicit(self, mixed_index, float_index):
@@ -479,12 +479,13 @@ def test_constructor_corner(self, dtype):
479479
assert index.values.dtype == index.dtype
480480
if dtype == np.int64:
481481

482-
msg = "will not infer"
483-
with tm.assert_produces_warning(FutureWarning, match=msg):
484-
without_dtype = Index(arr)
482+
without_dtype = Index(arr)
483+
# as of 2.0 we do not infer a dtype when we get an object-dtype
484+
# ndarray of numbers, matching Series behavior
485+
assert without_dtype.dtype == object
485486

486487
exact = True if index_cls is Int64Index else "equiv"
487-
tm.assert_index_equal(index, without_dtype, exact=exact)
488+
tm.assert_index_equal(index, without_dtype.astype(np.int64), exact=exact)
488489

489490
# preventing casting
490491
arr = np.array([1, "2", 3, "4"], dtype=object)

pandas/tests/indexes/ranges/test_constructors.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ def test_constructor_corner(self):
148148
arr = np.array([1, 2, 3, 4], dtype=object)
149149
index = RangeIndex(1, 5)
150150
assert index.values.dtype == np.int64
151-
with tm.assert_produces_warning(FutureWarning, match="will not infer"):
152-
expected = Index(arr).astype("int64")
151+
expected = Index(arr).astype("int64")
153152

154153
tm.assert_index_equal(index, expected, exact="equiv")
155154

0 commit comments

Comments
 (0)