Skip to content

BUG: bug in setitem where type promotion is applied to entire block #10308

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
Jun 9, 2015
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.16.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ Bug Fixes

- Bug in getting timezone data with ``dateutil`` on various platforms ( :issue:`9059`, :issue:`8639`, :issue:`9663`, :issue:`10121`)
- Bug in display datetimes with mixed frequencies uniformly; display 'ms' datetimes to the proper precision. (:issue:`10170`)
- Bug in ``setitem`` where type pormotion is applied to entire block (:issue:`10280`)

- Bug in ``Series`` arithmetic methods may incorrectly hold names (:issue:`10068`)

Expand Down
9 changes: 9 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,15 @@ def _setitem_with_indexer(self, indexer, value):

# maybe partial set
take_split_path = self.obj._is_mixed_type

# if there is only one block/type, still have to take split path
# unless the block is one-dimensional or it can hold the value
if not take_split_path and self.obj._data.blocks:
blk, = self.obj._data.blocks
if 1 < blk.ndim: # in case of dict, keys are indices
val = list(value.values()) if isinstance(value,dict) else value
take_split_path = not blk._can_hold_element(val)

if isinstance(indexer, tuple):
nindexer = []
for i, idx in enumerate(indexer):
Expand Down
25 changes: 25 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2330,6 +2330,31 @@ def test_setitem_dtype_upcast(self):
expected = DataFrame([{"a": 1, "c" : 'foo'}, {"a": 3, "b": 2, "c" : np.nan}])
assert_frame_equal(df,expected)

# GH10280
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add some additonal tests (these should already work), for separation of other blocks types, e.g. object->float, float->object).

df = DataFrame(np.arange(6).reshape(2, 3), index=list('ab'),
columns=['foo', 'bar', 'baz'])

for val in [3.14, 'wxyz']:
left = df.copy()
left.loc['a', 'bar'] = val
right = DataFrame([[0, val, 2], [3, 4, 5]], index=list('ab'),
columns=['foo', 'bar', 'baz'])

assert_frame_equal(left, right)
self.assertTrue(com.is_integer_dtype(left['foo']))
self.assertTrue(com.is_integer_dtype(left['baz']))

left = DataFrame(np.arange(6).reshape(2, 3) / 10.0, index=list('ab'),
columns=['foo', 'bar', 'baz'])
left.loc['a', 'bar'] = 'wxyz'

right = DataFrame([[0, 'wxyz', .2], [.3, .4, .5]], index=list('ab'),
columns=['foo', 'bar', 'baz'])

assert_frame_equal(left, right)
self.assertTrue(com.is_float_dtype(left['foo']))
self.assertTrue(com.is_float_dtype(left['baz']))

def test_setitem_iloc(self):


Expand Down