Skip to content

Loc set ndarray empty df bug #1

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ Indexing
- Bug in :meth:`DataFrame.loc` raising ``ValueError`` with ``bool`` indexer and :class:`MultiIndex` (:issue:`47687`)
- Bug in :meth:`DataFrame.__setitem__` raising ``ValueError`` when right hand side is :class:`DataFrame` with :class:`MultiIndex` columns (:issue:`49121`)
- Bug in :meth:`DataFrame.reindex` casting dtype to ``object`` when :class:`DataFrame` has single extension array column when re-indexing ``columns`` and ``index`` (:issue:`48190`)
- Bug in :meth:`DataFrame.loc` when initializing empty dataframe to numpy ndarray (:issue:`49972`)
- Bug in :func:`~DataFrame.describe` when formatting percentiles in the resulting index showed more decimals than needed (:issue:`46362`)
- Bug in :meth:`DataFrame.compare` does not recognize differences when comparing ``NA`` with value in nullable dtypes (:issue:`48939`)
-
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1739,7 +1739,9 @@ def _setitem_with_indexer(self, indexer, value, name: str = "iloc"):
if not isinstance(value, ABCSeries):
# if not Series (in which case we need to align),
# we can short-circuit
empty_value[indexer[0]] = arr
empty_value[
key if isinstance(indexer[0], dict) else indexer[0]
] = arr
self.obj[key] = empty_value
return

Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,16 @@ def test_loc_setitem_missing_columns(self, index, box, expected):
df.loc[index] = box
tm.assert_frame_equal(df, expected)

def test_loc_setitem_numpy_asarray(self):

df = DataFrame()
rhs = np.asarray([0])

df.loc[0,0] = rhs

expected = DataFrame([0], dtype=np.float64)
tm.assert_frame_equal(df, expected)

def test_loc_coercion(self):

# GH#12411
Expand Down