Skip to content
Prev Previous commit
Next Next commit
move _apply, round to NumericArray
  • Loading branch information
arw2019 committed Jan 4, 2021
commit 85e6d4fdc9323b2ecc0a0ff167997216e8f97a4c
11 changes: 11 additions & 0 deletions pandas/core/arrays/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,14 @@ def _arith_method(self, other, op):
)

return self._maybe_mask_result(result, mask, other, op_name)

def _apply(self, func, **kwargs):
values = self._data[~self._mask]
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 add a doc-string

Copy link
Member

Choose a reason for hiding this comment

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

I don't think you actually need to subset the _data with the mask in this case, as "round" should work on all values, and I can't think of a case where it would error by being called on the "invalid" values hidden by the mask.

Of course, if many values are masked, we might be calculating round on too many values. But doing the filter operation / copy also takes time. Maybe something to time both ways.

values = np.round(values, **kwargs)
Copy link
Contributor

Choose a reason for hiding this comment

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

should be func :->


data = np.zeros(self._data.shape)
data[~self._mask] = values
return type(self)(data, self._mask)
Copy link
Member

Choose a reason for hiding this comment

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

The mask needs to be copied I think? (result should not share a mask with the original array, because otherwise editing one can modify the other. We should probably also test this)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good point (and actually the same bug exists already in my implementation of to_numeric for EAs - #38974). I'll fix this and add tests


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