Skip to content

Commit d545ca0

Browse files
jbrockmendelCloseChoice
authored andcommitted
REF: remove BlockManager.set (pandas-dev#33347)
1 parent 57a2889 commit d545ca0

File tree

3 files changed

+36
-45
lines changed

3 files changed

+36
-45
lines changed

pandas/core/generic.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -3217,7 +3217,8 @@ def _maybe_cache_changed(self, item, value) -> None:
32173217
"""
32183218
The object has called back to us saying maybe it has changed.
32193219
"""
3220-
self._mgr.set(item, value)
3220+
loc = self._info_axis.get_loc(item)
3221+
self._mgr.iset(loc, value)
32213222

32223223
@property
32233224
def _is_cached(self) -> bool_t:
@@ -3594,8 +3595,14 @@ def _iset_item(self, loc: int, value) -> None:
35943595
self._clear_item_cache()
35953596

35963597
def _set_item(self, key, value) -> None:
3597-
self._mgr.set(key, value)
3598-
self._clear_item_cache()
3598+
try:
3599+
loc = self._info_axis.get_loc(key)
3600+
except KeyError:
3601+
# This item wasn't present, just insert at end
3602+
self._mgr.insert(len(self._info_axis), key, value)
3603+
return
3604+
3605+
NDFrame._iset_item(self, loc, value)
35993606

36003607
def _set_is_copy(self, ref, copy: bool_t = True) -> None:
36013608
if not copy:

pandas/core/internals/managers.py

-18
Original file line numberDiff line numberDiff line change
@@ -983,24 +983,6 @@ def idelete(self, indexer):
983983
)
984984
self._rebuild_blknos_and_blklocs()
985985

