Skip to content

ENH: Using built-in round on a series #11763 #11809

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

Merged
merged 1 commit into from
Dec 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v0.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Other enhancements
- Handle truncated floats in SAS xport files (:issue:`11713`)
- Added option to hide index in ``Series.to_string`` (:issue:`11729`)
- ``read_excel`` now supports s3 urls of the format ``s3://bucketname/filename`` (:issue:`11447`)
- A simple version of ``Panel.round()`` is now implemented (:issue:`11763`)
- For Python 3.x, ``round(DataFrame)``, ``round(Series)``, ``round(Panel)`` will work (:issue:`11763`)

.. _whatsnew_0180.enhancements.rounding:

Expand Down Expand Up @@ -90,6 +92,8 @@ In addition, ``.round()`` will be available thru the ``.dt`` accessor of ``Serie
Backwards incompatible API changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- The parameter ``out`` has been removed from the ``Series.round()`` method. (:issue:`11763`)

Bug in QuarterBegin with n=0
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
10 changes: 7 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4376,13 +4376,17 @@ def round(self, decimals=0, out=None):
Returns
-------
DataFrame object

See Also
--------
numpy.around
"""
from pandas.tools.merge import concat

def _dict_round(df, decimals):
for col, vals in df.iteritems():
try:
yield np.round(vals, decimals[col])
yield vals.round(decimals[col])
except KeyError:
yield vals

Expand All @@ -4392,8 +4396,8 @@ def _dict_round(df, decimals):
raise ValueError("Index of decimals must be unique")
new_cols = [col for col in _dict_round(self, decimals)]
elif com.is_integer(decimals):
# Dispatch to numpy.round
new_cols = [np.round(v, decimals) for _, v in self.iteritems()]
# Dispatch to Series.round
new_cols = [v.round(decimals) for _, v in self.iteritems()]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is fine, but not really necessary as these do the same thing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this change was needed to get rid of the out argument. If DataFrame.round() calls np.round(), then np.round() dispatches to Series.round(), passing the out argument.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

else:
raise TypeError("decimals must be an integer, a dict-like or a Series")

Expand Down
5 changes: 4 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,10 @@ def bool(self):

def __abs__(self):
return self.abs()


def __round__(self,decimals=0):
return self.round(decimals)

#----------------------------------------------------------------------
# Array Interface

Expand Down
27 changes: 27 additions & 0 deletions pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,33 @@ def head(self, n=5):

def tail(self, n=5):
raise NotImplementedError

def round(self, decimals=0):
"""
Round each value in Panel to a specified number of decimal places.

.. versionadded:: 0.18.0

Parameters
----------
decimals : int
Number of decimal places to round to (default: 0).
If decimals is negative, it specifies the number of
positions to the left of the decimal point.

Returns
-------
Panel object

See Also
--------
numpy.around
"""
if com.is_integer(decimals):
result = np.apply_along_axis(np.round, 0, self.values)
return self._wrap_result(result, axis=0)
raise TypeError("decimals must be an integer")


def _needs_reindex_multi(self, axes, method, level):
""" don't allow a multi reindex on Panel or above ndim """
Expand Down
26 changes: 20 additions & 6 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1235,15 +1235,29 @@ def idxmax(self, axis=None, out=None, skipna=True):
argmin = idxmin
argmax = idxmax

@Appender(np.ndarray.round.__doc__)
def round(self, decimals=0, out=None):
def round(self, decimals=0):
"""
Round each value in a Series to the given number of decimals.

Parameters
----------
decimals : int
Number of decimal places to round to (default: 0).
If decimals is negative, it specifies the number of
positions to the left of the decimal point.

Returns
-------
Series object

See Also
--------
numpy.around

"""
result = _values_from_object(self).round(decimals, out=out)
if out is None:
result = self._constructor(result,
index=self.index).__finalize__(self)
result = _values_from_object(self).round(decimals)
result = self._constructor(result,
index=self.index).__finalize__(self)

return result

Expand Down
124 changes: 1 addition & 123 deletions pandas/tests/test_format.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
from __future__ import print_function
from distutils.version import LooseVersion
import re
Expand Down Expand Up @@ -2969,128 +2969,6 @@ def test_to_csv_engine_kw_deprecation(self):
df = DataFrame({'col1' : [1], 'col2' : ['a'], 'col3' : [10.1] })
df.to_csv(engine='python')

def test_round_dataframe(self):

# GH 2665

