-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
TST: Add tests for single level indexing with loc(axis=1) #29519
Conversation
8c0c56d
to
e8a27bb
Compare
@@ -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): | |||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
@mroeschke all green now |
thanks @ryankarlos |
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff