Skip to content

TypeError from to_html on a mixed type column #12704

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

Closed
tdhopper opened this issue Mar 23, 2016 · 7 comments
Closed

TypeError from to_html on a mixed type column #12704

tdhopper opened this issue Mar 23, 2016 · 7 comments

Comments

@tdhopper
Copy link
Contributor

Code Sample, a copy-pastable example if possible

I'm getting an error when running:

pd.DataFrame.from_dict({'a': {0: 1., 2: "A"}}).to_html()

The traceback is:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-114-53996628e131> in <module>()
----> 1 pd.DataFrame.from_dict({'a': {0: 1., 2: "A"}}).to_html()

/Users/tdhopper/miniconda/envs/envname/lib/python2.7/site-packages/pandas/core/frame.pyc in to_html(self, buf, columns, col_space, colSpace, header, index, na_rep, formatters, float_format, sparsify, index_names, justify, bold_rows, classes, escape, max_rows, max_cols, show_dimensions, notebook, decimal)
   1539                                            decimal=decimal)
   1540         # TODO: a generic formatter wld b in DataFrameFormatter
-> 1541         formatter.to_html(classes=classes, notebook=notebook)
   1542 
   1543         if buf is None:

/Users/tdhopper/miniconda/envs/envname/lib/python2.7/site-packages/pandas/core/format.pyc in to_html(self, classes, notebook)
    667                                       notebook=notebook)
    668         if hasattr(self.buf, 'write'):
--> 669             html_renderer.write_result(self.buf)
    670         elif isinstance(self.buf, compat.string_types):
    671             with open(self.buf, 'w') as f:

/Users/tdhopper/miniconda/envs/envname/lib/python2.7/site-packages/pandas/core/format.pyc in write_result(self, buf)
    987         indent += self.indent_delta
    988         indent = self._write_header(indent)
--> 989         indent = self._write_body(indent)
    990 
    991         self.write('</table>', indent)

/Users/tdhopper/miniconda/envs/envname/lib/python2.7/site-packages/pandas/core/format.pyc in _write_body(self, indent)
   1139         fmt_values = {}
   1140         for i in range(min(len(self.columns), self.max_cols)):
-> 1141             fmt_values[i] = self.fmt._format_col(i)
   1142 
   1143         # write values

/Users/tdhopper/miniconda/envs/envname/lib/python2.7/site-packages/pandas/core/format.pyc in _format_col(self, i)
    650         return format_array(frame.iloc[:, i]._values, formatter,
    651                             float_format=self.float_format, na_rep=self.na_rep,
--> 652                             space=self.col_space, decimal=self.decimal)
    653 
    654     def to_html(self, classes=None, notebook=False):

/Users/tdhopper/miniconda/envs/envname/lib/python2.7/site-packages/pandas/core/format.pyc in format_array(values, formatter, float_format, na_rep, digits, space, justify, decimal)
   2004                         space=space, justify=justify, decimal=decimal)
   2005 
-> 2006     return fmt_obj.get_result()
   2007 
   2008 

/Users/tdhopper/miniconda/envs/envname/lib/python2.7/site-packages/pandas/core/format.pyc in get_result(self)
   2023 
   2024     def get_result(self):
-> 2025         fmt_values = self._format_strings()
   2026         return _make_fixed_width(fmt_values, self.justify)
   2027 

/Users/tdhopper/miniconda/envs/envname/lib/python2.7/site-packages/pandas/core/format.pyc in _format_strings(self)
   2064                 fmt_values.append(' %s' % _format(v))
   2065             elif is_float[i]:
-> 2066                 fmt_values.append(float_format(v))
   2067             else:
   2068                 fmt_values.append(' %s' % _format(v))

TypeError: 'str' object is not callable

Expected Output

output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.11.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.18.0
nose: None
pip: 8.1.1
setuptools: 20.3
Cython: None
numpy: 1.10.4
scipy: 0.17.0
statsmodels: None
xarray: None
IPython: 4.1.2
sphinx: None
patsy: None
dateutil: 2.5.0
pytz: 2016.1
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.5.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.12
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None
In [89]:
@jreback
Copy link
Contributor

jreback commented Mar 23, 2016

very strange. works for me, same platform & version

In [1]: pd.DataFrame.from_dict({'a': {0: 1., 2: "A"}}).to_html()
Out[1]: u'<table border="1" class="dataframe">\n  <thead>\n    <tr style="text-align: right;">\n      <th></th>\n      <th>a</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>A</td>\n    </tr>\n  </tbody>\n</table>'

@tdhopper
Copy link
Contributor Author

😬 I can't seem to reproduce it outside of the notebook I'm currently running.

@tdhopper
Copy link
Contributor Author

Restarted my kernel and it works fine.

@jreback
Copy link
Contributor

jreback commented Mar 23, 2016

and the video is worth it!

lmk if it recurs

@jreback jreback closed this as completed Mar 23, 2016
@tdhopper
Copy link
Contributor Author

@jreback: Here's how to reproduce:

import pandas as pd

pd.set_option("float_format", '%.1f')

pd.DataFrame.from_dict({'a': {0: 1., 2: "A"}}).to_html()

Am I using float_format incorrectly?

@jreback
Copy link
Contributor

jreback commented Mar 23, 2016

No it should be a callable. pd.set_option('float_format', lambda x: "%.1f" % x).
so that's (a different) bug

In [1]: pd.describe_option('float_format')
display.float_format : callable
    The callable should accept a floating point number and return
    a string with the desired format of the number. This is used
    in some places like SeriesFormatter.
    See core.format.EngFormatter for an example.
    [default: None] [currently: None]

@jreback
Copy link
Contributor

jreback commented Mar 23, 2016

see #12706

tdhopper added a commit to tdhopper/pandas that referenced this issue Mar 25, 2016
The `float_format` setting takes either None or a callable object
that returns a formatted string given a float value. Currently, the
setting isn't validated, so cryptic error messages are returned
if, for example, a format non-callable format string is given (see
\pandas-dev#12704). Add a standard validation method and use it to validate
the `float_format` setting.
jreback pushed a commit to jreback/pandas that referenced this issue Mar 25, 2016
The `float_format` setting takes either None or a callable object
that returns a formatted string given a float value. Currently, the
setting isn't validated, so cryptic error messages are returned
if, for example, a format non-callable format string is given (see
\pandas-dev#12704). Add a standard validation method and use it to validate
the `float_format` setting.

closes pandas-dev#12706
closes pandas-dev#12711
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants