This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
feat: Add DataFrame.round method #1742
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4788,6 +4788,83 @@ def merge( | |
| """ | ||
| raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE) | ||
|
|
||
| def round(self, decimals): | ||
| """ | ||
| Round a DataFrame to a variable number of decimal places. | ||
|
|
||
| **Examples:** | ||
|
|
||
| >>> import bigframes.pandas as bpd | ||
| >>> bpd.options.display.progress_bar = None | ||
| >>> df = bpd.DataFrame([(.21, .32), (.01, .67), (.66, .03), (.21, .18)], | ||
| ... columns=['dogs', 'cats']) | ||
| >>> df | ||
| dogs cats | ||
| 0 0.21 0.32 | ||
| 1 0.01 0.67 | ||
| 2 0.66 0.03 | ||
| 3 0.21 0.18 | ||
| <BLANKLINE> | ||
| [4 rows x 2 columns] | ||
|
|
||
| By providing an integer each column is rounded to the same number | ||
| of decimal places | ||
|
|
||
| >>> df.round(1) | ||
| dogs cats | ||
| 0 0.2 0.3 | ||
| 1 0.0 0.7 | ||
| 2 0.7 0.0 | ||
| 3 0.2 0.2 | ||
| <BLANKLINE> | ||
| [4 rows x 2 columns] | ||
|
|
||
| With a dict, the number of places for specific columns can be | ||
| specified with the column names as key and the number of decimal | ||
| places as value | ||
|
|
||
| >>> df.round({'dogs': 1, 'cats': 0}) | ||
| dogs cats | ||
| 0 0.2 0.0 | ||
| 1 0.0 1.0 | ||
| 2 0.7 0.0 | ||
| 3 0.2 0.0 | ||
| <BLANKLINE> | ||
| [4 rows x 2 columns] | ||
|
|
||
| Using a Series, the number of places for specific columns can be | ||
| specified with the column names as index and the number of | ||
| decimal places as value | ||
|
|
||
| >>> decimals = pd.Series([0, 1], index=['cats', 'dogs']) | ||
| >>> df.round(decimals) | ||
| dogs cats | ||
| 0 0.2 0.0 | ||
| 1 0.0 1.0 | ||
| 2 0.7 0.0 | ||
| 3 0.2 0.0 | ||
| <BLANKLINE> | ||
| [4 rows x 2 columns] | ||
|
|
||
| Args: | ||
| decimals (int, dict, Series): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIUC, it should be
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be either, really |
||
| Number of decimal places to round each column to. If an int is | ||
| given, round each column to the same number of places. | ||
| Otherwise dict and Series round to variable numbers of places. | ||
| Column names should be in the keys if `decimals` is a | ||
| dict-like, or in the index if `decimals` is a Series. Any | ||
| columns not included in `decimals` will be left as is. Elements | ||
| of `decimals` which are not columns of the input will be | ||
| ignored. | ||
|
|
||
| Returns: | ||
| DataFrame: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add type for the DataFrame?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added |
||
| A DataFrame with the affected columns rounded to the specified | ||
| number of decimal places. | ||
|
|
||
| """ | ||
| raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE) | ||
|
|
||
| def apply(self, func, *, axis=0, args=(), **kwargs): | ||
| """Apply a function along an axis of the DataFrame. | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you please add type hints for decimals?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added