-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
REF: replace column-wise, remove BlockManager.apply filter kwarg #33340
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 3 commits
d9910af
456f501
f94e2a2
8697b13
dec71b0
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 |
---|---|---|
|
@@ -6459,7 +6459,6 @@ def replace( | |
): | ||
if not ( | ||
is_scalar(to_replace) | ||
or isinstance(to_replace, pd.Series) | ||
or is_re_compilable(to_replace) | ||
or is_list_like(to_replace) | ||
): | ||
|
@@ -6550,18 +6549,20 @@ def replace( | |
|
||
# {'A': NA} -> 0 | ||
elif not is_list_like(value): | ||
keys = [(k, src) for k, src in to_replace.items() if k in self] | ||
keys_len = len(keys) - 1 | ||
for i, (k, src) in enumerate(keys): | ||
convert = i == keys_len | ||
new_data = new_data.replace( | ||
to_replace=src, | ||
value=value, | ||
filter=[k], | ||
inplace=inplace, | ||
regex=regex, | ||
convert=convert, | ||
) | ||
|
||
# Operate column-wise | ||
res = self if inplace else self.copy() | ||
ax = self._info_axis | ||
# Note: we only get here with self.ndim == 2 | ||
for i in range(len(ax)): | ||
if ax[i] in to_replace: | ||
ser = self._ixs(i, axis=1) | ||
newobj = ser.replace(to_replace[ax[i]], value, regex=regex) | ||
res.iloc[:, i] = newobj | ||
|
||
if inplace: | ||
return | ||
return res.__finalize__(self) | ||
else: | ||
raise TypeError("value argument must be scalar, dict, or Series") | ||
|
||
|
@@ -6602,17 +6603,20 @@ def replace( | |
|
||
# dest iterable dict-like | ||
if is_dict_like(value): # NA -> {'A' : 0, 'B' : -1} | ||
new_data = self._mgr | ||
|
||
for k, v in value.items(): | ||
if k in self: | ||
new_data = new_data.replace( | ||
to_replace=to_replace, | ||
value=v, | ||
filter=[k], | ||
inplace=inplace, | ||
regex=regex, | ||
) | ||
|
||
# Operate column-wise | ||
res = self if inplace else self.copy() | ||
ax = self._info_axis | ||
for i in range(len(ax)): | ||
if ax[i] in value: | ||
v = value[ax[i]] | ||
ser = self._ixs(i, axis=1) | ||
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, can you factor this to a function instead. 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. refactored + green i think in follow-up might be able to use this new helper method in one or two more places |
||
newobj = ser.replace(to_replace, value=v, regex=regex) | ||
res.iloc[:, i] = newobj | ||
|
||
if inplace: | ||
return | ||
return res.__finalize__(self) | ||
|
||
elif not is_list_like(value): # NA -> 0 | ||
new_data = self._mgr.replace( | ||
|
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.
i would just use
.iloc
here