Skip to content
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

BUG: NDFrame.replace wrong exception type, wrong return when size==0 #36045

Merged
merged 37 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
4c5eddd
REF: remove unnecesary try/except
jbrockmendel Aug 21, 2020
c632c9f
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Aug 21, 2020
9e64be3
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Aug 21, 2020
42649fb
TST: add test for agg on ordered categorical cols (#35630)
mathurk1 Aug 21, 2020
47121dd
TST: resample does not yield empty groups (#10603) (#35799)
tkmz-n Aug 21, 2020
1decb3e
revert accidental rebase
jbrockmendel Aug 22, 2020
57c5dd3
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 22, 2020
a358463
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 23, 2020
ffa7ad7
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 23, 2020
e5e98d4
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 24, 2020
408db5a
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 24, 2020
d3493cf
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 25, 2020
75a805a
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 25, 2020
9f61070
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 25, 2020
2d10f6e
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 26, 2020
3e20187
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 26, 2020
e27d07f
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 27, 2020
c52bed4
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 27, 2020
b69d4d7
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 28, 2020
1c5f8fd
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 28, 2020
061c9e2
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 28, 2020
0129a83
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 30, 2020
4d0d8e8
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 30, 2020
715848f
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 30, 2020
01f9c50
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 31, 2020
5f4f7dc
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 31, 2020
4f1f4e1
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 31, 2020
aa7d119
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 31, 2020
1f783d1
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 31, 2020
7def723
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Sep 1, 2020
1c24499
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Sep 1, 2020
df4de64
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Sep 1, 2020
af88c43
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Sep 1, 2020
076bfe1
BUG: NDFrame.replace wrong exception type, wrong return when size==0
jbrockmendel Sep 1, 2020
692a6bf
bool->bool_t
jbrockmendel Sep 1, 2020
5b1e6f9
Merge branch 'master' of https://github.com/pandas-dev/pandas into cl…
jbrockmendel Sep 2, 2020
ccf92c9
whatsnew
jbrockmendel Sep 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6179,8 +6179,8 @@ def replace(
self,
to_replace=None,
value=None,
inplace=False,
limit=None,
inplace: bool_t = False,
limit: Optional[int] = None,
regex=False,
method="pad",
):
Expand Down Expand Up @@ -6256,7 +6256,7 @@ def replace(
If True, in place. Note: this will modify any
other views on this object (e.g. a column from a DataFrame).
Returns the caller if this is True.
limit : int, default None
limit : int or None, default None
Maximum size gap to forward or backward fill.
regex : bool or same types as `to_replace`, default False
Whether to interpret `to_replace` and/or `value` as regular
Expand Down Expand Up @@ -6490,7 +6490,7 @@ def replace(

inplace = validate_bool_kwarg(inplace, "inplace")
if not is_bool(regex) and to_replace is not None:
raise AssertionError("'to_replace' must be 'None' if 'regex' is not a bool")
raise ValueError("'to_replace' must be 'None' if 'regex' is not a bool")

if value is None:
# passing a single value that is scalar like
Expand Down Expand Up @@ -6550,12 +6550,14 @@ def replace(

# need a non-zero len on all axes
if not self.size:
return self
if inplace:
return
return self.copy()

if is_dict_like(to_replace):
if is_dict_like(value): # {'A' : NA} -> {'A' : 0}
# Note: Checking below for `in foo.keys()` instead of
# `in foo`is needed for when we have a Series and not dict
# `in foo` is needed for when we have a Series and not dict
mapping = {
col: (to_replace[col], value[col])
for col in to_replace.keys()
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/series/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,29 @@ def test_replace_invalid_to_replace(self):
with pytest.raises(TypeError, match=msg):
series.replace(lambda x: x.strip())

@pytest.mark.parametrize("frame", [False, True])
def test_replace_nonbool_regex(self, frame):
obj = pd.Series(["a", "b", "c "])
if frame:
obj = obj.to_frame()

msg = "'to_replace' must be 'None' if 'regex' is not a bool"
with pytest.raises(ValueError, match=msg):
obj.replace(to_replace=["a"], regex="foo")

@pytest.mark.parametrize("frame", [False, True])
def test_replace_empty_copy(self, frame):
obj = pd.Series([], dtype=np.float64)
if frame:
obj = obj.to_frame()

res = obj.replace(4, 5, inplace=True)
assert res is None

res = obj.replace(4, 5, inplace=False)
tm.assert_equal(res, obj)
assert res is not obj

def test_replace_only_one_dictlike_arg(self):
# GH#33340

Expand Down