Skip to content

BUG: round not functioning as expected #55114

Open
@galipremsagar

Description

@galipremsagar

Pandas version checks

  • I have checked that this issue has not already been reported.

    I have confirmed this bug exists on the latest version of pandas.

    I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

In [2]: import pandas as pd
In [3]: pd.__version__
Out[3]: '2.1.0'
In [4]: import decimal
In [6]: import numpy as np
In [7]: pdf = pd.DataFrame(
   ...:         {
   ...:             "a": [1.2234242333234, 323432.3243423, np.nan],
   ...:             "b": ["a", "b", "c"],
   ...:             "c": pd.Series([34224, 324324, 324342], dtype="datetime64[ns]"),
   ...:             "d": pd.Series([224.242, None, 2424.234324], dtype="category"),
   ...:             "e": [
   ...:                 decimal.Decimal("342.3243234234242"),
   ...:                 decimal.Decimal("89.32432497687622"),
   ...:                 None,
   ...:             ],
   ...:         }
   ...:     )
In [8]: round(pdf, 2)
Out[8]: 
           a  b                             c            d                  e
0       1.22  a 1970-01-01 00:00:00.000034224   224.242000  342.3243234234242
1  323432.32  b 1970-01-01 00:00:00.000324324          NaN  89.32432497687622
2        NaN  c 1970-01-01 00:00:00.000324342  2424.234324               None
In [9]: round(pdf.a, 2)
Out[9]: 
0         1.22
1    323432.32
2          NaN
Name: a, dtype: float64
In [10]: round(pdf.b, 2)
Traceback (most recent call last):
  File "/Users/pgali/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3378, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-10-9c75601ee514>", line 1, in <module>
    round(pdf.b, 2)
  File "/Users/pgali/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/pandas/core/generic.py", line 1663, in __round__
    return self.round(decimals).__finalize__(self, method="__round__")
  File "/Users/pgali/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/pandas/core/series.py", line 2688, in round
    result = self._values.round(decimals)
TypeError: can't multiply sequence by non-int of type 'float'
In [11]: round(pdf.c, 2)
Traceback (most recent call last):
  File "/Users/pgali/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3378, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-11-6d1f4094de81>", line 1, in <module>
    round(pdf.c, 2)
  File "/Users/pgali/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/pandas/core/generic.py", line 1663, in __round__
    return self.round(decimals).__finalize__(self, method="__round__")
  File "/Users/pgali/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/pandas/core/series.py", line 2688, in round
    result = self._values.round(decimals)
  File "/Users/pgali/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/pandas/core/arrays/datetimelike.py", line 2140, in round
    return self._round(freq, RoundTo.NEAREST_HALF_EVEN, ambiguous, nonexistent)
  File "/Users/pgali/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/pandas/core/arrays/datetimelike.py", line 2122, in _round
    offset = to_offset(freq)
  File "offsets.pyx", line 4460, in pandas._libs.tslibs.offsets.to_offset
  File "offsets.pyx", line 4562, in pandas._libs.tslibs.offsets.to_offset
ValueError: Invalid frequency: 2
In [12]: round(pdf.d, 2)
Traceback (most recent call last):
  File "/Users/pgali/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3378, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-12-9fba030eaf35>", line 1, in <module>
    round(pdf.d, 2)
  File "/Users/pgali/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/pandas/core/generic.py", line 1663, in __round__
    return self.round(decimals).__finalize__(self, method="__round__")
  File "/Users/pgali/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/pandas/core/series.py", line 2688, in round
    result = self._values.round(decimals)
AttributeError: 'Categorical' object has no attribute 'round'
In [13]: round(pdf.e, 2)
Traceback (most recent call last):
  File "/Users/pgali/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3378, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-13-d91def30b1e6>", line 1, in <module>
    round(pdf.e, 2)
  File "/Users/pgali/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/pandas/core/generic.py", line 1663, in __round__
    return self.round(decimals).__finalize__(self, method="__round__")
  File "/Users/pgali/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/pandas/core/series.py", line 2688, in round
    result = self._values.round(decimals)
TypeError: unsupported operand type(s) for *: 'decimal.Decimal' and 'float'


In [4]: round(decimal.Decimal("23234.323224"), 2)
Out[4]: Decimal('23234.32')

Issue Description

  1. Looks like DataFrame.round is able to ignore errors and return the existing columns if rounding isn't possible, but Series.round is throwing errors, would it be possible to do the same incase of Series too?
  2. rounding on decimals doesn't seem to happen both incase of Series & DataFrame

Expected Behavior

  1. Consistent behavior with Series.round & DataFrame.round.
  2. The rounding of decimals work should work as shown above.

Installed Versions

INSTALLED VERSIONS

commit : ba1cccd
python : 3.10.2.final.0
python-bits : 64
OS : Darwin
OS-release : 22.6.0
Version : Darwin Kernel Version 22.6.0: Wed Jul 5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 2.1.0
numpy : 1.23.2
pytz : 2022.2.1
dateutil : 2.8.2
setuptools : 60.2.0
pip : 21.3.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.5.0
pandas_datareader : None
bs4 : None
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : 2.8.4
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 11.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Activity

mroeschke

mroeschke commented on Sep 13, 2023

@mroeschke
Member

IMO I think

  1. DataFrame.round should raise if some columns are not numeric (especially since a numeric_only keyword does not exist (yet) on this method)
  2. Yes, Decimal rounding should work
added
DataFrameDataFrame data structure
and removed
Needs TriageIssue that has not been reviewed by a pandas team member
on Sep 13, 2023
sharkipelago

sharkipelago commented on Jun 10, 2025

@sharkipelago
Contributor

take

sharkipelago

sharkipelago commented on Jun 17, 2025

@sharkipelago
Contributor

I checked on how pandas handles things now for the stuff above and found out the following (also I could not figure out how to format it with the In[0]/Out[0] like the reproducible example above so sorry, its a little messy):

import pandas as pd
pd.__version__
3.0.0.dev0+2124.gcfe54bd5da

import decimal
import numpy as np
df = pd.DataFrame(
	{
		"a": [1.2234242333234, 323432.3243423, np.nan],
		"b": ["a", "b", "c"],
		"c": pd.Series([34224, 324324, 324342], dtype="datetime64[ns]"),
		"d": pd.Series([224.242, None, 2424.234324], dtype="category"),
		"e": [
			decimal.Decimal("342.3243234234242"),
			decimal.Decimal("89.32432497687622"),
			None,
		],
	}
  )


round(df, 2)
           a  b                             c            d                  e
0       1.22  a 1970-01-01 00:00:00.000034224   224.242000  342.3243234234242
1  323432.32  b 1970-01-01 00:00:00.000324324          NaN  89.32432497687622
2        NaN  c 1970-01-01 00:00:00.000324342  2424.234324               None


round(df.a, 2)
0         1.22
1    323432.32
2          NaN
Name: a, dtype: float64


round(df.b, 2)
TypeError                                 Traceback (most recent call last)
Cell In[27], [line 1](vscode-notebook-cell:?execution_count=27&line=1)
----> [1](vscode-notebook-cell:?execution_count=27&line=1) round(df.b, 2)

File ~/pandas-sharkipelago/pandas/core/generic.py:1583, in NDFrame.__round__(self, decimals)
   1581 @final
   1582 def __round__(self, decimals: int = 0) -> Self:
-> [1583](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/baozi/italy/~/pandas-sharkipelago/pandas/core/generic.py:1583)     return self.round(decimals).__finalize__(self, method="__round__")

File ~/pandas-sharkipelago/pandas/core/series.py:2518, in Series.round(self, decimals, *args, **kwargs)
   2516 nv.validate_round(args, kwargs)
   2517 if self.dtype == "object":
-> [2518](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/baozi/italy/~/pandas-sharkipelago/pandas/core/series.py:2518)     raise TypeError("Expected numeric dtype, got object instead.")
   2519 new_mgr = self._mgr.round(decimals=decimals)
   2520 return self._constructor_from_mgr(new_mgr, axes=new_mgr.axes).__finalize__(
   2521     self, method="round"
   2522 )

TypeError: Expected numeric dtype, got object instead.


round(df.c, 2)
0   1970-01-01 00:00:00.000034224
1   1970-01-01 00:00:00.000324324
2   1970-01-01 00:00:00.000324342
Name: c, dtype: datetime64[ns]


round(df.d, 2)
0     224.242000
1            NaN
2    2424.234324
Name: d, dtype: category
Categories (2, float64): [224.242000, 2424.234324]


round(df.e, 2)
TypeError                                 Traceback (most recent call last)
Cell In[30], [line 1](vscode-notebook-cell:?execution_count=30&line=1)
----> [1](vscode-notebook-cell:?execution_count=30&line=1) round(df.e, 2)

File ~/pandas-sharkipelago/pandas/core/generic.py:1583, in NDFrame.__round__(self, decimals)
   1581 @final
   1582 def __round__(self, decimals: int = 0) -> Self:
-> [1583](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/baozi/italy/~/pandas-sharkipelago/pandas/core/generic.py:1583)     return self.round(decimals).__finalize__(self, method="__round__")

File ~/pandas-sharkipelago/pandas/core/series.py:2518, in Series.round(self, decimals, *args, **kwargs)
   2516 nv.validate_round(args, kwargs)
   2517 if self.dtype == "object":
-> [2518](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/baozi/italy/~/pandas-sharkipelago/pandas/core/series.py:2518)     raise TypeError("Expected numeric dtype, got object instead.")
   2519 new_mgr = self._mgr.round(decimals=decimals)
   2520 return self._constructor_from_mgr(new_mgr, axes=new_mgr.axes).__finalize__(
   2521     self, method="round"
   2522 )

TypeError: Expected numeric dtype, got object instead.

So, I think the entire DataFrame and column A and B are pretty much the same (except the error message is different). Column E should not raise an error which is one of the main fixes that was discussed above. But I had the following questions about the other columns.

  1. Should column C raise an error as it's a datetime?
  2. Should column D raise an error as it's a category? (Because right now it looks like it's not actually even rounding the number?)
  3. I guess my understanding is that the final result of the bugfix should be calling round() on the DataFrame and Columns B, C, and D raise errors, as opposed to Columns A and E which are valid operations. Is this correct?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

BugDataFrameDataFrame data structure

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Participants

    @mroeschke@galipremsagar@sharkipelago

    Issue actions

      BUG: round not functioning as expected · Issue #55114 · pandas-dev/pandas