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 replace method to DataFrame #261
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4bc2753
feat: add replace method to DataFrame
TrevorBergeron 7d80aa9
Merge branch 'main' into df_replace
TrevorBergeron b98206f
Merge branch 'main' into df_replace
TrevorBergeron 3ea1d93
Merge branch 'main' into df_replace
TrevorBergeron aab1355
Merge branch 'main' into df_replace
TrevorBergeron 9c87404
remove unwanted change to describe method
TrevorBergeron a5dda74
better docs
TrevorBergeron 1036461
is_patype docstring
TrevorBergeron b11def8
Merge remote-tracking branch 'github/main' into df_replace
TrevorBergeron fd5a555
docstring fix
TrevorBergeron 4d2b6b3
mypy fix
TrevorBergeron 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
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 |
|---|---|---|
|
|
@@ -4356,6 +4356,94 @@ def fillna(self, value): | |
| """ | ||
| raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE) | ||
|
|
||
| def replace( | ||
| self, | ||
| to_replace, | ||
| value=None, | ||
| *, | ||
| regex=False, | ||
| ): | ||
| """ | ||
| Replace values given in `to_replace` with `value`. | ||
|
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. Could you also help add code_samples in the docs as well? Since the person who implements the method knows more about the use cases, and we don't need to do it later in a seperate PR.
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 few code samples |
||
|
|
||
| Values of the Series/DataFrame are replaced with other values dynamically. | ||
| This differs from updating with ``.loc`` or ``.iloc``, which require | ||
| you to specify a location to update with some value. | ||
|
|
||
| **Examples:** | ||
|
|
||
| >>> import bigframes.pandas as bpd | ||
| >>> bpd.options.display.progress_bar = None | ||
|
|
||
| >>> df = bpd.DataFrame({ | ||
| ... 'int_col': [1, 1, 2, 3], | ||
| ... 'string_col': ["a", "b", "c", "b"], | ||
| ... }) | ||
|
|
||
| Using scalar `to_replace` and `value`: | ||
|
|
||
| >>> df.replace("b", "e") | ||
| int_col string_col | ||
| 0 1 a | ||
| 1 1 e | ||
| 2 2 c | ||
| 3 3 e | ||
| <BLANKLINE> | ||
| [4 rows x 2 columns] | ||
|
|
||
| Using dictionary: | ||
|
|
||
| >>> df.replace({"a": "e", 2: 5}) | ||
| int_col string_col | ||
| 0 1 e | ||
| 1 1 b | ||
| 2 5 c | ||
| 3 3 b | ||
| <BLANKLINE> | ||
| [4 rows x 2 columns] | ||
|
|
||
| Using regex: | ||
|
|
||
| >>> df.replace("[ab]", "e", regex=True) | ||
| int_col string_col | ||
| 0 1 e | ||
| 1 1 e | ||
| 2 2 c | ||
| 3 3 e | ||
| <BLANKLINE> | ||
| [4 rows x 2 columns] | ||
|
|
||
|
|
||
| Args: | ||
| to_replace (str, regex, list, int, float or None): | ||
| How to find the values that will be replaced. | ||
| numeric: numeric values equal to `to_replace` will be replaced with `value` | ||
| str: string exactly matching `to_replace` will be replaced with `value` | ||
| regex: regexs matching `to_replace` will be replaced with`value` | ||
| list of str, regex, or numeric: | ||
| First, if `to_replace` and `value` are both lists, they **must** be the same length. | ||
| Second, if ``regex=True`` then all of the strings in **both** | ||
| lists will be interpreted as regexs otherwise they will match | ||
| directly. This doesn't matter much for `value` since there | ||
| are only a few possible substitution regexes you can use. | ||
| str, regex and numeric rules apply as above. | ||
|
|
||
| value (scalar, default None): | ||
| Value to replace any values matching `to_replace` with. | ||
| For a DataFrame a dict of values can be used to specify which | ||
| value to use for each column (columns not in the dict will not be | ||
| filled). Regular expressions, strings and lists or dicts of such | ||
| objects are also allowed. | ||
| regex (bool, default False): | ||
| Whether to interpret `to_replace` and/or `value` as regular | ||
| expressions. If this is ``True`` then `to_replace` *must* be a | ||
| string. | ||
|
|
||
| Returns: | ||
| Series/DataFrame: Object after replacement. | ||
| """ | ||
| raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE) | ||
|
|
||
| @property | ||
| def iloc(self): | ||
| """Purely integer-location based indexing for selection by position.""" | ||
|
|
||
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.
Missing docstring here?
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 docstring