Skip to content

Commit 6822e43

Browse files
jbrockmendeljreback
authored andcommitted
TST: tests for preserving views (#30523)
1 parent 0f86ddb commit 6822e43

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

pandas/tests/arrays/test_numpy.py

+22
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,25 @@ def test_setitem_no_coercion():
226226
arr = PandasArray(np.array([1, 2, 3]))
227227
with pytest.raises(ValueError, match="int"):
228228
arr[0] = "a"
229+
230+
# With a value that we do coerce, check that we coerce the value
231+
# and not the underlying array.
232+
arr[0] = 2.5
233+
assert isinstance(arr[0], (int, np.integer)), type(arr[0])
234+
235+
236+
def test_setitem_preserves_views():
237+
# GH#28150, see also extension test of the same name
238+
arr = PandasArray(np.array([1, 2, 3]))
239+
view1 = arr.view()
240+
view2 = arr[:]
241+
view3 = np.asarray(arr)
242+
243+
arr[0] = 9
244+
assert view1[0] == 9
245+
assert view2[0] == 9
246+
assert view3[0] == 9
247+
248+
arr[-1] = 2.5
249+
view1[-1] = 5
250+
assert arr[-1] == 5

pandas/tests/extension/base/setitem.py

+11
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,14 @@ def test_setitem_scalar_key_sequence_raise(self, data):
186186
arr = data[:5].copy()
187187
with pytest.raises(ValueError):
188188
arr[0] = arr[[0, 1]]
189+
190+
def test_setitem_preserves_views(self, data):
191+
# GH#28150 setitem shouldn't swap the underlying data
192+
assert data[-1] != data[0] # otherwise test would not be meaningful
193+
194+
view1 = data.view()
195+
view2 = data[:]
196+
197+
data[0] = data[-1]
198+
assert view1[0] == data[-1]
199+
assert view2[0] == data[-1]

pandas/tests/extension/test_interval.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ class TestReshaping(BaseInterval, base.BaseReshapingTests):
147147

148148

149149
class TestSetitem(BaseInterval, base.BaseSetitemTests):
150-
pass
150+
@pytest.mark.xfail(reason="GH#27147 setitem changes underlying index")
151+
def test_setitem_preserves_views(self, data):
152+
super().test_setitem_preserves_views(data)
151153

152154

153155
class TestPrinting(BaseInterval, base.BasePrintingTests):

0 commit comments

Comments
 (0)