Skip to content

Add base test for extensionarray setitem #23300 #23304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,12 @@ def mock():
return pytest.importorskip("mock")


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


# ----------------------------------------------------------------
# Global setup for tests using Hypothesis

Expand Down
54 changes: 29 additions & 25 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):
if box:
data = pd.Series(data)
data[0] = data[1]
assert data[0] == data[1]

def test_setitem_sequence(self, data, box):
if box:
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 @@ -36,25 +38,27 @@ def test_setitem_sequence_mismatched_length_raises(self, data, as_array):
with tm.assert_raises_regex(ValueError, xpr.format('slice')):
ser[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)
def test_setitem_empty_indxer(self, data, box):
if box:
data = pd.Series(data)
original = data.copy()
data[[]] = []
self.assert_series_equal(data, original)

arr[[0, 1]] = data[2]
assert arr[0] == data[2]
assert arr[1] == data[2]
def test_setitem_sequence_broadcasts(self, data, box):
if box:
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):
arr = pd.Series(data)
setter = getattr(arr, setter)
def test_setitem_scalar(self, data, box, setter):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will not work, since loc and iloc are only available on Series.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed.
thanks for the quick feedback!

if box:
data = pd.Series(data)
setter = getattr(data, setter)
operator.setitem(setter, 0, data[1])
assert arr[0] == data[1]
assert data[0] == data[1]

def test_setitem_loc_scalar_mixed(self, data):
df = pd.DataFrame({"A": np.arange(len(data)), "B": data})
Expand Down