Skip to content

Commit

Permalink
TST: Add base test for extensionarray setitem pandas-dev#23300 (panda…
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesdong1991 authored and tm9k1 committed Nov 19, 2018
1 parent 44b743d commit 3fac4d2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
49 changes: 27 additions & 22 deletions pandas/tests/extension/base/setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@


class BaseSetitemTests(BaseExtensionTests):
def test_setitem_scalar_series(self, data):
arr = pd.Series(data)
arr[0] = data[1]
assert arr[0] == data[1]

def test_setitem_sequence(self, data):
arr = pd.Series(data)
def test_setitem_scalar_series(self, data, box_in_series):
if box_in_series:
data = pd.Series(data)
data[0] = data[1]
assert data[0] == data[1]

def test_setitem_sequence(self, data, box_in_series):
if box_in_series:
data = pd.Series(data)
original = data.copy()

arr[[0, 1]] = [data[1], data[0]]
assert arr[0] == original[1]
assert arr[1] == original[0]
data[[0, 1]] = [data[1], data[0]]
assert data[0] == original[1]
assert data[1] == original[0]

@pytest.mark.parametrize('as_array', [True, False])
def test_setitem_sequence_mismatched_length_raises(self, data, as_array):
Expand All @@ -32,22 +34,25 @@ def test_setitem_sequence_mismatched_length_raises(self, data, as_array):
xpr = 'cannot set using a {} indexer with a different length'
with tm.assert_raises_regex(ValueError, xpr.format('list-like')):
ser[[0, 1]] = value
assert ser._values[[0, 1]] == value

with tm.assert_raises_regex(ValueError, xpr.format('slice')):
ser[slice(3)] = value
assert ser._values[slice(3)] == value

def test_setitem_empty_indxer(self, data):
ser = pd.Series(data)
original = ser.copy()
ser[[]] = []
self.assert_series_equal(ser, original)

def test_setitem_sequence_broadcasts(self, data):
arr = pd.Series(data)

arr[[0, 1]] = data[2]
assert arr[0] == data[2]
assert arr[1] == data[2]
def test_setitem_empty_indxer(self, data, box_in_series):
if box_in_series:
data = pd.Series(data)
original = data.copy()
data[np.array([], dtype=int)] = []
self.assert_equal(data, original)

def test_setitem_sequence_broadcasts(self, data, box_in_series):
if box_in_series:
data = pd.Series(data)
data[[0, 1]] = data[2]
assert data[0] == data[2]
assert data[1] == data[2]

@pytest.mark.parametrize('setter', ['loc', 'iloc'])
def test_setitem_scalar(self, data, setter):
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/extension/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,9 @@ def data_for_grouping():
Where A < B < C and NA is missing
"""
raise NotImplementedError


@pytest.fixture(params=[True, False])
def box_in_series(request):
"""Whether to box the data in a Series"""
return request.param

0 comments on commit 3fac4d2

Please sign in to comment.