Skip to content

TST: add test for .loc indexing Index type preservation #29533

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
Changes from 2 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
23 changes: 23 additions & 0 deletions pandas/tests/frame/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1944,6 +1944,29 @@ def test_set_value_resize(self, float_frame):
with pytest.raises(ValueError, match=msg):
res3._set_value("foobar", "baz", "sam")

def test_loc_indexing_preserves_index_category_dtype(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you move this to: TestDataFrameIndexingCategorical

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, @jreback - unfortunately, I couldn't find tests/frame/indexing/test_..., that's why I put the test in tests/frame/test_indexing.py.

However, there seems to be a slight inconsistency in the tests/frame/ and tests/series/ structure as there's a indexing subfolder in series but not in frame.

Copy link
Contributor

Choose a reason for hiding this comment

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

this is a class name in the current file

However, there seems to be a slight inconsistency in the tests/frame/ and tests/series/ structure as there's a indexing subfolder in series but not in frame.

yes I know, there is an issue about fixing this (happy to have a PR), but orthogonal to this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I see - moved the test there. Thanks

# GH 15166
df = DataFrame(
data=np.arange(2, 22, 2),
index=pd.MultiIndex(
levels=[pd.CategoricalIndex(["a", "b"]), range(10)],
codes=[[0] * 5 + [1] * 5, range(10)],
names=["Index1", "Index2"],
),
)

result_1 = df.index.levels[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

can you write this like

expected=

result=
tm.assert_index_equal....

result=
tm.assert_index_equal....

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, @jreback - done.

result_2 = df.loc[["a"]].index.levels[0]
expected = pd.CategoricalIndex(
["a", "b"],
categories=["a", "b"],
ordered=False,
name="Index1",
dtype="category",
)
tm.assert_index_equal(result_1, expected)
tm.assert_index_equal(result_2, expected)

def test_set_value_with_index_dtype_change(self):
df_orig = DataFrame(np.random.randn(3, 3), index=range(3), columns=list("ABC"))

Expand Down