Skip to content
Prev Previous commit
Next Next commit
Convert frame.round to working doctest
  • Loading branch information
stijnvanhoey committed Feb 1, 2019
commit a05cc9cdee26949b044afb7921f419c794e6a1b6
63 changes: 40 additions & 23 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6902,8 +6902,7 @@ def merge(self, right, how='inner', on=None, left_on=None, right_on=None,
copy=copy, indicator=indicator, validate=validate)

def round(self, decimals=0, *args, **kwargs):
"""
Round a DataFrame to a variable number of decimal places.
"""Round a DataFrame to a variable number of decimal places.

Parameters
----------
Expand All @@ -6928,29 +6927,47 @@ def round(self, decimals=0, *args, **kwargs):

Examples
--------
>>> df = pd.DataFrame(np.random.random([3, 3]),
... columns=['A', 'B', 'C'], index=['first', 'second', 'third'])
>>> df = pd.DataFrame([(.21, .32), (.01, .67), (.66, .03), (.21, .18)],
... columns=['dogs', 'cats'])
>>> df
A B C
first 0.028208 0.992815 0.173891
second 0.038683 0.645646 0.577595
third 0.877076 0.149370 0.491027
>>> df.round(2)
A B C
first 0.03 0.99 0.17
second 0.04 0.65 0.58
third 0.88 0.15 0.49
>>> df.round({'A': 1, 'C': 2})
A B C
first 0.0 0.992815 0.17
second 0.0 0.645646 0.58
third 0.9 0.149370 0.49
>>> decimals = pd.Series([1, 0, 2], index=['A', 'B', 'C'])
dogs cats
0 0.21 0.32
1 0.01 0.67
2 0.66 0.03
3 0.21 0.18

By providing an integer each column is rounded to the same number
of places

>>> df.round(1)
dogs cats
0 0.2 0.3
1 0.0 0.7
2 0.7 0.0
3 0.2 0.2

With a dict, the number of places for specific columns can be
specfified with the column names as key and the number of places as
value

>>> df.round({'dogs': 1, 'cats': 0})
dogs cats
0 0.2 0.0
1 0.0 1.0
2 0.7 0.0
3 0.2 0.0

Using a Series, the number of places for specific columns can be
specfified with the column names as index and the number of places as
value

>>> decimals = pd.Series([0, 1], index=['cats', 'dogs'])
>>> df.round(decimals)
A B C
first 0.0 1 0.17
second 0.0 1 0.58
third 0.9 0 0.49
dogs cats
0 0.2 0.0
1 0.0 1.0
2 0.7 0.0
3 0.2 0.0
"""
from pandas.core.reshape.concat import concat

Expand Down