Styler class fails to render numeric columns when 0 not in columns #12125

Closed
HHammond opened this Issue Jan 24, 2016 · 0 comments

Comments

Projects
None yet
2 participants
Contributor

HHammond commented Jan 24, 2016

The core.style.Styler._translate method uses __getitem__ indexing when working with numerical cell locations (https://github.com/pydata/pandas/blob/master/pandas/core/style.py#L240). When using numeric dataframe columns the location based index causes confusion with the column name index, causing an error.

df = pd.DataFrame({i: range(10) for i in range(1,10)})
df.style._translate()

The expected behaviour should be to always use location based indexing. Changing line 240 to:

    "value": self.data.iloc[r].iloc[c],

Fixes the error.

I do currently have a PR to fix this but wanted to file the bug report first.

Error code
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.5/site-packages/IPython/core/formatters.py in __call__(self, obj)
    339             method = _safe_get_formatter_method(obj, self.print_method)
    340             if method is not None:
--> 341                 return method()
    342             return None
    343         else:

/usr/local/lib/python3.5/site-packages/pandas/core/style.py in _repr_html_(self)
    158         Hooks into Jupyter notebook rich display system.
    159         '''
--> 160         return self.render()
    161 
    162     def _translate(self):

/usr/local/lib/python3.5/site-packages/pandas/core/style.py in render(self)
    259         """
    260         self._compute()
--> 261         d = self._translate()
    262         # filter out empty styles, every cell will have a class
    263         # but the list of props may just be [['', '']].

/usr/local/lib/python3.5/site-packages/pandas/core/style.py in _translate(self)
    220                 cs = [DATA_CLASS, "row%s" % r, "col%s" % c]
    221                 cs.extend(cell_context.get("data", {}).get(r, {}).get(c, []))
--> 222                 row_es.append({"type": "td", "value": self.data.iloc[r][c],
    223                                "class": " ".join(cs), "id": "_".join(cs[1:])})
    224                 props = []

/usr/local/lib/python3.5/site-packages/pandas/core/series.py in __getitem__(self, key)
    555     def __getitem__(self, key):
    556         try:
--> 557             result = self.index.get_value(self, key)
    558 
    559             if not np.isscalar(result):

/usr/local/lib/python3.5/site-packages/pandas/core/index.py in get_value(self, series, key)
   1788 
   1789         try:
-> 1790             return self._engine.get_value(s, k)
   1791         except KeyError as e1:
   1792             if len(self) > 0 and self.inferred_type in ['integer','boolean']:

pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:3204)()

pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:2903)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3843)()

pandas/hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6525)()

pandas/hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6463)()

KeyError: 0

INSTALLED VERSIONS

commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Darwin
OS-release: 15.3.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.17.1
nose: 1.3.7
pip: 7.1.2
setuptools: 19.4
Cython: None
numpy: 1.10.4
scipy: 0.16.0
statsmodels: None
IPython: 4.0.3
sphinx: 1.3.1
patsy: None
dateutil: 2.4.2
pytz: 2015.7
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.4.3
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
Jinja2: 2.8

jreback added this to the 0.18.0 milestone Jan 24, 2016

@TomAugspurger TomAugspurger added a commit to TomAugspurger/pandas that referenced this issue Feb 7, 2016

@TomAugspurger TomAugspurger ENH: display_format for style
Closes pandas-dev#11692
Closes pandas-dev#12134
Closes pandas-dev#12125

This adds a `.format` method to Styler for formatting the display value
(the actual text) of each scalar value.

In the processes of cleaning up the template, I close #12134 (spurious 0)
and #12125 (KeyError from using iloc improperly)

cherry pick test from #12126

only allow str formatting for now

fix tests for new spec

formatter callable

update notebook
aee0d90

@TomAugspurger TomAugspurger added a commit to TomAugspurger/pandas that referenced this issue Feb 12, 2016

@TomAugspurger TomAugspurger ENH: display_format for style
Closes pandas-dev#11692
Closes pandas-dev#12134
Closes pandas-dev#12125

This adds a `.format` method to Styler for formatting the display value
(the actual text) of each scalar value.

In the processes of cleaning up the template, I close #12134 (spurious 0)
and #12125 (KeyError from using iloc improperly)

cherry pick test from #12126

only allow str formatting for now

fix tests for new spec

formatter callable

update notebook
a3c38fe

jreback closed this in cf8b7f8 Feb 12, 2016

@HHammond HHammond added a commit to HHammond/pandas that referenced this issue Feb 13, 2016

@TomAugspurger @HHammond TomAugspurger + HHammond ENH: display_format for style
Closes pandas-dev#11692
Closes pandas-dev#12134
Closes pandas-dev#12125

This adds a `.format` method to Styler for formatting the display value
(the actual text) of each scalar value.

In the processes of cleaning up the template, I close #12134 (spurious 0)
and #12125 (KeyError from using iloc improperly)

cherry pick test from #12126

only allow str formatting for now

fix tests for new spec

formatter callable

update notebook
a15248a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment