I was trying to use the round method on a dataframe and was getting some columns not rounded. This example replicates it:
0 1
0 163.339996338 55.178787875
1 166.788757324 47.98198625
2 199.475143433 64.40626575
3 257.137786865 88.855215375
4 248.984573364 86.0543125
df.dtypes:
0 float32
1 float64
dtype: object
df.round(3)
0 163.339996338 55.179
1 166.789001465 47.982
2 199.475006104 64.406
3 257.138000488 88.855
4 248.98500061 86.054
I'm not sure if this is being caused only by the different types for each column, but if I convert column 0 to a float64
the rounding is applied to both columns.
Comment From: jorisvandenbossche
Can you show pd.show_versions()
? This is working correctly for me on master and with 0.17.1?
Code snippet to test
df = pd.DataFrame({'a': np.random.randn(3).astype('float32'), 'b': np.random.randn(3)})
df.dtypes
df.round(2)
Comment From: stoffprof
INSTALLED VERSIONS commit: None python: 2.7.9.final.0 python-bits: 64 OS: Windows OS-release: 7 machine: AMD64 processor: Intel64 Family 6 Model 58 Stepping 9, GenuineIntel byteorder: little LC_ALL: None LANG: None
pandas: 0.17.1 nose: 1.3.4 pip: 7.1.2 setuptools: 18.7.1 Cython: None numpy: 1.9.2 scipy: 0.16.1 statsmodels: 0.6.1 IPython: 2.4.1 sphinx: None patsy: 0.4.0 dateutil: 2.4.2 pytz: 2014.9 blosc: None bottleneck: None tables: 3.2.2 numexpr: 2.4 matplotlib: 1.5.0 openpyxl: 2.3.1 xlrd: 0.9.4 xlwt: None xlsxwriter: None lxml: 3.4.4 bs4: 4.3.2 html5lib: 0.999 httplib2: None apiclient: None sqlalchemy: None pymysql: None psycopg2: None Jinja2: None
Comment From: stoffprof
Your code snippet does work for me. I'm attaching a small pickled version of the dataframe that is giving me trouble. Perhaps there's something else going on? (I changed the extension to .txt so it would upload, but it is pickled.) df.txt
Comment From: kawochen
@itzybitzy your pickle rounds correctly for me. Actually even your pasted output is correct
Comment From: stoffprof
Whoa. That is very weird. I restarted my kernel, and loaded the pickle file that I uploaded. I still get the strange result:
Comment From: kawochen
that's what I got as well -- they are rounded!
In [0]: np.float32(98.24)
Out[0]: 98.239998
Comment From: stoffprof
OK, well now I feel dumb. I wasn't looking closely at the digits, just saw that there were more than 3 of them. I see now that it's just a display problem. Thanks, and sorry to waste your time.
Comment From: jorisvandenbossche
@itzybitzy No problem, I also hadn't noticed it ;-)