Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] 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
- Looks like
DataFrame.round
is able to ignore errors and return the existing columns if rounding isn't possible, butSeries.round
is throwing errors, would it be possible to do the same incase of Series too? - rounding on decimals doesn't seem to happen both incase of Series & DataFrame
Expected Behavior
- Consistent behavior with
Series.round
&DataFrame.round
. - The rounding of decimals work should work as shown above.
Installed Versions
INSTALLED VERSIONS
------------------
commit : ba1cccd19da778f0c3a7d6a885685da16a072870
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
Comment From: mroeschke
IMO I think
DataFrame.round
should raise if some columns are not numeric (especially since anumeric_only
keyword does not exist (yet) on this method)- Yes, Decimal rounding should work
Comment From: sharkipelago
take