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

Conversation

ryankarlos
Copy link
Contributor

@ryankarlos ryankarlos commented Nov 10, 2019

@ryankarlos ryankarlos force-pushed the TST-loc-axis1-singlelevel branch from 8c0c56d to e8a27bb Compare November 10, 2019 03:38
@@ -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

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

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?

@gfyoung gfyoung added DataFrame DataFrame data structure Indexing Related to indexing on series/frames, not to indexes themselves Testing pandas testing functions or related to the test suite labels Nov 10, 2019
@ryankarlos
Copy link
Contributor Author

@mroeschke all green now

@jreback jreback added this to the 1.0 milestone Nov 13, 2019
@jreback jreback merged commit 0cbf399 into pandas-dev:master Nov 13, 2019
@jreback
Copy link
Contributor

jreback commented Nov 13, 2019

thanks @ryankarlos

Reksbril pushed a commit to Reksbril/pandas that referenced this pull request Nov 18, 2019
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DataFrame DataFrame data structure Indexing Related to indexing on series/frames, not to indexes themselves Testing pandas testing functions or related to the test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DataFrame.loc(axis=1)[..] not working for single-level indexer
4 participants