-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: inconsistency in dtype of replace() #44897
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 5 commits
d0565b1
2fc7c96
cb4dd4f
1129c9a
1a1c378
eb06d43
fdbd5f5
087aad2
d0f3c7f
54f5676
c83c2b6
a438762
9617165
36e9a5d
f04d05d
ddbb113
75e3bd8
af0585c
2a80b69
e281264
d00915b
2fd474c
0d93bdd
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 |
---|---|---|
|
@@ -512,3 +512,10 @@ def test_pandas_replace_na(self): | |
result = ser.replace(regex_mapping, regex=True) | ||
exp = pd.Series(["CC", "CC", "CC-REPL", "DD", "CC", "", pd.NA], dtype="string") | ||
tm.assert_series_equal(result, exp) | ||
|
||
def test_replace_regex_dtype(self): | ||
# GH-48644 | ||
s = pd.Series(["0"]) | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
exp = s.replace(to_replace="0", value=1, regex=False).dtype | ||
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. explictly create the expected and use 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. Done 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. No, you should create expected = Series(...) not using replace. And please don't use one letter variable names 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. Done |
||
res = s.replace(to_replace="0", value=1, regex=True).dtype | ||
assert exp == res |
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.
when we use this pattern the 'else' path usually involves split_and_operate
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.
In the previous case where the dimensions are as follows split and operate don't operate on these dimensions, for split and operate you need
assert self.ndim == 2 and self.shape[0] != 1
, which we do not use here.Since we have a single element here, we should not split and upcast, right?
Uh oh!
There was an error while loading. Please reload this page.
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.
not sure i understand. im saying its weird to see this check on L734 and not see a split_and_operate on L737.
e.g. in the test you implemented, what happens if you use
pd.DataFrame({"A": ["0"], "B": ["0"]})
?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.
In
replace_regex
, we have aldready applied the replacement with the only issue of the type cast. So, if I apply split_and_operate again, it won't give any specific result except for converting dtype before breaking a lot of tests. The solution is correct with the dtype being wrong.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.
pd.DataFrame({"A": ["0"], "B: ["0"]})
is giving a dtype assertion errorThere 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 paste it?
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 rectified it, the testcase has been added.