Skip to content

Commit 74f7c26

Browse files
committed
Merge pull request #9877 from cpcloud/fix-index-dict-assign
Fix right hand side dict assignment for DataFrames
2 parents f0d4949 + 216f051 commit 74f7c26

File tree

5 files changed

+27
-8
lines changed

5 files changed

+27
-8
lines changed

doc/source/indexing.rst

+8
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,14 @@ new column.
249249
If you are using the IPython environment, you may also use tab-completion to
250250
see these accessible attributes.
251251

252+
You can also assign a ``dict`` to a row of a ``DataFrame``:
253+
254+
.. ipython:: python
255+
256+
x = pd.DataFrame({'x': [1, 2, 3], 'y': [3, 4, 5]})
257+
x.iloc[1] = dict(x=9, y=99)
258+
x
259+
252260
Slicing ranges
253261
--------------
254262

doc/source/whatsnew/v0.16.1.txt

+2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ Bug Fixes
134134

135135
- Bug in unequal comparisons between categorical data and a scalar, which was not in the categories (e.g. ``Series(Categorical(list("abc"), ordered=True)) > "d"``. This returned ``False`` for all elements, but now raises a ``TypeError``. Equality comparisons also now return ``False`` for ``==`` and ``True`` for ``!=``. (:issue:`9848`)
136136

137+
- Bug in DataFrame ``__setitem__`` when right hand side is a dictionary (:issue:`9874`)
138+
137139
- Bug in ``MultiIndex.sortlevel()`` results in unicode level name breaks (:issue:`9875`)
138140
- Bug in which ``groupby.transform`` incorrectly enforced output dtypes to match input dtypes. (:issue:`9807`)
139141

pandas/core/frame.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -4414,12 +4414,12 @@ def mode(self, axis=0, numeric_only=False):
44144414
"""
44154415
Gets the mode(s) of each element along the axis selected. Empty if nothing
44164416
has 2+ occurrences. Adds a row for each mode per label, fills in gaps
4417-
with nan.
4418-
4417+
with nan.
4418+
44194419
Note that there could be multiple values returned for the selected
4420-
axis (when more than one item share the maximum frequency), which is the
4421-
reason why a dataframe is returned. If you want to impute missing values
4422-
with the mode in a dataframe ``df``, you can just do this:
4420+
axis (when more than one item share the maximum frequency), which is the
4421+
reason why a dataframe is returned. If you want to impute missing values
4422+
with the mode in a dataframe ``df``, you can just do this:
44234423
``df.fillna(df.mode().iloc[0])``
44244424
44254425
Parameters

pandas/core/indexing.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ def _has_valid_positional_setitem_indexer(self, indexer):
200200
return True
201201

202202
def _setitem_with_indexer(self, indexer, value):
203-
204203
self._has_valid_setitem_indexer(indexer)
205204

206205
# also has the side effect of consolidating in-place
@@ -486,8 +485,8 @@ def can_do_equal_len():
486485
self.obj[item_labels[indexer[info_axis]]] = value
487486
return
488487

489-
if isinstance(value, ABCSeries):
490-
value = self._align_series(indexer, value)
488+
if isinstance(value, (ABCSeries, dict)):
489+
value = self._align_series(indexer, Series(value))
491490

492491
elif isinstance(value, ABCDataFrame):
493492
value = self._align_frame(indexer, value)

pandas/tests/test_indexing.py

+10
Original file line numberDiff line numberDiff line change
@@ -4411,6 +4411,16 @@ def test_slice_with_zero_step_raises(self):
44114411
self.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
44124412
lambda: s.ix[::0])
44134413

4414+
def test_indexing_assignment_dict_already_exists(self):
4415+
df = pd.DataFrame({'x': [1, 2, 6],
4416+
'y': [2, 2, 8],
4417+
'z': [-5, 0, 5]}).set_index('z')
4418+
expected = df.copy()
4419+
rhs = dict(x=9, y=99)
4420+
df.loc[5] = rhs
4421+
expected.loc[5] = [9, 99]
4422+
tm.assert_frame_equal(df, expected)
4423+
44144424

44154425
class TestSeriesNoneCoercion(tm.TestCase):
44164426
EXPECTED_RESULTS = [

0 commit comments

Comments
 (0)