Skip to content

Commit

Permalink
Merge pull request pandas-dev#9877 from cpcloud/fix-index-dict-assign
Browse files Browse the repository at this point in the history
Fix right hand side dict assignment for DataFrames
  • Loading branch information
cpcloud committed Apr 14, 2015
2 parents f0d4949 + 216f051 commit 74f7c26
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
8 changes: 8 additions & 0 deletions doc/source/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ new column.
If you are using the IPython environment, you may also use tab-completion to
see these accessible attributes.

You can also assign a ``dict`` to a row of a ``DataFrame``:

.. ipython:: python
x = pd.DataFrame({'x': [1, 2, 3], 'y': [3, 4, 5]})
x.iloc[1] = dict(x=9, y=99)
x
Slicing ranges
--------------

Expand Down
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.16.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ Bug Fixes

- 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`)

- Bug in DataFrame ``__setitem__`` when right hand side is a dictionary (:issue:`9874`)

- Bug in ``MultiIndex.sortlevel()`` results in unicode level name breaks (:issue:`9875`)
- Bug in which ``groupby.transform`` incorrectly enforced output dtypes to match input dtypes. (:issue:`9807`)

Expand Down
10 changes: 5 additions & 5 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4414,12 +4414,12 @@ def mode(self, axis=0, numeric_only=False):
"""
Gets the mode(s) of each element along the axis selected. Empty if nothing
has 2+ occurrences. Adds a row for each mode per label, fills in gaps
with nan.
with nan.
Note that there could be multiple values returned for the selected
axis (when more than one item share the maximum frequency), which is the
reason why a dataframe is returned. If you want to impute missing values
with the mode in a dataframe ``df``, you can just do this:
axis (when more than one item share the maximum frequency), which is the
reason why a dataframe is returned. If you want to impute missing values
with the mode in a dataframe ``df``, you can just do this:
``df.fillna(df.mode().iloc[0])``
Parameters
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ def _has_valid_positional_setitem_indexer(self, indexer):
return True

def _setitem_with_indexer(self, indexer, value):

self._has_valid_setitem_indexer(indexer)

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

if isinstance(value, ABCSeries):
value = self._align_series(indexer, value)
if isinstance(value, (ABCSeries, dict)):
value = self._align_series(indexer, Series(value))

elif isinstance(value, ABCDataFrame):
value = self._align_frame(indexer, value)
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4411,6 +4411,16 @@ def test_slice_with_zero_step_raises(self):
self.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: s.ix[::0])

def test_indexing_assignment_dict_already_exists(self):
df = pd.DataFrame({'x': [1, 2, 6],
'y': [2, 2, 8],
'z': [-5, 0, 5]}).set_index('z')
expected = df.copy()
rhs = dict(x=9, y=99)
df.loc[5] = rhs
expected.loc[5] = [9, 99]
tm.assert_frame_equal(df, expected)


class TestSeriesNoneCoercion(tm.TestCase):
EXPECTED_RESULTS = [
Expand Down

0 comments on commit 74f7c26

Please sign in to comment.