style apply of rows fails for subset list, but works for single row? · Issue #13222 · pandas-dev/pandas (original) (raw)
Code Sample, a copy-pastable example if possible
df = pd.DataFrame([["A", 1],["B", 2]], columns=["Letter", "Number"])
def highlight(s):
return ['background-color: yellow']
df.style.apply(highlight, axis=0, subset=[0])
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/Users/paulperry/anaconda/lib/python3.5/site-packages/IPython/core/formatters.py in __call__(self, obj)
341 method = _safe_get_formatter_method(obj, self.print_method)
342 if method is not None:
--> 343 return method()
344 return None
345 else:
/Users/paulperry/anaconda/lib/python3.5/site-packages/pandas/core/style.py in _repr_html_(self)
163 def _repr_html_(self):
164 """Hooks into Jupyter notebook rich display system."""
--> 165 return self.render()
166
167 def _translate(self):
/Users/paulperry/anaconda/lib/python3.5/site-packages/pandas/core/style.py in render(self)
356 the rendered HTML in the notebook.
357 """
--> 358 self._compute()
359 d = self._translate()
360 # filter out empty styles, every cell will have a class
/Users/paulperry/anaconda/lib/python3.5/site-packages/pandas/core/style.py in _compute(self)
422 r = self
423 for func, args, kwargs in self._todo:
--> 424 r = func(self)(*args, **kwargs)
425 return r
426
/Users/paulperry/anaconda/lib/python3.5/site-packages/pandas/core/style.py in _apply(self, func, axis, subset, **kwargs)
429 subset = _non_reducing_slice(subset)
430 if axis is not None:
--> 431 result = self.data.loc[subset].apply(func, axis=axis, **kwargs)
432 else:
433 # like tee
/Users/paulperry/anaconda/lib/python3.5/site-packages/pandas/core/indexing.py in __getitem__(self, key)
1282 def __getitem__(self, key):
1283 if type(key) is tuple:
-> 1284 return self._getitem_tuple(key)
1285 else:
1286 return self._getitem_axis(key, axis=0)
/Users/paulperry/anaconda/lib/python3.5/site-packages/pandas/core/indexing.py in _getitem_tuple(self, tup)
783
784 # no multi-index, so validate all of the indexers
--> 785 self._has_valid_tuple(tup)
786
787 # ugly hack for GH #836
/Users/paulperry/anaconda/lib/python3.5/site-packages/pandas/core/indexing.py in _has_valid_tuple(self, key)
136 if i >= self.obj.ndim:
137 raise IndexingError('Too many indexers')
--> 138 if not self._has_valid_type(k, i):
139 raise ValueError("Location based indexing can only have [%s] "
140 "types" % self._valid_types)
/Users/paulperry/anaconda/lib/python3.5/site-packages/pandas/core/indexing.py in _has_valid_type(self, key, axis)
1365
1366 raise KeyError("None of [%s] are in the [%s]" %
-> 1367 (key, self.obj._get_axis_name(axis)))
1368
1369 return True
KeyError: 'None of [[0]] are in the [columns]'
Out[16]:
<pandas.core.style.Styler at 0x11ae32cf8>
The code appears to be applying it to the columns, not the rows, but passing a single value as the subset renders the table with the correct row highlighted.
df.style.apply(highlight, axis=0, subset=0)
Letter Number
0 A 1 <- in html this row is highlighted yellow
1 B 2
Passing a bad row index (e.g. 3) also correctly fails with 'None of [[3]] are in the [index]'
df.style.apply(highlight, axis=1, subset=['Letter'])
works, butdf.style.apply(highlight, axis=1, subset=['Letter','Number'])
Fails
Expected Output
df.style.apply(highlight, axis=0, subset=[0])
Letter Number
0 A 1 <- in html this row should be highlighted yellow
1 B 2
output of pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Darwin
OS-release: 15.4.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
pandas: 0.18.0
nose: 1.3.7
pip: 8.1.2
setuptools: 20.3
Cython: 0.23.4
numpy: 1.10.4
scipy: 0.17.0
statsmodels: 0.6.1
xarray: None
IPython: 4.1.2
sphinx: 1.3.5
patsy: 0.4.0
dateutil: 2.5.1
pytz: 2016.2
blosc: None
bottleneck: 1.0.0
tables: 3.2.2
numexpr: 2.5
matplotlib: 1.5.1
openpyxl: 2.3.2
xlrd: 0.9.4
xlwt: 1.0.0
xlsxwriter: 0.8.4
lxml: 3.6.0
bs4: 4.4.1
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.12
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.39.0