Skip to content

Fix right hand side dict assignment for DataFrames #9877

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 4 commits into from
Apr 14, 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
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``:
Copy link
Member

Choose a reason for hiding this comment

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

Can you maybe specify here that the dict keys are matched with the column names?


.. 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 @@ -136,4 +136,6 @@ 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`)
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
8 changes: 4 additions & 4 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5970,18 +5970,18 @@ def test_boolean_comparison(self):

def test_equals_different_blocks(self):
# GH 9330
df0 = pd.DataFrame({"A": ["x","y"], "B": [1,2],
df0 = pd.DataFrame({"A": ["x","y"], "B": [1,2],
"C": ["w","z"]})
df1 = df0.reset_index()[["A","B","C"]]
# this assert verifies that the above operations have
# this assert verifies that the above operations have
# induced a block rearrangement
self.assertTrue(df0._data.blocks[0].dtype !=
self.assertTrue(df0._data.blocks[0].dtype !=
df1._data.blocks[0].dtype)
# do the real tests
self.assert_frame_equal(df0, df1)
self.assertTrue(df0.equals(df1))
self.assertTrue(df1.equals(df0))

def test_to_csv_from_csv(self):

pname = '__tmp_to_csv_from_csv__'
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