Skip to content

BUG: fix dic_values update #52188

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

Closed
wants to merge 8 commits into from
Closed
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
6 changes: 6 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import collections
from contextlib import suppress
import sys
from typing import (
Expand Down Expand Up @@ -1864,6 +1865,11 @@ def _setitem_with_indexer_split_path(self, indexer, value, name: str):
if len(value) == 1 and not is_integer(info_axis):
# This is a case like df.iloc[:3, [1]] = [0]
# where we treat as df.iloc[:3, 1] = 0

# to avoid "TypeError: 'dict_values' object is not subscriptable"
if isinstance(value, collections.abc.ValuesView):
value = list(value)

return self._setitem_with_indexer((pi, info_axis[0]), value[0])

raise ValueError(
Expand Down
62 changes: 62 additions & 0 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3197,6 +3197,68 @@ def test_loc_setitem_dict_timedelta_multiple_set(self):
)
tm.assert_frame_equal(result, expected)

def test_loc_setitems_dict(self):
# GH 52175

# single row, single column
df1 = DataFrame(index=[1, 2], columns=["a"])
d = {"b": 1.1}
df1.loc[1, d.keys()] = d.values()

# single row, multiple columns
df2 = DataFrame(index=[1, 2], columns=["a"])
d = {"b": 1.1, "c": 2.2}
df2.loc[1, d.keys()] = d.values()

# multiple rows, single column
df3 = DataFrame(index=[1, 2], columns=["a"])
d = {"b": 1.1}
df3.loc[[1, 2], d.keys()] = d.values()

# multiple rows, multiple columns
df4 = DataFrame(index=[1, 2], columns=["a"])
d = {"b": 1.1, "c": 2.2}
df4.loc[[1, 2], d.keys()] = d.values()

expected1 = DataFrame(
{
"a": Series([np.nan, np.nan], dtype="object"),
"b": [1.1, np.nan],
},
index=[1, 2],
)

expected2 = DataFrame(
{
"a": Series([np.nan, np.nan], dtype="object"),
"b": [1.1, np.nan],
"c": [2.2, np.nan],
},
index=[1, 2],
)

expected3 = DataFrame(
{
"a": Series([np.nan, np.nan], dtype="object"),
"b": [1.1, 1.1],
},
index=[1, 2],
)

expected4 = DataFrame(
{
"a": Series([np.nan, np.nan], dtype="object"),
"b": [1.1, 1.1],
"c": [2.2, 2.2],
},
index=[1, 2],
)

tm.assert_frame_equal(df1, expected1)
tm.assert_frame_equal(df2, expected2)
tm.assert_frame_equal(df3, expected3)
tm.assert_frame_equal(df4, expected4)

def test_loc_set_multiple_items_in_multiple_new_columns(self):
# GH 25594
df = DataFrame(index=[1, 2], columns=["a"])
Expand Down