Skip to content

TST: Add tests for single level indexing with loc(axis=1) #29519

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 6 commits into from
Nov 13, 2019
Merged
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
38 changes: 38 additions & 0 deletions pandas/tests/indexing/multiindex/test_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,44 @@ def test_loc_axis_arguments(self):
with pytest.raises(ValueError):
df.loc(axis="foo")[:, :, ["C1", "C3"]]

def test_loc_axis_single_level_multi_col_indexing_multiindex_col_df(self):

Copy link
Member

Choose a reason for hiding this comment

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

Could you add the issue number?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure

# GH29519
df = pd.DataFrame(
np.arange(27).reshape(3, 9),
columns=pd.MultiIndex.from_product(
[["a1", "a2", "a3"], ["b1", "b2", "b3"]]
),
)
result = df.loc(axis=1)["a1":"a2"]
expected = df.iloc[:, :-3]

tm.assert_frame_equal(result, expected)

def test_loc_axis_single_level_single_col_indexing_multiindex_col_df(self):

# GH29519
df = pd.DataFrame(
np.arange(27).reshape(3, 9),
columns=pd.MultiIndex.from_product(
[["a1", "a2", "a3"], ["b1", "b2", "b3"]]
),
)
result = df.loc(axis=1)["a1"]
expected = df.iloc[:, :3]
expected.columns = ["b1", "b2", "b3"]

tm.assert_frame_equal(result, expected)
Copy link
Member

Choose a reason for hiding this comment

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

Could you make 3 different functions for these tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mroeschke Is it ok if I split it into 2 functions - one for Multindex column df and one for simple df --- and for the Multiindex bit I can comment on the two different tests - i.e one is slicing on multi columns on single level and other is slicing on single col. Otherwise, having concise names for the functions is tricky.

Something like this:

        # GH29519
        # test with single level multiple columns slice
        df1 = pd.DataFrame(
            np.arange(27).reshape(3, 9),
            columns=pd.MultiIndex.from_product(
                [["a1", "a2", "a3"], ["b1", "b2", "b3"]]
            ),
        )
        result = df1.loc(axis=1)["a1":"a2"]
        expected = df1.iloc[:, :-3]

        tm.assert_frame_equal(result, expected)

        # GH29519
        # test with single level single column slice
        result = df1.loc(axis=1)["a1"]
        expected = df1.iloc[:, :3]
        expected.columns = ["b1", "b2", "b3"]

        tm.assert_frame_equal(result, expected)


Copy link
Member

Choose a reason for hiding this comment

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

Verbose function names are fine. Helps isolate the failing test as separate methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok just pushed with changes as you originally suggested


def test_loc_ax_single_level_indexer_simple_df(self):

# GH29519
# test single level indexing on single index column data frame
df = pd.DataFrame(np.arange(9).reshape(3, 3), columns=["a", "b", "c"])
result = df.loc(axis=1)["a"]
expected = pd.Series(np.array([0, 3, 6]), name="a")
tm.assert_series_equal(result, expected)

def test_per_axis_per_level_setitem(self):

# test index maker
Expand Down