Skip to content

BUG/TST: PeriodArray.__setitem__ with slice and list-like value #23991

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
merged 14 commits into from
Dec 2, 2018
9 changes: 6 additions & 3 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def __repr__(self):

def __setitem__(
self,
key, # type: Union[int, Sequence[int], Sequence[bool]]
key, # type: Union[int, Sequence[int], Sequence[bool], slice]
value # type: Union[NaTType, Period, Sequence[Period]]
):
# type: (...) -> None
Expand All @@ -357,11 +357,14 @@ def __setitem__(
# ndarray[datetime64ns]. I think ndarray[int] / ndarray[str] won't
# work, since the freq can't be inferred.
if is_list_like(value):
if len(key) != len(value) and not com.is_bool_indexer(key):
is_slice = isinstance(key, slice)
if (not is_slice
and len(key) != len(value)
and not com.is_bool_indexer(key)):
msg = ("shape mismatch: value array of length '{}' does not "
"match indexing result of length '{}'.")
raise ValueError(msg.format(len(key), len(value)))
if len(key) == 0:
if not is_slice and len(key) == 0:
return

value = period_array(value)
Expand Down
12 changes: 11 additions & 1 deletion pandas/tests/extension/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pandas as pd
from pandas.core.arrays import PeriodArray
from pandas.tests.extension import base
import pandas.util.testing as tm
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably delete this



@pytest.fixture
Expand Down Expand Up @@ -147,7 +148,16 @@ class TestReshaping(BasePeriodTests, base.BaseReshapingTests):


class TestSetitem(BasePeriodTests, base.BaseSetitemTests):
pass

Copy link
Contributor

Choose a reason for hiding this comment

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

And this.

def test_setitem_slice_mismatch_length_raises(self, data):
Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh, just noticed. This should be on the base class BaseSetitemTests. Then all the subclasses will be tested, including period.

Copy link
Contributor

Choose a reason for hiding this comment

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

Same for the other test.

Copy link
Member Author

Choose a reason for hiding this comment

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

ahh, i see, yeah, makes sense!! thx!

arr = data[:5]
with pytest.raises(ValueError):
arr[:1] = arr[:2]

def test_setitem_slice_array(self, data):
arr = data[:5].copy()
arr[:5] = data[-5:]
tm.assert_extension_array_equal(arr, data[-5:])


class TestGroupby(BasePeriodTests, base.BaseGroupbyTests):
Expand Down