Skip to content
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

ENH: implement FloatingArray.round() #38866

Closed
wants to merge 10 commits into from
8 changes: 8 additions & 0 deletions pandas/core/arrays/floating.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,14 @@ def max(self, *, skipna=True, **kwargs):
nv.validate_max((), kwargs)
return super()._reduce("max", skipna=skipna)

def round(self, decimals=0):
Copy link
Contributor

Choose a reason for hiding this comment

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

should do this generically (and move to NumericArray), e.g. something like

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

where _apply handles the masking & recasting. I think we discussed similar in .to_numeric

Copy link
Contributor

Choose a reason for hiding this comment

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

or event better

round = _apply_function(np.round, decimals=0, "nice doc-string here")

Copy link
Member Author

Choose a reason for hiding this comment

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

If we're going with the second way (may as well) would _apply_function be method on NumericArray (versus a module level function)?

values = self._data[~self._mask]
values = np.round(values, decimals)

data = np.zeros(self._data.shape)
data[~self._mask] = values
return FloatingArray(data, self._mask)

def _maybe_mask_result(self, result, mask, other, op_name: str):
"""
Parameters
Expand Down
20 changes: 14 additions & 6 deletions pandas/tests/series/methods/test_round.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,23 @@ def test_round_numpy_with_nan(self):
expected = Series([2.0, np.nan, 0.0])
tm.assert_series_equal(result, expected)

def test_round_builtin(self):
ser = Series([1.123, 2.123, 3.123], index=range(3))
result = round(ser)
expected_rounded0 = Series([1.0, 2.0, 3.0], index=range(3))
def test_round_builtin(self, any_float_allowed_nullable_dtype):
ser = Series(
[1.123, 2.123, 3.123],
index=range(3),
dtype=any_float_allowed_nullable_dtype,
)
result = round(ser).astype(any_float_allowed_nullable_dtype)
expected_rounded0 = Series(
[1.0, 2.0, 3.0], index=range(3), dtype=any_float_allowed_nullable_dtype
)
tm.assert_series_equal(result, expected_rounded0)

decimals = 2
expected_rounded = Series([1.12, 2.12, 3.12], index=range(3))
result = round(ser, decimals)
expected_rounded = Series(
[1.12, 2.12, 3.12], index=range(3), dtype=any_float_allowed_nullable_dtype
)
result = round(ser, decimals).astype(any_float_allowed_nullable_dtype)
tm.assert_series_equal(result, expected_rounded)

@pytest.mark.parametrize("method", ["round", "floor", "ceil"])
Expand Down