Skip to content

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

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

Closed
TomAugspurger opened this issue Nov 28, 2018 · 2 comments · Fixed by #23991
Closed

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

TomAugspurger opened this issue Nov 28, 2018 · 2 comments · Fixed by #23991
Labels
ExtensionArray Extending pandas with custom dtypes or arrays. good first issue Indexing Related to indexing on series/frames, not to indexes themselves Period Period data type Testing pandas testing functions or related to the test suite
Milestone

Comments

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Nov 28, 2018

In [1]: import pandas as pd; import numpy as np

In [2]: arr = pd.core.arrays.period_array([2000, 2001], 'D')

In [3]: arr[:1] = ['2001']
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-907919e36fd9> in <module>()
----> 1 arr[:1] = ['2001']

~/sandbox/pandas/pandas/core/arrays/period.py in __setitem__(self, key, value)
    358         # work, since the freq can't be inferred.
    359         if is_list_like(value):
--> 360             if len(key) != len(value) and not com.is_bool_indexer(key):
    361                 msg = ("shape mismatch: value array of length '{}' does not "
    362                        "match indexing result of length '{}'.")

TypeError: object of type 'slice' has no len()

We should be checking that the if key is a slice

            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 not is_slice and len(key) == 0:
                return

Then, if they slice length doesn't match with value, NumPy will complain at the end when we do self._data[key] = value.

I've fixed this on my all-in-one branch. To close this issue, we should also add a base extension tests to BaseSetitemTests like

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

and a successful one like

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

(those are untested)

@TomAugspurger TomAugspurger added Testing pandas testing functions or related to the test suite Indexing Related to indexing on series/frames, not to indexes themselves ExtensionArray Extending pandas with custom dtypes or arrays. Period Period data type labels Nov 28, 2018
@TomAugspurger TomAugspurger added this to the 0.24.0 milestone Nov 28, 2018
@charlesdong1991
Copy link
Member

Hello, has your all-in-one branch been merged into master? i fetched all, and looks like this change is not in master yet? maybe i did something wrong...

@TomAugspurger
Copy link
Contributor Author

No, it's not quite ready for a PR yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ExtensionArray Extending pandas with custom dtypes or arrays. good first issue Indexing Related to indexing on series/frames, not to indexes themselves Period Period data type Testing pandas testing functions or related to the test suite
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants