Skip to content

BUG: item_cache invalidation #38351

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
Dec 8, 2020
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Timezones

Numeric
^^^^^^^

- Bug in :meth:`DataFrame.quantile`, :meth:`DataFrame.sort_values` causing incorrect subsequent indexing behavior (:issue:`38351`)
-
-

Expand Down
11 changes: 5 additions & 6 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,6 @@ def apply(
def quantile(
self,
axis: int = 0,
consolidate: bool = True,
transposed: bool = False,
interpolation="linear",
qs=None,
Expand Down Expand Up @@ -472,9 +471,6 @@ def quantile(
# simplify some of the code here and in the blocks
assert self.ndim >= 2

if consolidate:
self._consolidate_inplace()

def get_axe(block, qs, axes):
# Because Series dispatches to DataFrame, we will always have
# block.ndim == 2
Expand Down Expand Up @@ -1455,7 +1451,6 @@ def take(self, indexer, axis: int = 1, verify: bool = True, convert: bool = True
"""
Take items along any axis.
"""
self._consolidate_inplace()
indexer = (
np.arange(indexer.start, indexer.stop, indexer.step, dtype="int64")
if isinstance(indexer, slice)
Expand All @@ -1472,7 +1467,11 @@ def take(self, indexer, axis: int = 1, verify: bool = True, convert: bool = True

new_labels = self.axes[axis].take(indexer)
return self.reindex_indexer(
new_axis=new_labels, indexer=indexer, axis=axis, allow_dups=True
new_axis=new_labels,
indexer=indexer,
axis=axis,
allow_dups=True,
consolidate=False,
)

def equals(self, other: object) -> bool:
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/frame/methods/test_quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,15 @@ def test_quantile_empty_no_columns(self):
expected = DataFrame([], index=[0.5], columns=[])
expected.columns.name = "captain tightpants"
tm.assert_frame_equal(result, expected)

def test_quantile_item_cache(self):
# previous behavior incorrect retained an invalid _item_cache entry
df = DataFrame(np.random.randn(4, 3), columns=["A", "B", "C"])
df["D"] = df["A"] * 2
ser = df["A"]
assert len(df._mgr.blocks) == 2

df.quantile(numeric_only=False)
ser.values[0] = 99

assert df.iloc[0, 0] == df["A"][0]
12 changes: 12 additions & 0 deletions pandas/tests/frame/methods/test_sort_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,18 @@ def test_sort_values_nat_na_position_default(self):
result = expected.sort_values(["A", "date"])
tm.assert_frame_equal(result, expected)

def test_sort_values_item_cache(self):
# previous behavior incorrect retained an invalid _item_cache entry
df = DataFrame(np.random.randn(4, 3), columns=["A", "B", "C"])
df["D"] = df["A"] * 2
ser = df["A"]
assert len(df._mgr.blocks) == 2

df.sort_values(by="A")
ser.values[0] = 99

assert df.iloc[0, 0] == df["A"][0]


class TestDataFrameSortKey: # test key sorting (issue 27237)
def test_sort_values_inplace_key(self, sort_by_key):
Expand Down