Skip to content

Commit a921fb1

Browse files
jbrockmendelpmhatre1
authored andcommitted
DEPR: Index.insert dtype inference (pandas-dev#58059)
1 parent 49898b7 commit a921fb1

File tree

8 files changed

+16
-61
lines changed

8 files changed

+16
-61
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ Removal of prior version deprecations/changes
211211
- Removed "freq" keyword from :class:`PeriodArray` constructor, use "dtype" instead (:issue:`52462`)
212212
- Removed deprecated "method" and "limit" keywords from :meth:`Series.replace` and :meth:`DataFrame.replace` (:issue:`53492`)
213213
- Removed the "closed" and "normalize" keywords in :meth:`DatetimeIndex.__new__` (:issue:`52628`)
214+
- Stopped performing dtype inference with in :meth:`Index.insert` with object-dtype index; this often affects the index/columns that result when setting new entries into an empty :class:`Series` or :class:`DataFrame` (:issue:`51363`)
214215
- Removed the "closed" and "unit" keywords in :meth:`TimedeltaIndex.__new__` (:issue:`52628`, :issue:`55499`)
215216
- All arguments in :meth:`Index.sort_values` are now keyword only (:issue:`56493`)
216217
- All arguments in :meth:`Series.to_dict` are now keyword only (:issue:`56493`)

pandas/core/indexes/base.py

+3-21
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@
2020

2121
import numpy as np
2222

23-
from pandas._config import (
24-
get_option,
25-
using_pyarrow_string_dtype,
26-
)
23+
from pandas._config import get_option
2724

2825
from pandas._libs import (
2926
NaT,
@@ -6591,23 +6588,8 @@ def insert(self, loc: int, item) -> Index:
65916588
loc = loc if loc >= 0 else loc - 1
65926589
new_values[loc] = item
65936590

6594-
out = Index._with_infer(new_values, name=self.name)
6595-
if (
6596-
using_pyarrow_string_dtype()
6597-
and is_string_dtype(out.dtype)
6598-
and new_values.dtype == object
6599-
):
6600-
out = out.astype(new_values.dtype)
6601-
if self.dtype == object and out.dtype != object:
6602-
# GH#51363
6603-
warnings.warn(
6604-
"The behavior of Index.insert with object-dtype is deprecated, "
6605-
"in a future version this will return an object-dtype Index "
6606-
"instead of inferring a non-object dtype. To retain the old "
6607-
"behavior, do `idx.insert(loc, item).infer_objects(copy=False)`",
6608-
FutureWarning,
6609-
stacklevel=find_stack_level(),
6610-
)
6591+
# GH#51363 stopped doing dtype inference here
6592+
out = Index(new_values, dtype=new_values.dtype, name=self.name)
66116593
return out
66126594

66136595
def drop(

pandas/core/indexing.py

+2-17
Original file line numberDiff line numberDiff line change
@@ -1896,15 +1896,7 @@ def _setitem_with_indexer(self, indexer, value, name: str = "iloc") -> None:
18961896
# just replacing the block manager here
18971897
# so the object is the same
18981898
index = self.obj._get_axis(i)
1899-
with warnings.catch_warnings():
1900-
# TODO: re-issue this with setitem-specific message?
1901-
warnings.filterwarnings(
1902-
"ignore",
1903-
"The behavior of Index.insert with object-dtype "
1904-
"is deprecated",
1905-
category=FutureWarning,
1906-
)
1907-
labels = index.insert(len(index), key)
1899+
labels = index.insert(len(index), key)
19081900

19091901
# We are expanding the Series/DataFrame values to match
19101902
# the length of thenew index `labels`. GH#40096 ensure
@@ -2222,14 +2214,7 @@ def _setitem_with_indexer_missing(self, indexer, value):
22222214
# and set inplace
22232215
if self.ndim == 1:
22242216
index = self.obj.index
2225-
with warnings.catch_warnings():
2226-
# TODO: re-issue this with setitem-specific message?
2227-
warnings.filterwarnings(
2228-
"ignore",
2229-
"The behavior of Index.insert with object-dtype is deprecated",
2230-
category=FutureWarning,
2231-
)
2232-
new_index = index.insert(len(index), indexer)
2217+
new_index = index.insert(len(index), indexer)
22332218

22342219
# we have a coerced indexer, e.g. a float
22352220
# that matches in an int64 Index, so

pandas/core/internals/managers.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -1480,14 +1480,7 @@ def insert(self, loc: int, item: Hashable, value: ArrayLike, refs=None) -> None:
14801480
value : np.ndarray or ExtensionArray
14811481
refs : The reference tracking object of the value to set.
14821482
"""
1483-
with warnings.catch_warnings():
1484-
# TODO: re-issue this with setitem-specific message?
1485-
warnings.filterwarnings(
1486-
"ignore",
1487-
"The behavior of Index.insert with object-dtype is deprecated",
1488-
category=FutureWarning,
1489-
)
1490-
new_axis = self.items.insert(loc, item)
1483+
new_axis = self.items.insert(loc, item)
14911484

14921485
if value.ndim == 2:
14931486
value = value.T

pandas/tests/indexes/test_old_base.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -409,19 +409,13 @@ def test_where(self, listlike_box, simple_index):
409409
tm.assert_index_equal(result, expected)
410410

411411
def test_insert_base(self, index):
412+
# GH#51363
412413
trimmed = index[1:4]
413414

414415
if not len(index):
415416
pytest.skip("Not applicable for empty index")
416417

417-
# test 0th element
418-
warn = None
419-
if index.dtype == object and index.inferred_type == "boolean":
420-
# GH#51363
421-
warn = FutureWarning
422-
msg = "The behavior of Index.insert with object-dtype is deprecated"
423-
with tm.assert_produces_warning(warn, match=msg):
424-
result = trimmed.insert(0, index[0])
418+
result = trimmed.insert(0, index[0])
425419
assert index[0:4].equals(result)
426420

427421
@pytest.mark.skipif(

pandas/tests/indexing/test_loc.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2025,7 +2025,7 @@ def test_loc_setitem_incremental_with_dst(self):
20252025
ids=["self", "to_datetime64", "to_pydatetime", "np.datetime64"],
20262026
)
20272027
def test_loc_setitem_datetime_keys_cast(self, conv):
2028-
# GH#9516
2028+
# GH#9516, GH#51363 changed in 3.0 to not cast on Index.insert
20292029
dt1 = Timestamp("20130101 09:00:00")
20302030
dt2 = Timestamp("20130101 10:00:00")
20312031
df = DataFrame()
@@ -2034,7 +2034,7 @@ def test_loc_setitem_datetime_keys_cast(self, conv):
20342034

20352035
expected = DataFrame(
20362036
{"one": [100.0, 200.0]},
2037-
index=[dt1, dt2],
2037+
index=Index([conv(dt1), conv(dt2)], dtype=object),
20382038
columns=Index(["one"], dtype=object),
20392039
)
20402040
tm.assert_frame_equal(df, expected)

pandas/tests/series/indexing/test_set_value.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
import numpy as np
44

55
from pandas import (
6-
DatetimeIndex,
6+
Index,
77
Series,
88
)
99
import pandas._testing as tm
1010

1111

1212
def test_series_set_value():
13-
# GH#1561
13+
# GH#1561, GH#51363 as of 3.0 we do not do inference in Index.insert
1414

1515
dates = [datetime(2001, 1, 1), datetime(2001, 1, 2)]
16-
index = DatetimeIndex(dates)
16+
index = Index(dates, dtype=object)
1717

1818
s = Series(dtype=object)
1919
s._set_value(dates[0], 1.0)

pandas/tests/series/indexing/test_setitem.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -495,11 +495,11 @@ def test_setitem_callable_other(self):
495495

496496
class TestSetitemWithExpansion:
497497
def test_setitem_empty_series(self):
498-
# GH#10193
498+
# GH#10193, GH#51363 changed in 3.0 to not do inference in Index.insert
499499
key = Timestamp("2012-01-01")
500500
series = Series(dtype=object)
501501
series[key] = 47
502-
expected = Series(47, [key])
502+
expected = Series(47, Index([key], dtype=object))
503503
tm.assert_series_equal(series, expected)
504504

505505
def test_setitem_empty_series_datetimeindex_preserves_freq(self):

0 commit comments

Comments
 (0)