Skip to content

BUG: Bug in setitem with empty indexer and unwanted coercion of dtypes (GH8669) #8671

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
Oct 29, 2014
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
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v0.15.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ Bug Fixes



- Bug in numeric index operations of add/sub with Float/Index Index with numpy arrays (:issue:`8608`)
- Bug in numeric index operations of add/sub with Float/Index Index with numpy arrays (:issue:`8608`
- Bug in setitem with empty indexer and unwanted coercion of dtypes (:issue:`8669`)



Expand Down
27 changes: 24 additions & 3 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,10 +549,31 @@ def setitem(self, indexer, value):
"different length than the value")

try:

def _is_scalar_indexer(indexer):
# treat a len 0 array like a scalar
# return True if we are all scalar indexers

if arr_value.ndim == 1:
if not isinstance(indexer, tuple):
indexer = tuple([indexer])

def _is_ok(idx):

if np.isscalar(idx):
return True
elif isinstance(idx, slice):
return False
return len(idx) == 0

return all([ _is_ok(idx) for idx in indexer ])
return False


# setting a single element for each dim and with a rhs that could be say a list
# GH 6043
if arr_value.ndim == 1 and (
np.isscalar(indexer) or (isinstance(indexer, tuple) and all([ np.isscalar(idx) for idx in indexer ]))):
# or empty indexers (so no astyping)
# GH 6043, 8669 (empty)
if _is_scalar_indexer(indexer):
values[indexer] = value

# if we are an exact match (ex-broadcasting),
Expand Down
8 changes: 7 additions & 1 deletion pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,13 @@ def test_loc_setitem_frame(self):
expected = DataFrame(dict(A = Series(val1,index=keys1), B = Series(val2,index=keys2))).reindex(index=index)
assert_frame_equal(df, expected)

# GH 8669
# invalid coercion of nan -> int
df = DataFrame({'A' : [1,2,3], 'B' : np.nan })
df.loc[df.B > df.A, 'B'] = df.A
expected = DataFrame({'A' : [1,2,3], 'B' : np.nan})
assert_frame_equal(df, expected)

# GH 6546
# setting with mixed labels
df = DataFrame({1:[1,2],2:[3,4],'a':['a','b']})
Expand All @@ -1055,7 +1062,6 @@ def test_loc_setitem_frame(self):
df.loc[0,[1,2]] = [5,6]
assert_frame_equal(df, expected)


def test_loc_setitem_frame_multiples(self):
# multiple setting
df = DataFrame({ 'A' : ['foo','bar','baz'],
Expand Down