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 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/indexing/multiindex/test_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,29 @@ def test_loc_axis_arguments(self):
with pytest.raises(ValueError):
df.loc(axis="foo")[:, :, ["C1", "C3"]]

def test_loc_axis_single_level_indexer(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

# test single level indexing on Multindex column dataframe
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)
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.

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


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

Choose a reason for hiding this comment

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

Why is check_dtype=False set?

Copy link
Contributor Author

@ryankarlos ryankarlos Nov 10, 2019

Choose a reason for hiding this comment

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

@mroeschke Since CI was failing for some of the 32 bit OS builds because of int32 vs int64 dtype comparison - im having a similar issue in #29522. Don't know if there is any other work around.

================================== FAILURES ===================================
__________ TestMultiIndexSlicers.test_loc_axis_single_level_indexer ___________
[gw0] win32 -- Python 3.6.7 C:\Miniconda\envs\pandas-dev\python.exe

self = <pandas.tests.indexing.multiindex.test_slice.TestMultiIndexSlicers object at 0x00000156C517C898>

    def test_loc_axis_single_level_indexer(self):
    
        # test single level indexing on Multindex column dataframe
        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)
        result = df1.loc(axis=1)["a1"]
        expected = df1.iloc[:, :3]
        expected.columns = ["b1", "b2", "b3"]
        tm.assert_frame_equal(result, expected)
    
        # test single level indexing on single index column data frame
        df2 = pd.DataFrame(np.arange(9).reshape(3, 3), columns=["a", "b", "c"])
        result = df2.loc(axis=1)["a"]
        expected = pd.Series([0, 3, 6], dtype="int64", name="a")
>       tm.assert_series_equal(result, expected)
E       AssertionError: Attributes of Series are different
E       
E       Attribute "dtype" are different
E       [left]:  int32
E       [right]: int64

pandas\tests\indexing\multiindex\test_slice.py:516: AssertionError


Copy link
Member

Choose a reason for hiding this comment

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

I see you're not assigning dtype="int64" anymore. Could you remove check_dtype=False now?


def test_per_axis_per_level_setitem(self):

# test index maker
Expand Down