Skip to content

DEPR: Index(ndarray[object]) inferring numeric dtype #49458

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
Nov 2, 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
11 changes: 2 additions & 9 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
13 changes: 7 additions & 6 deletions pandas/tests/indexes/numeric/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/indexes/ranges/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down