diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 2878204f5ee79..e1beeff3f2005 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3217,7 +3217,8 @@ def _maybe_cache_changed(self, item, value) -> None: """ The object has called back to us saying maybe it has changed. """ - self._mgr.set(item, value) + loc = self._info_axis.get_loc(item) + self._mgr.iset(loc, value) @property def _is_cached(self) -> bool_t: @@ -3594,8 +3595,14 @@ def _iset_item(self, loc: int, value) -> None: self._clear_item_cache() def _set_item(self, key, value) -> None: - self._mgr.set(key, value) - self._clear_item_cache() + try: + loc = self._info_axis.get_loc(key) + except KeyError: + # This item wasn't present, just insert at end + self._mgr.insert(len(self._info_axis), key, value) + return + + NDFrame._iset_item(self, loc, value) def _set_is_copy(self, ref, copy: bool_t = True) -> None: if not copy: diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 80ba9cdb29916..dd950c0276646 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -983,24 +983,6 @@ def idelete(self, indexer): ) self._rebuild_blknos_and_blklocs() - def set(self, item: Label, value): - """ - Set new item in-place. - - Notes - ----- - Does not consolidate. - Adds new Block if not contained in the current items Index. - """ - try: - loc = self.items.get_loc(item) - except KeyError: - # This item wasn't present, just insert at end - self.insert(len(self.items), item, value) - return - - self.iset(loc, value) - def iset(self, loc: Union[int, slice, np.ndarray], value): """ Set new item in-place. Does not consolidate. Adds new Block if not diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index f1d4c865a0ced..b2239c077bd69 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -342,8 +342,8 @@ def test_iget(self): def test_set(self): mgr = create_mgr("a,b,c: int", item_shape=(3,)) - mgr.set("d", np.array(["foo"] * 3)) - mgr.set("b", np.array(["bar"] * 3)) + mgr.insert(len(mgr.items), "d", np.array(["foo"] * 3)) + mgr.iset(1, np.array(["bar"] * 3)) tm.assert_numpy_array_equal(mgr.iget(0).internal_values(), np.array([0] * 3)) tm.assert_numpy_array_equal( mgr.iget(1).internal_values(), np.array(["bar"] * 3, dtype=np.object_) @@ -354,22 +354,22 @@ def test_set(self): ) def test_set_change_dtype(self, mgr): - mgr.set("baz", np.zeros(N, dtype=bool)) + mgr.insert(len(mgr.items), "baz", np.zeros(N, dtype=bool)) - mgr.set("baz", np.repeat("foo", N)) + mgr.iset(mgr.items.get_loc("baz"), np.repeat("foo", N)) idx = mgr.items.get_loc("baz") assert mgr.iget(idx).dtype == np.object_ mgr2 = mgr.consolidate() - mgr2.set("baz", np.repeat("foo", N)) + mgr2.iset(mgr2.items.get_loc("baz"), np.repeat("foo", N)) idx = mgr2.items.get_loc("baz") assert mgr2.iget(idx).dtype == np.object_ - mgr2.set("quux", tm.randn(N).astype(int)) + mgr2.insert(len(mgr2.items), "quux", tm.randn(N).astype(int)) idx = mgr2.items.get_loc("quux") assert mgr2.iget(idx).dtype == np.int_ - mgr2.set("quux", tm.randn(N)) + mgr2.iset(mgr2.items.get_loc("quux"), tm.randn(N)) assert mgr2.iget(idx).dtype == np.float_ def test_copy(self, mgr): @@ -496,9 +496,9 @@ def _compare(old_mgr, new_mgr): # convert mgr = create_mgr("a,b,foo: object; f: i8; g: f8") - mgr.set("a", np.array(["1"] * N, dtype=np.object_)) - mgr.set("b", np.array(["2."] * N, dtype=np.object_)) - mgr.set("foo", np.array(["foo."] * N, dtype=np.object_)) + mgr.iset(0, np.array(["1"] * N, dtype=np.object_)) + mgr.iset(1, np.array(["2."] * N, dtype=np.object_)) + mgr.iset(2, np.array(["foo."] * N, dtype=np.object_)) new_mgr = mgr.convert(numeric=True) assert new_mgr.iget(0).dtype == np.int64 assert new_mgr.iget(1).dtype == np.float64 @@ -509,9 +509,9 @@ def _compare(old_mgr, new_mgr): mgr = create_mgr( "a,b,foo: object; f: i4; bool: bool; dt: datetime; i: i8; g: f8; h: f2" ) - mgr.set("a", np.array(["1"] * N, dtype=np.object_)) - mgr.set("b", np.array(["2."] * N, dtype=np.object_)) - mgr.set("foo", np.array(["foo."] * N, dtype=np.object_)) + mgr.iset(0, np.array(["1"] * N, dtype=np.object_)) + mgr.iset(1, np.array(["2."] * N, dtype=np.object_)) + mgr.iset(2, np.array(["foo."] * N, dtype=np.object_)) new_mgr = mgr.convert(numeric=True) assert new_mgr.iget(0).dtype == np.int64 assert new_mgr.iget(1).dtype == np.float64 @@ -599,11 +599,11 @@ def test_interleave_dtype(self, mgr_string, dtype): assert mgr.as_array().dtype == "object" def test_consolidate_ordering_issues(self, mgr): - mgr.set("f", tm.randn(N)) - mgr.set("d", tm.randn(N)) - mgr.set("b", tm.randn(N)) - mgr.set("g", tm.randn(N)) - mgr.set("h", tm.randn(N)) + mgr.iset(mgr.items.get_loc("f"), tm.randn(N)) + mgr.iset(mgr.items.get_loc("d"), tm.randn(N)) + mgr.iset(mgr.items.get_loc("b"), tm.randn(N)) + mgr.iset(mgr.items.get_loc("g"), tm.randn(N)) + mgr.iset(mgr.items.get_loc("h"), tm.randn(N)) # we have datetime/tz blocks in mgr cons = mgr.consolidate() @@ -641,7 +641,7 @@ def test_get_numeric_data(self): "str: object; bool: bool; obj: object; dt: datetime", item_shape=(3,), ) - mgr.set("obj", np.array([1, 2, 3], dtype=np.object_)) + mgr.iset(5, np.array([1, 2, 3], dtype=np.object_)) numeric = mgr.get_numeric_data() tm.assert_index_equal( @@ -653,7 +653,7 @@ def test_get_numeric_data(self): ) # Check sharing - numeric.set("float", np.array([100.0, 200.0, 300.0])) + numeric.iset(numeric.items.get_loc("float"), np.array([100.0, 200.0, 300.0])) tm.assert_almost_equal( mgr.iget(mgr.items.get_loc("float")).internal_values(), np.array([100.0, 200.0, 300.0]), @@ -663,7 +663,9 @@ def test_get_numeric_data(self): tm.assert_index_equal( numeric.items, pd.Index(["int", "float", "complex", "bool"]) ) - numeric2.set("float", np.array([1000.0, 2000.0, 3000.0])) + numeric2.iset( + numeric2.items.get_loc("float"), np.array([1000.0, 2000.0, 3000.0]) + ) tm.assert_almost_equal( mgr.iget(mgr.items.get_loc("float")).internal_values(), np.array([100.0, 200.0, 300.0]), @@ -675,7 +677,7 @@ def test_get_bool_data(self): "str: object; bool: bool; obj: object; dt: datetime", item_shape=(3,), ) - mgr.set("obj", np.array([True, False, True], dtype=np.object_)) + mgr.iset(6, np.array([True, False, True], dtype=np.object_)) bools = mgr.get_bool_data() tm.assert_index_equal(bools.items, pd.Index(["bool"])) @@ -684,7 +686,7 @@ def test_get_bool_data(self): bools.iget(bools.items.get_loc("bool")).internal_values(), ) - bools.set("bool", np.array([True, False, True])) + bools.iset(0, np.array([True, False, True])) tm.assert_numpy_array_equal( mgr.iget(mgr.items.get_loc("bool")).internal_values(), np.array([True, False, True]), @@ -692,7 +694,7 @@ def test_get_bool_data(self): # Check sharing bools2 = mgr.get_bool_data(copy=True) - bools2.set("bool", np.array([False, True, False])) + bools2.iset(0, np.array([False, True, False])) tm.assert_numpy_array_equal( mgr.iget(mgr.items.get_loc("bool")).internal_values(), np.array([True, False, True]),