986-
def set(self, item: Label, value):
987-
"""
988-
Set new item in-place.
989-
990-
Notes
991-
-----
992-
Does not consolidate.
993-
Adds new Block if not contained in the current items Index.
994-
"""
995-
try:
996-
loc = self.items.get_loc(item)
997-
except KeyError:
998-
# This item wasn't present, just insert at end
999-
self.insert(len(self.items), item, value)
1000-
return
1001-
1002-
self.iset(loc, value)
1003-
1004986
def iset(self, loc: Union[int, slice, np.ndarray], value):
1005987
"""
1006988
Set new item in-place. Does not consolidate. Adds new Block if not

pandas/tests/internals/test_internals.py

+26-24
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ def test_iget(self):
342342
def test_set(self):
343343
mgr = create_mgr("a,b,c: int", item_shape=(3,))
344344

345-
mgr.set("d", np.array(["foo"] * 3))
346-
mgr.set("b", np.array(["bar"] * 3))
345+
mgr.insert(len(mgr.items), "d", np.array(["foo"] * 3))
346+
mgr.iset(1, np.array(["bar"] * 3))
347347
tm.assert_numpy_array_equal(mgr.iget(0).internal_values(), np.array([0] * 3))
348348
tm.assert_numpy_array_equal(
349349
mgr.iget(1).internal_values(), np.array(["bar"] * 3, dtype=np.object_)
@@ -354,22 +354,22 @@ def test_set(self):
354354
)
355355

356356
def test_set_change_dtype(self, mgr):
357-
mgr.set("baz", np.zeros(N, dtype=bool))
357+
mgr.insert(len(mgr.items), "baz", np.zeros(N, dtype=bool))
358358

359-
mgr.set("baz", np.repeat("foo", N))
359+
mgr.iset(mgr.items.get_loc("baz"), np.repeat("foo", N))
360360
idx = mgr.items.get_loc("baz")
361361
assert mgr.iget(idx).dtype == np.object_
362362

363363
mgr2 = mgr.consolidate()
364-
mgr2.set("baz", np.repeat("foo", N))
364+
mgr2.iset(mgr2.items.get_loc("baz"), np.repeat("foo", N))
365365
idx = mgr2.items.get_loc("baz")
366366
assert mgr2.iget(idx).dtype == np.object_
367367

368-
mgr2.set("quux", tm.randn(N).astype(int))
368+
mgr2.insert(len(mgr2.items), "quux", tm.randn(N).astype(int))
369369
idx = mgr2.items.get_loc("quux")
370370
assert mgr2.iget(idx).dtype == np.int_
371371

372-
mgr2.set("quux", tm.randn(N))
372+
mgr2.iset(mgr2.items.get_loc("quux"), tm.randn(N))
373373
assert mgr2.iget(idx).dtype == np.float_
374374

375375
def test_copy(self, mgr):
@@ -496,9 +496,9 @@ def _compare(old_mgr, new_mgr):
496496

497497
# convert
498498
mgr = create_mgr("a,b,foo: object; f: i8; g: f8")
499-
mgr.set("a", np.array(["1"] * N, dtype=np.object_))
500-
mgr.set("b", np.array(["2."] * N, dtype=np.object_))
501-
mgr.set("foo", np.array(["foo."] * N, dtype=np.object_))
499+
mgr.iset(0, np.array(["1"] * N, dtype=np.object_))
500+
mgr.iset(1, np.array(["2."] * N, dtype=np.object_))
501+
mgr.iset(2, np.array(["foo."] * N, dtype=np.object_))
502502
new_mgr = mgr.convert(numeric=True)
503503
assert new_mgr.iget(0).dtype == np.int64
504504
assert new_mgr.iget(1).dtype == np.float64
@@ -509,9 +509,9 @@ def _compare(old_mgr, new_mgr):
509509
mgr = create_mgr(
510510
"a,b,foo: object; f: i4; bool: bool; dt: datetime; i: i8; g: f8; h: f2"
511511
)
512-
mgr.set("a", np.array(["1"] * N, dtype=np.object_))
513-
mgr.set("b", np.array(["2."] * N, dtype=np.object_))
514-
mgr.set("foo", np.array(["foo."] * N, dtype=np.object_))
512+
mgr.iset(0, np.array(["1"] * N, dtype=np.object_))
513+
mgr.iset(1, np.array(["2."] * N, dtype=np.object_))
514+
mgr.iset(2, np.array(["foo."] * N, dtype=np.object_))
515515
new_mgr = mgr.convert(numeric=True)
516516
assert new_mgr.iget(0).dtype == np.int64
517517
assert new_mgr.iget(1).dtype == np.float64
@@ -599,11 +599,11 @@ def test_interleave_dtype(self, mgr_string, dtype):
599599
assert mgr.as_array().dtype == "object"
600600

601601
def test_consolidate_ordering_issues(self, mgr):
602-
mgr.set("f", tm.randn(N))
603-
mgr.set("d", tm.randn(N))
604-
mgr.set("b", tm.randn(N))
605-
mgr.set("g", tm.randn(N))
606-
mgr.set("h", tm.randn(N))
602+
mgr.iset(mgr.items.get_loc("f"), tm.randn(N))
603+
mgr.iset(mgr.items.get_loc("d"), tm.randn(N))
604+
mgr.iset(mgr.items.get_loc("b"), tm.randn(N))
605+
mgr.iset(mgr.items.get_loc("g"), tm.randn(N))
606+
mgr.iset(mgr.items.get_loc("h"), tm.randn(N))
607607

608608
# we have datetime/tz blocks in mgr
609609
cons = mgr.consolidate()
@@ -641,7 +641,7 @@ def test_get_numeric_data(self):
641641
"str: object; bool: bool; obj: object; dt: datetime",
642642
item_shape=(3,),
643643
)
644-
mgr.set("obj", np.array([1, 2, 3], dtype=np.object_))
644+
mgr.iset(5, np.array([1, 2, 3], dtype=np.object_))
645645

646646
numeric = mgr.get_numeric_data()
647647
tm.assert_index_equal(
@@ -653,7 +653,7 @@ def test_get_numeric_data(self):
653653
)
654654

655655
# Check sharing
656-
numeric.set("float", np.array([100.0, 200.0, 300.0]))
656+
numeric.iset(numeric.items.get_loc("float"), np.array([100.0, 200.0, 300.0]))
657657
tm.assert_almost_equal(
658658
mgr.iget(mgr.items.get_loc("float")).internal_values(),
659659
np.array([100.0, 200.0, 300.0]),
@@ -663,7 +663,9 @@ def test_get_numeric_data(self):
663663
tm.assert_index_equal(
664664
numeric.items, pd.Index(["int", "float", "complex", "bool"])
665665
)
666-
numeric2.set("float", np.array([1000.0, 2000.0, 3000.0]))
666+
numeric2.iset(
667+
numeric2.items.get_loc("float"), np.array([1000.0, 2000.0, 3000.0])
668+
)
667669
tm.assert_almost_equal(
668670
mgr.iget(mgr.items.get_loc("float")).internal_values(),
669671
np.array([100.0, 200.0, 300.0]),
@@ -675,7 +677,7 @@ def test_get_bool_data(self):
675677
"str: object; bool: bool; obj: object; dt: datetime",
676678
item_shape=(3,),
677679
)
678-
mgr.set("obj", np.array([True, False, True], dtype=np.object_))
680+
mgr.iset(6, np.array([True, False, True], dtype=np.object_))
679681

680682
bools = mgr.get_bool_data()
681683
tm.assert_index_equal(bools.items, pd.Index(["bool"]))
@@ -684,15 +686,15 @@ def test_get_bool_data(self):
684686
bools.iget(bools.items.get_loc("bool")).internal_values(),
685687
)
686688

687-
bools.set("bool", np.array([True, False, True]))
689+
bools.iset(0, np.array([True, False, True]))
688690
tm.assert_numpy_array_equal(
689691
mgr.iget(mgr.items.get_loc("bool")).internal_values(),
690692
np.array([True, False, True]),
691693
)
692694

693695
# Check sharing
694696
bools2 = mgr.get_bool_data(copy=True)
695-
bools2.set("bool", np.array([False, True, False]))
697+
bools2.iset(0, np.array([False, True, False]))
696698
tm.assert_numpy_array_equal(
697699
mgr.iget(mgr.items.get_loc("bool")).internal_values(),
698700
np.array([True, False, True]),

0 commit comments

Comments
 (0)