-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Add use_nullable_dtypes in csv internals #48403
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
mroeschke
merged 12 commits into
pandas-dev:main
from
phofl:enh_add_use_nullable_dtypes_in_maybe_upcast
Sep 19, 2022
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0f370f0
ENH: Add use_nullable_dtypes in csv internals
phofl f5e2015
Add tests
phofl 373a17b
Fix mypy
phofl 197c796
Merge remote-tracking branch 'upstream/main' into enh_add_use_nullabl…
phofl baa5310
Add comment
phofl 7a106b4
Merge remote-tracking branch 'upstream/main' into enh_add_use_nullabl…
phofl 709e068
Add pyarrow test
phofl 4100ad0
Fix float32
phofl 757f113
Fix float32
phofl 9e85b39
Add contextmanager
phofl 77b3715
Merge remote-tracking branch 'upstream/main' into enh_add_use_nullabl…
phofl 326378f
Merge remote-tracking branch 'upstream/main' into enh_add_use_nullabl…
phofl 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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from pandas._libs.parsers import ( # type: ignore[attr-defined] | ||
_maybe_upcast, | ||
na_values, | ||
) | ||
|
||
from pandas import NA | ||
import pandas._testing as tm | ||
from pandas.core.arrays import ( | ||
BooleanArray, | ||
FloatingArray, | ||
IntegerArray, | ||
StringArray, | ||
) | ||
|
||
|
||
def test_maybe_upcast(any_real_numpy_dtype): | ||
# GH#36712 | ||
if any_real_numpy_dtype == "float32": | ||
pytest.skip() | ||
mroeschke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
dtype = np.dtype(any_real_numpy_dtype) | ||
na_value = na_values[dtype] | ||
arr = np.array([1, 2, na_value], dtype=dtype) | ||
result = _maybe_upcast(arr, use_nullable_dtypes=True) | ||
|
||
expected_mask = np.array([False, False, True]) | ||
if issubclass(dtype.type, np.integer): | ||
expected = IntegerArray(arr, mask=expected_mask) | ||
else: | ||
expected = FloatingArray(arr, mask=expected_mask) | ||
|
||
tm.assert_extension_array_equal(result, expected) | ||
|
||
|
||
def test_maybe_upcast_no_na(any_real_numpy_dtype): | ||
# GH#36712 | ||
if any_real_numpy_dtype == "float32": | ||
pytest.skip() | ||
|
||
arr = np.array([1, 2, 3], dtype=any_real_numpy_dtype) | ||
result = _maybe_upcast(arr, use_nullable_dtypes=True) | ||
|
||
expected_mask = np.array([False, False, False]) | ||
if issubclass(np.dtype(any_real_numpy_dtype).type, np.integer): | ||
expected = IntegerArray(arr, mask=expected_mask) | ||
else: | ||
expected = FloatingArray(arr, mask=expected_mask) | ||
|
||
tm.assert_extension_array_equal(result, expected) | ||
|
||
|
||
def test_maybe_upcaste_bool(): | ||
# GH#36712 | ||
dtype = np.bool_ | ||
na_value = na_values[dtype] | ||
arr = np.array([True, False, na_value], dtype="uint8").view(dtype) | ||
result = _maybe_upcast(arr, use_nullable_dtypes=True) | ||
|
||
expected_mask = np.array([False, False, True]) | ||
expected = BooleanArray(arr, mask=expected_mask) | ||
tm.assert_extension_array_equal(result, expected) | ||
|
||
|
||
def test_maybe_upcaste_bool_no_nan(): | ||
# GH#36712 | ||
dtype = np.bool_ | ||
arr = np.array([True, False, False], dtype="uint8").view(dtype) | ||
result = _maybe_upcast(arr, use_nullable_dtypes=True) | ||
|
||
expected_mask = np.array([False, False, False]) | ||
expected = BooleanArray(arr, mask=expected_mask) | ||
tm.assert_extension_array_equal(result, expected) | ||
|
||
|
||
def test_maybe_upcaste_all_nan(): | ||
# GH#36712 | ||
dtype = np.int64 | ||
na_value = na_values[dtype] | ||
arr = np.array([na_value, na_value], dtype=dtype) | ||
result = _maybe_upcast(arr, use_nullable_dtypes=True) | ||
|
||
expected_mask = np.array([True, True]) | ||
expected = IntegerArray(arr, mask=expected_mask) | ||
tm.assert_extension_array_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize("val", [na_values[np.object_], "c"]) | ||
def test_maybe_upcast_object(val): | ||
# GH#36712 | ||
arr = np.array(["a", "b", val], dtype=np.object_) | ||
result = _maybe_upcast(arr, use_nullable_dtypes=True) | ||
|
||
exp_val = "c" if val == "c" else NA | ||
expected = StringArray(np.array(["a", "b", exp_val], dtype=np.object_)) | ||
mroeschke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tm.assert_extension_array_equal(result, expected) |
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.
is there a viable way to do this outside of the cython code?
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.
A possible option might be to change
_maybe_upcast
to optionally return a values array + mask array, and then do the actual construction in python?Although since
_maybe_upcast
is only being called in this file (and thus from cython), that won't help. Unless if we would propagate such a (values, mask) tuple (instead of ArrayLike) through the different calls and return that from theTextReader.read
method.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.
yah not worth contorting ourselves to avoid non-cython imports. just if there's a convenient alternative
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.
Could be done yes, but would make logic more complex, which is something we should avoid here I think
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.
yah, never mind then. thanks for taking a look