# Test that rounding an empty DataFrame does nothing
df = DataFrame()
tm.assert_frame_equal(df, df.round())

# Here's the test frame we'll be working with
df = DataFrame(
{'col1': [1.123, 2.123, 3.123], 'col2': [1.234, 2.234, 3.234]})

# Default round to integer (i.e. decimals=0)
expected_rounded = DataFrame(
{'col1': [1., 2., 3.], 'col2': [1., 2., 3.]})
tm.assert_frame_equal(df.round(), expected_rounded)

# Round with an integer
decimals = 2
expected_rounded = DataFrame(
{'col1': [1.12, 2.12, 3.12], 'col2': [1.23, 2.23, 3.23]})
tm.assert_frame_equal(df.round(decimals), expected_rounded)

# This should also work with np.round (since np.round dispatches to
# df.round)
tm.assert_frame_equal(np.round(df, decimals), expected_rounded)

# Round with a list
round_list = [1, 2]
with self.assertRaises(TypeError):
df.round(round_list)

# Round with a dictionary
expected_rounded = DataFrame(
{'col1': [1.1, 2.1, 3.1], 'col2': [1.23, 2.23, 3.23]})
round_dict = {'col1': 1, 'col2': 2}
tm.assert_frame_equal(df.round(round_dict), expected_rounded)

# Incomplete dict
expected_partially_rounded = DataFrame(
{'col1': [1.123, 2.123, 3.123], 'col2': [1.2, 2.2, 3.2]})
partial_round_dict = {'col2': 1}
tm.assert_frame_equal(
df.round(partial_round_dict), expected_partially_rounded)

# Dict with unknown elements
wrong_round_dict = {'col3': 2, 'col2': 1}
tm.assert_frame_equal(
df.round(wrong_round_dict), expected_partially_rounded)

# float input to `decimals`
non_int_round_dict = {'col1': 1, 'col2': 0.5}
if sys.version < LooseVersion('2.7'):
# np.round([1.123, 2.123], 0.5) is only a warning in Python 2.6
with self.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
df.round(non_int_round_dict)
else:
with self.assertRaises(TypeError):
df.round(non_int_round_dict)

# String input
non_int_round_dict = {'col1': 1, 'col2': 'foo'}
with self.assertRaises(TypeError):
df.round(non_int_round_dict)

non_int_round_Series = Series(non_int_round_dict)
with self.assertRaises(TypeError):
df.round(non_int_round_Series)

# List input
non_int_round_dict = {'col1': 1, 'col2': [1, 2]}
with self.assertRaises(TypeError):
df.round(non_int_round_dict)

non_int_round_Series = Series(non_int_round_dict)
with self.assertRaises(TypeError):
df.round(non_int_round_Series)

# Non integer Series inputs
non_int_round_Series = Series(non_int_round_dict)
with self.assertRaises(TypeError):
df.round(non_int_round_Series)

non_int_round_Series = Series(non_int_round_dict)
with self.assertRaises(TypeError):
df.round(non_int_round_Series)

# Negative numbers
negative_round_dict = {'col1': -1, 'col2': -2}
big_df = df * 100
expected_neg_rounded = DataFrame(
{'col1':[110., 210, 310], 'col2':[100., 200, 300]})
tm.assert_frame_equal(
big_df.round(negative_round_dict), expected_neg_rounded)

# nan in Series round
nan_round_Series = Series({'col1': nan, 'col2':1})
expected_nan_round = DataFrame(
{'col1': [1.123, 2.123, 3.123], 'col2': [1.2, 2.2, 3.2]})
if sys.version < LooseVersion('2.7'):
# Rounding with decimal is a ValueError in Python < 2.7
with self.assertRaises(ValueError):
df.round(nan_round_Series)
else:
with self.assertRaises(TypeError):
df.round(nan_round_Series)

# Make sure this doesn't break existing Series.round
tm.assert_series_equal(df['col1'].round(1), expected_rounded['col1'])

def test_round_issue(self):
# GH11611

df = pd.DataFrame(np.random.random([3, 3]), columns=['A', 'B', 'C'],
index=['first', 'second', 'third'])

dfs = pd.concat((df, df), axis=1)
rounded = dfs.round()
self.assertTrue(rounded.index.equals(dfs.index))

decimals = pd.Series([1, 0, 2], index=['A', 'B', 'A'])
self.assertRaises(ValueError, df.round, decimals)

class TestSeriesFormatting(tm.TestCase):
_multiprocess_can_split_ = True
Expand Down
Loading