-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Coerce to numeric despite uint64 conflict #17823
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
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 |
---|---|---|
|
@@ -39,6 +39,11 @@ | |
from pandas.util import testing as tm | ||
|
||
|
||
@pytest.fixture(params=[True, False], ids=lambda val: str(val)) | ||
def coerce(request): | ||
return request.param | ||
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. Should we do this as a parameterize instead of fixture? (I don't know whether are some 'rules' where it is better to use one or the other, but in this case it really seems to be a parameterizing of the test, so I would find it easier to read the code if it uses paramatrize) 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. I use fixture whenever I find myself using the same parametrization multiple times. As you can see below, I use it three times. |
||
|
||
|
||
def test_is_sequence(): | ||
is_seq = inference.is_sequence | ||
assert (is_seq((1, 2))) | ||
|
@@ -340,44 +345,38 @@ def test_convert_numeric_uint64(self): | |
exp = np.array([2**63], dtype=np.uint64) | ||
tm.assert_numpy_array_equal(lib.maybe_convert_numeric(arr, set()), exp) | ||
|
||
def test_convert_numeric_uint64_nan(self): | ||
msg = 'uint64 array detected' | ||
cases = [(np.array([2**63, np.nan], dtype=object), set()), | ||
(np.array([str(2**63), np.nan], dtype=object), set()), | ||
(np.array([np.nan, 2**63], dtype=object), set()), | ||
(np.array([np.nan, str(2**63)], dtype=object), set()), | ||
(np.array([2**63, 2**63 + 1], dtype=object), set([2**63])), | ||
(np.array([str(2**63), str(2**63 + 1)], | ||
dtype=object), set([2**63]))] | ||
|
||
for coerce in (True, False): | ||
for arr, na_values in cases: | ||
if coerce: | ||
with tm.assert_raises_regex(ValueError, msg): | ||
lib.maybe_convert_numeric(arr, na_values, | ||
coerce_numeric=coerce) | ||
else: | ||
tm.assert_numpy_array_equal(lib.maybe_convert_numeric( | ||
arr, na_values), arr) | ||
|
||
def test_convert_numeric_int64_uint64(self): | ||
msg = 'uint64 and negative values detected' | ||
cases = [np.array([2**63, -1], dtype=object), | ||
np.array([str(2**63), -1], dtype=object), | ||
np.array([str(2**63), str(-1)], dtype=object), | ||
np.array([-1, 2**63], dtype=object), | ||
np.array([-1, str(2**63)], dtype=object), | ||
np.array([str(-1), str(2**63)], dtype=object)] | ||
|
||
for coerce in (True, False): | ||
for case in cases: | ||
if coerce: | ||
with tm.assert_raises_regex(ValueError, msg): | ||
lib.maybe_convert_numeric(case, set(), | ||
coerce_numeric=coerce) | ||
else: | ||
tm.assert_numpy_array_equal(lib.maybe_convert_numeric( | ||
case, set()), case) | ||
@pytest.mark.parametrize("arr", [ | ||
np.array([2**63, np.nan], dtype=object), | ||
np.array([str(2**63), np.nan], dtype=object), | ||
np.array([np.nan, 2**63], dtype=object), | ||
np.array([np.nan, str(2**63)], dtype=object)]) | ||
def test_convert_numeric_uint64_nan(self, coerce, arr): | ||
expected = arr.astype(float) if coerce else arr.copy() | ||
result = lib.maybe_convert_numeric(arr, set(), | ||
coerce_numeric=coerce) | ||
tm.assert_almost_equal(result, expected) | ||
|
||
def test_convert_numeric_uint64_nan_values(self, coerce): | ||
arr = np.array([2**63, 2**63 + 1], dtype=object) | ||
na_values = set([2**63]) | ||
|
||
expected = (np.array([np.nan, 2**63 + 1], dtype=float) | ||
if coerce else arr.copy()) | ||
result = lib.maybe_convert_numeric(arr, na_values, | ||
coerce_numeric=coerce) | ||
tm.assert_almost_equal(result, expected) | ||
|
||
@pytest.mark.parametrize("case", [ | ||
np.array([2**63, -1], dtype=object), | ||
np.array([str(2**63), -1], dtype=object), | ||
np.array([str(2**63), str(-1)], dtype=object), | ||
np.array([-1, 2**63], dtype=object), | ||
np.array([-1, str(2**63)], dtype=object), | ||
np.array([str(-1), str(2**63)], dtype=object)]) | ||
def test_convert_numeric_int64_uint64(self, case, coerce): | ||
expected = case.astype(float) if coerce else case.copy() | ||
result = lib.maybe_convert_numeric(case, set(), coerce_numeric=coerce) | ||
tm.assert_almost_equal(result, expected) | ||
|
||
def test_maybe_convert_objects_uint64(self): | ||
# see gh-4471 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -381,3 +381,28 @@ def test_downcast_limits(self): | |
for dtype, downcast, min_max in dtype_downcast_min_max: | ||
series = pd.to_numeric(pd.Series(min_max), downcast=downcast) | ||
assert series.dtype == dtype | ||
|
||
def test_coerce_uint64_conflict(self): | ||
# see gh-17007 and gh-17125 | ||
# | ||
# Still returns float despite the uint64-nan conflict, | ||
# which would normally force the casting to object. | ||
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. can you also test with 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. |
||
df = pd.DataFrame({"a": [200, 300, "", "NaN", 30000000000000000000]}) | ||
expected = pd.Series([200, 300, np.nan, np.nan, | ||
30000000000000000000], dtype=float, name="a") | ||
result = to_numeric(df["a"], errors="coerce") | ||
tm.assert_series_equal(result, expected) | ||
|
||
s = pd.Series(["12345678901234567890", "1234567890", "ITEM"]) | ||
expected = pd.Series([12345678901234567890, | ||
1234567890, np.nan], dtype=float) | ||
result = to_numeric(s, errors="coerce") | ||
tm.assert_series_equal(result, expected) | ||
|
||
# For completeness, check against "ignore" and "raise" | ||
result = to_numeric(s, errors="ignore") | ||
tm.assert_series_equal(result, s) | ||
|
||
msg = "Unable to parse string" | ||
with tm.assert_raises_regex(ValueError, msg): | ||
to_numeric(s, errors="raise") |
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.
interesting, so these cases were not actually tested then? (IOW you didn't have to fix any tests)?
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.
nvm. I see you blew away the entire section and re-wrote.