Round each value in a Series to the given number of decimals. Parameters ---------- decimals : int, default 0 Number of decimal places to round to. If decimals is negative, it specifies the number of positions to the left of the decimal point
(self, decimals: int = 0, *args, **kwargs)
| 2546 | return self.index[iloc] |
| 2547 | |
| 2548 | def round(self, decimals: int = 0, *args, **kwargs) -> Series: |
| 2549 | """ |
| 2550 | Round each value in a Series to the given number of decimals. |
| 2551 | |
| 2552 | Parameters |
| 2553 | ---------- |
| 2554 | decimals : int, default 0 |
| 2555 | Number of decimal places to round to. If decimals is negative, |
| 2556 | it specifies the number of positions to the left of the decimal point. |
| 2557 | *args, **kwargs |
| 2558 | Additional arguments and keywords have no effect but might be |
| 2559 | accepted for compatibility with NumPy. |
| 2560 | |
| 2561 | Returns |
| 2562 | ------- |
| 2563 | Series |
| 2564 | Rounded values of the Series. |
| 2565 | |
| 2566 | See Also |
| 2567 | -------- |
| 2568 | numpy.around : Round values of an np.array. |
| 2569 | DataFrame.round : Round values of a DataFrame. |
| 2570 | Series.dt.round : Round values of data to the specified freq. |
| 2571 | |
| 2572 | Notes |
| 2573 | ----- |
| 2574 | For values exactly halfway between rounded decimal values, pandas rounds |
| 2575 | to the nearest even value (e.g. -0.5 and 0.5 round to 0.0, 1.5 and 2.5 |
| 2576 | round to 2.0, etc.). |
| 2577 | |
| 2578 | Examples |
| 2579 | -------- |
| 2580 | >>> s = pd.Series([-0.5, 0.1, 2.5, 1.3, 2.7]) |
| 2581 | >>> s.round() |
| 2582 | 0 -0.0 |
| 2583 | 1 0.0 |
| 2584 | 2 2.0 |
| 2585 | 3 1.0 |
| 2586 | 4 3.0 |
| 2587 | dtype: float64 |
| 2588 | """ |
| 2589 | |
| 2590 | nv.validate_round(args, kwargs) |
| 2591 | |
| 2592 | if len(self) == 0: |
| 2593 | return self.copy() |
| 2594 | |
| 2595 | if is_object_dtype(self.dtype): |
| 2596 | values = self._values |
| 2597 | result = lib.map_infer(values, lambda x: round(x, decimals), convert=False) |
| 2598 | return self._constructor(result, index=self.index, copy=False).__finalize__( |
| 2599 | self, method="round" |
| 2600 | ) |
| 2601 | new_mgr = self._mgr.round(decimals=decimals) |
| 2602 | return self._constructor_from_mgr(new_mgr, axes=new_mgr.axes).__finalize__( |
| 2603 | self, method="round" |
| 2604 | ) |
| 2605 |