Sometimes pandas round function does not return the expected results without informing me about the reason. Consider the following example:

import pandas as pd
import numpy as np 

output = pd.DataFrame(index = ['foo'], columns = ['bar'])
output.loc['foo', 'bar'] = 1.2345
output.round(2) 
Out[1]: 
        bar
foo  1.2345

I think the reason for the unexpected result is the type of output (or it's elements?), which is:

In [2]: output.loc[:,'bar']
Out[2]: 
foo    1.2345
Name: bar, dtype: object

To obtain the expected result, I write:

output = output.astype(np.double)
output.round(2) 
Out[3]: 
      bar
foo  1.23

Can somebody maybe say whether the first result is indeed intended? I wonder if pandas.round can warn the user that it does not operate on a dtype: object?

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.5.3.final.0 python-bits: 64 OS: Linux OS-release: 3.13.0-128-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8 pandas: 0.19.2 nose: 1.3.7 pip: 9.0.1 setuptools: 27.2.0 Cython: 0.25.2 numpy: 1.13.1 scipy: 0.19.0 statsmodels: 0.8.0 xarray: None IPython: 5.3.0 sphinx: 1.5.6 patsy: 0.4.1 dateutil: 2.6.1 pytz: 2017.2 blosc: None bottleneck: 1.2.1 tables: 3.3.0 numexpr: 2.6.2 matplotlib: 2.0.2 openpyxl: 2.4.7 xlrd: 1.0.0 xlwt: 1.2.0 xlsxwriter: 0.9.6 lxml: 3.7.3 bs4: 4.6.0 html5lib: 0.999 httplib2: None apiclient: None sqlalchemy: 1.1.9 pymysql: None psycopg2: None jinja2: 2.9.6 boto: 2.46.1 pandas_datareader: 0.2.1

Comment From: gfyoung

@FabianSchuetze : Thanks for reporting this! Rounding with object elements is generally not a good idea because object is very ambiguous Python-wise. In fact, numpy (a pandas dependency) can get very mad if you try to do that. Thus, the result that you see should be expected IMO.

We could maybe issue a warning on a dtype that is not numeric, but we can't just check for object in this case because there are many other types that would fail (think str + non-numeric subclasses). Thus, it might not be as straightforward to do (or maybe I'm overthinking it 😄 )

Comment From: jreback

.round() only operates on int/float columns by-definition. I suppose the doc-string could be slightly improved.

But this is the expected behavior. If you have object dtypes that are in fact numeric you are responsible to conversions. It is not normal, except by explicit user action to have this.