Skip to content

BUG: Series with non-unique index: "Index length did not match values" error upon assignment #4548

Closed
@jgehrcke

Description

@jgehrcke

Create a Series with non-unique index:

>>> import pandas as pd
>>> pd.__version__
'0.12.0'
>>> s1 = pd.Series(range(3))
>>> s2 = pd.Series(range(3))
>>> comb = pd.concat([s1,s2])
>>> comb
0    0
1    1
2    2
0    0
1    1
2    2
dtype: int64

Assign value by boolean mask:

>>> comb[comb<1] = 5
>>> comb
0    5
1    1
2    2
0    5
1    1
2    2
dtype: int64

This has worked. Now add a value by boolean mask:

>>> comb[comb<2] += 10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "***/pandas/core/series.py", line 852, in __setitem__
    self.where(~key,value,inplace=True)
  File "***/pandas/core/series.py", line 749, in where
    other = other.reindex(ser.index)
  File "***/pandas/core/series.py", line 2646, in reindex
    return self._reindex_with_indexers(new_index, indexer, copy=copy, fill_value=fill_value)
  File "***/pandas/core/series.py", line 2650, in _reindex_with_indexers
    return Series(new_values, index=index, name=self.name)
  File "***/pandas/core/series.py", line 492, in __new__
    subarr.index = index
  File "properties.pyx", line 74, in pandas.lib.SeriesIndex.__set__ (pandas/lib.c:29541)
AssertionError: Index length did not match values

Is this expected behavior? If it is, I am sorry, because this was not clear to me from the docs and I am just wondering why simple assignment via = works and special assignment via += does not...

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions