doctestprinter.round_collections¶
-
doctestprinter.
round_collections
(item_to_round: Any, digits: int = 3) → Any¶ Rounds items within collections (dict, list, tuple). This method also supports object, which implements a round(digits) method.
- Parameters
item_to_round (Any) – An item with the potential to be rounded. If the item cannot be rounded it will be returned instead.
digits (int) – Remaining digits of the rounded number. Default is 3.
- Returns
Any
Examples
>>> sample_dict = {"a_number": 1.234567, "not": "a number"} >>> round_collections(item_to_round=sample_dict, digits=4) {'a_number': 1.2346, 'not': 'a number'} >>> sample_list = [1.234567, "not a number"] >>> round_collections(item_to_round=sample_list, digits=4) [1.2346, 'not a number'] >>> sample_tuple = (1.234567, "not a number") >>> round_collections(item_to_round=sample_tuple, digits=4) (1.2346, 'not a number')
Invoking the round(digit) method of objects.
>>> import numpy >>> sample_array = numpy.array([1.234567]) >>> round_collections(item_to_round=sample_array, digits=4) array([1.2346]) >>> from pandas import Series >>> sample_series = Series([1.234567]) >>> round_collections(item_to_round=sample_series, digits=4) 0 1.2346 dtype: float64 >>> from pandas import DataFrame >>> sample_frame = DataFrame([[1.234567, "not a number"]]) >>> round_collections(item_to_round=sample_frame, digits=4) 0 1 0 1.2346 not a number
Or just do nothing.
>>> round_collections(item_to_round="nothing at all", digits=4) 'nothing at all'