Skip to content

BUG: frame.iloc[i] with a single EA column #45241

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 3 commits into from
Jan 10, 2022
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ Interval

Indexing
^^^^^^^^
- Bug in :meth:`DataFrame.iloc` where indexing a single row on a :class:`DataFrame` with a single ExtensionDtype column gave a copy instead of a view on the underlying data (:issue:`45241`)
- Bug in :meth:`Series.__setitem__` with a non-integer :class:`Index` when using an integer key to set a value that cannot be set inplace where a ``ValueError`` was raised insead of casting to a common dtype (:issue:`45070`)
-

Expand Down
10 changes: 7 additions & 3 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1509,9 +1509,13 @@ def iget(self, i: int | tuple[int, int] | tuple[slice, int]):
if not com.is_null_slice(col) and col != 0:
raise IndexError(f"{self} only contains one item")
elif isinstance(col, slice):
if col != slice(None):
raise NotImplementedError(col)
return self.values[[loc]]
# the is_null_slice check above assures that col is slice(None)
# so what we want is a view on all our columns and row loc
if loc < 0:
loc += len(self.values)
# Note: loc:loc+1 vs [[loc]] makes a difference when called
# from fast_xs because we want to get a view back.
return self.values[loc : loc + 1]
return self.values[loc]
else:
if i != 0:
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
array,
concat,
date_range,
interval_range,
isna,
)
import pandas._testing as tm
Expand Down Expand Up @@ -1177,6 +1178,21 @@ def test_iloc_setitem_2d_ndarray_into_ea_block(self):
expected = DataFrame({"status": ["a", "a", "c"]}, dtype=df["status"].dtype)
tm.assert_frame_equal(df, expected)

@td.skip_array_manager_not_yet_implemented
def test_iloc_getitem_int_single_ea_block_view(self):
# GH#45241
# TODO: make an extension interface test for this?
arr = interval_range(1, 10.0)._values
df = DataFrame(arr)

# ser should be a *view* on the the DataFrame data
ser = df.iloc[2]

# if we have a view, then changing arr[2] should also change ser[0]
assert arr[2] != arr[-1] # otherwise the rest isn't meaningful
arr[2] = arr[-1]
assert ser[0] == arr[-1]


class TestILocErrors:
# NB: this test should work for _any_ Series we can pass as
Expand Down