Skip to content

BUG: Fixed various setitem with iterable that does not have a matching length to the indexer (GH5508) #5512

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 1 commit into from
Nov 14, 2013
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,8 @@ Bug Fixes
- ``pd.to_timedelta`` of a scalar returns a scalar (:issue:`5410`)
- ``pd.to_timedelta`` accepts ``NaN`` and ``NaT``, returning ``NaT`` instead of raising (:issue:`5437`)
- performance improvements in ``isnull`` on larger size pandas objects
- Fixed various setitem with 1d ndarray that does not have a matching
length to the indexer (:issue:`5508`)

pandas 0.12.0
-------------
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ def can_do_equal_len():
# per label values
else:

if len(labels) != len(value):
raise ValueError('Must have equal len keys and value when'
' setting with an iterable')

for item, v in zip(labels, value):
setter(item, v)
else:
Expand Down
29 changes: 29 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,35 @@ def test_iloc_getitem_frame(self):
# trying to use a label
self.assertRaises(ValueError, df.iloc.__getitem__, tuple(['j','D']))

def test_setitem_ndarray_1d(self):
# GH5508

# len of indexer vs length of the 1d ndarray
df = DataFrame(index=Index(lrange(1,11)))
df['foo'] = np.zeros(10, dtype=np.float64)
df['bar'] = np.zeros(10, dtype=np.complex)

# invalid
def f():
df.ix[2:5, 'bar'] = np.array([2.33j, 1.23+0.1j, 2.2])
self.assertRaises(ValueError, f)

# valid
df.ix[2:5, 'bar'] = np.array([2.33j, 1.23+0.1j, 2.2, 1.0])

result = df.ix[2:5, 'bar']
expected = Series([2.33j, 1.23+0.1j, 2.2, 1.0],index=[2,3,4,5])
assert_series_equal(result,expected)

# dtype getting changed?
df = DataFrame(index=Index(lrange(1,11)))
df['foo'] = np.zeros(10, dtype=np.float64)
df['bar'] = np.zeros(10, dtype=np.complex)

def f():
df[2:5] = np.arange(1,4)*1j
self.assertRaises(ValueError, f)

def test_iloc_setitem_series(self):
""" originally from test_series.py """
df = DataFrame(np.random.randn(10, 4), index=list('abcdefghij'), columns=list('ABCD'))
Expand Down