Skip to content

Commit

Permalink
REF: remove BlockManager.set (#33347)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Apr 17, 2020
1 parent f11fb8c commit ab9f3c9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 45 deletions.
13 changes: 10 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
18 changes: 0 additions & 18 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 26 additions & 24 deletions pandas/tests/internals/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_)
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand All @@ -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]),
Expand All @@ -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]),
Expand All @@ -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"]))
Expand All @@ -684,15 +686,15 @@ 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]),
)

# 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]),
Expand Down

0 comments on commit ab9f3c9

Please sign in to comment.