From 7ce2677e09a73d8eae4eb92e0a44c433f2dce041 Mon Sep 17 00:00:00 2001 From: Fangchen Li Date: Mon, 3 May 2021 21:46:22 -0500 Subject: [PATCH 1/3] COMPAT: frame round error msg for py310 --- pandas/core/frame.py | 13 ++++++++++--- pandas/tests/frame/methods/test_round.py | 5 +---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 8e12a8cb18b68..a188305f0ea30 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -9199,9 +9199,16 @@ def _series_round(s, decimals): nv.validate_round(args, kwargs) if isinstance(decimals, (dict, Series)): - if isinstance(decimals, Series): - if not decimals.index.is_unique: - raise ValueError("Index of decimals must be unique") + if isinstance(decimals, Series) and not decimals.index.is_unique: + raise ValueError("Index of decimals must be unique") + if ( + isinstance(decimals, dict) + and not all((is_integer(value) for value in decimals.values())) + ) or ( + isinstance(decimals, Series) + and not all(is_integer(value) for value in decimals.values) + ): + raise TypeError("Values in decimals must be integers") new_cols = list(_dict_round(self, decimals)) elif is_integer(decimals): # Dispatch to Series.round diff --git a/pandas/tests/frame/methods/test_round.py b/pandas/tests/frame/methods/test_round.py index ebe33922be541..dd9206940bcd6 100644 --- a/pandas/tests/frame/methods/test_round.py +++ b/pandas/tests/frame/methods/test_round.py @@ -62,13 +62,12 @@ def test_round(self): # float input to `decimals` non_int_round_dict = {"col1": 1, "col2": 0.5} - msg = "integer argument expected, got float" + msg = "Values in decimals must be integers" with pytest.raises(TypeError, match=msg): df.round(non_int_round_dict) # String input non_int_round_dict = {"col1": 1, "col2": "foo"} - msg = r"an integer is required \(got type str\)" with pytest.raises(TypeError, match=msg): df.round(non_int_round_dict) @@ -78,7 +77,6 @@ def test_round(self): # List input non_int_round_dict = {"col1": 1, "col2": [1, 2]} - msg = r"an integer is required \(got type list\)" with pytest.raises(TypeError, match=msg): df.round(non_int_round_dict) @@ -106,7 +104,6 @@ def test_round(self): # nan in Series round nan_round_Series = Series({"col1": np.nan, "col2": 1}) - msg = "integer argument expected, got float" with pytest.raises(TypeError, match=msg): df.round(nan_round_Series) From c6436187e847fb5ebea9b7648db9fd7ae1dce385 Mon Sep 17 00:00:00 2001 From: Fangchen Li Date: Mon, 3 May 2021 21:54:19 -0500 Subject: [PATCH 2/3] COMPAT: frame round error msg for py310 --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index a188305f0ea30..d383103d3a11b 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -9203,7 +9203,7 @@ def _series_round(s, decimals): raise ValueError("Index of decimals must be unique") if ( isinstance(decimals, dict) - and not all((is_integer(value) for value in decimals.values())) + and not all(is_integer(value) for value in decimals.values()) ) or ( isinstance(decimals, Series) and not all(is_integer(value) for value in decimals.values) From 908c18968083f7daa25307f21cf0926031c57fb6 Mon Sep 17 00:00:00 2001 From: Fangchen Li Date: Wed, 5 May 2021 20:02:57 -0500 Subject: [PATCH 3/3] use is_dick_like --- pandas/core/frame.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 7a69ae79eb144..d1ff69f16d993 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -9208,12 +9208,8 @@ def _series_round(s, decimals): if isinstance(decimals, (dict, Series)): if isinstance(decimals, Series) and not decimals.index.is_unique: raise ValueError("Index of decimals must be unique") - if ( - isinstance(decimals, dict) - and not all(is_integer(value) for value in decimals.values()) - ) or ( - isinstance(decimals, Series) - and not all(is_integer(value) for value in decimals.values) + if is_dict_like(decimals) and not all( + is_integer(value) for _, value in decimals.items() ): raise TypeError("Values in decimals must be integers") new_cols = list(_dict_round(self, decimals))