-
Notifications
You must be signed in to change notification settings - Fork 49
feat: add replace method to DataFrame #261
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
Changes from all commits
4bc2753
7d80aa9
b98206f
3ea1d93
aab1355
9c87404
a5dda74
1036461
b11def8
fd5a555
4d2b6b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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`. | ||
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. 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.""" | ||
|
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