Skip to content

COMPAT: frame round error msg for py310 #41301

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 4 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 10 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Copy link
Contributor

Choose a reason for hiding this comment

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

can you check with is_dict_like and use .items() here?

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
Expand Down
5 changes: 1 addition & 4 deletions pandas/tests/frame/methods/test_round.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down