Skip to content

Commit 3bcb402

Browse files
committed
Use tm.assert_frame_equal and smoke test from GH issue
1 parent 8410210 commit 3bcb402

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

pandas/core/indexing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2732,8 +2732,8 @@ def _non_reducing_slice(slice_):
27322732
slice_ = IndexSlice[:, slice_]
27332733

27342734
def pred(part):
2735-
# true when slice does *not* reduce
2736-
# False when part is a tuple, i.e. MultiIndex slice
2735+
# true when slice does *not* reduce False when part is a tuple,
2736+
# i.e. MultiIndex slice
27372737
if isinstance(part, tuple):
27382738
return False
27392739
return isinstance(part, slice) or is_list_like(part)

pandas/tests/indexing/test_indexing.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -908,13 +908,14 @@ def test_non_reducing_slice_on_multiindex(self):
908908
('b', 'c'): [3, 2],
909909
('b', 'd'): [4, 1]
910910
}
911-
912911
df = pd.DataFrame(dic, index=[0, 1])
913912
idx = pd.IndexSlice
914-
915913
slice_ = idx[:, idx['b', 'd']]
916914
tslice_ = _non_reducing_slice(slice_)
917-
assert isinstance(df.loc[tslice_], DataFrame)
915+
916+
result = df.loc[tslice_]
917+
expected = pd.DataFrame({('b', 'd'): [4, 1]})
918+
tm.assert_frame_equal(result, expected)
918919

919920
def test_list_slice(self):
920921
# like dataframe getitem

pandas/tests/io/formats/test_style.py

+25
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,31 @@ def f(x):
274274
col in self.df.loc[slice_].columns}
275275
assert result == expected
276276

277+
def test_applymap_subset_multiindex(self):
278+
# https://github.com/pandas-dev/pandas/issues/19861
279+
def color_negative_red(val):
280+
"""
281+
Takes a scalar and returns a string with
282+
the css property `'color: red'` for negative
283+
strings, black otherwise.
284+
"""
285+
color = 'red' if val < 0 else 'black'
286+
return 'color: %s' % color
287+
288+
dic = {
289+
('a', 'd'): [-1.12, 2.11],
290+
('a', 'c'): [2.78, -2.88],
291+
('b', 'c'): [-3.99, 3.77],
292+
('b', 'd'): [4.21, -1.22],
293+
}
294+
295+
idx = pd.IndexSlice
296+
df = pd.DataFrame(dic, index=[0, 1])
297+
298+
(df.style
299+
.applymap(color_negative_red, subset=idx[:, idx['b', 'd']])
300+
.render())
301+
277302
def test_where_with_one_style(self):
278303
# GH 17474
279304
def f(x):

0 commit comments

Comments
 (0)