Skip to content

Commit 681c26a

Browse files
committed
Use tm.assert_frame_equal and smoke test from GH issue
1 parent 561da9f commit 681c26a

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
@@ -2168,8 +2168,8 @@ def _non_reducing_slice(slice_):
21682168
slice_ = IndexSlice[:, slice_]
21692169

21702170
def pred(part):
2171-
# true when slice does *not* reduce
2172-
# False when part is a tuple, i.e. MultiIndex slice
2171+
# true when slice does *not* reduce False when part is a tuple,
2172+
# i.e. MultiIndex slice
21732173
if isinstance(part, tuple):
21742174
return False
21752175
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
@@ -853,13 +853,14 @@ def test_non_reducing_slice_on_multiindex(self):
853853
('b', 'c'): [3, 2],
854854
('b', 'd'): [4, 1]
855855
}
856-
857856
df = pd.DataFrame(dic, index=[0, 1])
858857
idx = pd.IndexSlice
859-
860858
slice_ = idx[:, idx['b', 'd']]
861859
tslice_ = _non_reducing_slice(slice_)
862-
assert isinstance(df.loc[tslice_], DataFrame)
860+
861+
result = df.loc[tslice_]
862+
expected = pd.DataFrame({('b', 'd'): [4, 1]})
863+
tm.assert_frame_equal(result, expected)
863864

864865
def test_list_slice(self):
865866
# like dataframe getitem

pandas/tests/io/formats/test_style.py

+25
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,31 @@ def f(x):
267267
col in self.df.loc[slice_].columns)
268268
assert result == expected
269269

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

0 commit comments

Comments
 (0)