-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: inconsistency between replace dict using integers and using strings (#20656) #21477
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d8f2d70
BUG: align logic between replace dict using integers and using string…
peterpanmj 3afd287
remove unused condition BlockManager
peterpanmj 2bafaaa
add docstring for ObjectBlock._replace_single and BlockManager._maybe…
peterpanmj f76b2e2
update whatsnew entry ,move to reshaping
peterpanmj 6f836b4
cosmetic changes and doc-string enhancing
peterpanmj dd916d2
pull and update whatsnew
peterpanmj 7b1624b
Merge branch 'master' into replace_object
peterpanmj 4729fc5
Merge branch 'master' into replace_object
peterpanmj 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1695,6 +1695,45 @@ def _nanpercentile(values, q, axis, **kw): | |
placement=np.arange(len(result)), | ||
ndim=ndim) | ||
|
||
def _replace_coerce(self, to_replace, value, inplace=True, regex=False, | ||
convert=False, mgr=None, mask=None): | ||
""" | ||
Replace value corresponding to the given boolean array with another | ||
value. | ||
|
||
Parameters | ||
---------- | ||
to_replace : object or pattern | ||
Scalar to replace or regular expression to match. | ||
value : object | ||
Replacement object. | ||
inplace : bool, default False | ||
Perform inplace modification. | ||
regex : bool, default False | ||
If true, perform regular expression substitution. | ||
convert : bool, default True | ||
If true, try to coerce any object types to better types. | ||
mgr : BlockManager, optional | ||
mask : array-like of bool, optional | ||
True indicate corresponding element is ignored. | ||
|
||
Returns | ||
------- | ||
A new block if there is anything to replace or the original block. | ||
""" | ||
|
||
if mask.any(): | ||
if not regex: | ||
self = self.coerce_to_target_dtype(value) | ||
return self.putmask(mask, value, inplace=inplace) | ||
else: | ||
return self._replace_single(to_replace, value, inplace=inplace, | ||
regex=regex, | ||
convert=convert, | ||
mask=mask, | ||
mgr=mgr) | ||
return self | ||
|
||
|
||
class ScalarBlock(Block): | ||
""" | ||
|
@@ -2470,8 +2509,31 @@ def replace(self, to_replace, value, inplace=False, filter=None, | |
regex=regex, mgr=mgr) | ||
|
||
def _replace_single(self, to_replace, value, inplace=False, filter=None, | ||
regex=False, convert=True, mgr=None): | ||
regex=False, convert=True, mgr=None, mask=None): | ||
""" | ||
Replace elements by the given value. | ||
|
||
Parameters | ||
---------- | ||
to_replace : object or pattern | ||
Scalar to replace or regular expression to match. | ||
value : object | ||
Replacement object. | ||
inplace : bool, default False | ||
Perform inplace modification. | ||
filter : list, optional | ||
regex : bool, default False | ||
If true, perform regular expression substitution. | ||
convert : bool, default True | ||
If true, try to coerce any object types to better types. | ||
mgr : BlockManager, optional | ||
mask : array-like of bool, optional | ||
True indicate corresponding element is ignored. | ||
|
||
Returns | ||
------- | ||
a new block, the result after replacing | ||
""" | ||
inplace = validate_bool_kwarg(inplace, 'inplace') | ||
|
||
# to_replace is regex compilable | ||
|
@@ -2537,15 +2599,53 @@ def re_replacer(s): | |
else: | ||
filt = self.mgr_locs.isin(filter).nonzero()[0] | ||
|
||
new_values[filt] = f(new_values[filt]) | ||
if mask is None: | ||
new_values[filt] = f(new_values[filt]) | ||
else: | ||
new_values[filt][mask] = f(new_values[filt][mask]) | ||
|
||
# convert | ||
block = self.make_block(new_values) | ||
if convert: | ||
block = block.convert(by_item=True, numeric=False) | ||
|
||
return block | ||
|
||
def _replace_coerce(self, to_replace, value, inplace=True, regex=False, | ||
convert=False, mgr=None, mask=None): | ||
""" | ||
Replace value corresponding to the given boolean array with another | ||
value. | ||
|
||
Parameters | ||
---------- | ||
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. same as above (you can make a shared doc-string if you want here) |
||
to_replace : object or pattern | ||
Scalar to replace or regular expression to match. | ||
value : object | ||
Replacement object. | ||
inplace : bool, default False | ||
Perform inplace modification. | ||
regex : bool, default False | ||
If true, perform regular expression substitution. | ||
convert : bool, default True | ||
If true, try to coerce any object types to better types. | ||
mgr : BlockManager, optional | ||
mask : array-like of bool, optional | ||
True indicate corresponding element is ignored. | ||
|
||
Returns | ||
------- | ||
A new block if there is anything to replace or the original block. | ||
""" | ||
if mask.any(): | ||
block = super(ObjectBlock, self)._replace_coerce( | ||
to_replace=to_replace, value=value, inplace=inplace, | ||
regex=regex, convert=convert, mgr=mgr, mask=mask) | ||
if convert: | ||
block = [b.convert(by_item=True, numeric=False, copy=True) | ||
for b in block] | ||
return block | ||
return self | ||
|
||
|
||
class CategoricalBlock(ExtensionBlock): | ||
__slots__ = () | ||
|
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
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.
can you add a doc-string here