-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Test nested PandasArray #24993
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
Merged
Test nested PandasArray #24993
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
558cdbe
REF: Move numpy tests down a directory
TomAugspurger 38e7413
The fix
TomAugspurger 9122bb6
implement skips
TomAugspurger 86948a1
fixed skip message
TomAugspurger afb1bee
Merge remote-tracking branch 'upstream/master' into 24986-nested-array
TomAugspurger 642b01a
py2 compat
TomAugspurger 518315c
seperate file
TomAugspurger 6d7e0d8
remove duplicate fixtures
TomAugspurger bf1efc9
Skip for old NumPy
TomAugspurger cc246c9
comment
TomAugspurger bea8de0
Merge remote-tracking branch 'upstream/master' into 24986-nested-array
TomAugspurger 7cf5583
Update pandas/tests/extension/numpy_/conftest.py
jorisvandenbossche 358df86
Update test_numpy_nested.py
TomAugspurger 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
Empty file.
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,38 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from pandas.core.arrays.numpy_ import PandasArray | ||
|
||
|
||
@pytest.fixture | ||
def allow_in_pandas(monkeypatch): | ||
""" | ||
A monkeypatch to tell pandas to let us in. | ||
|
||
By default, passing a PandasArray to an index / series / frame | ||
constructor will unbox that PandasArray to an ndarray, and treat | ||
it as a non-EA column. We don't want people using EAs without | ||
reason. | ||
|
||
The mechanism for this is a check against ABCPandasArray | ||
in each constructor. | ||
|
||
But, for testing, we need to allow them in pandas. So we patch | ||
the _typ of PandasArray, so that we evade the ABCPandasArray | ||
check. | ||
""" | ||
with monkeypatch.context() as m: | ||
m.setattr(PandasArray, '_typ', 'extension') | ||
yield | ||
|
||
|
||
@pytest.fixture | ||
def na_value(): | ||
return np.nan | ||
|
||
|
||
@pytest.fixture | ||
def na_cmp(): | ||
def cmp(a, b): | ||
return np.isnan(a) and np.isnan(b) | ||
return cmp |
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,286 @@ | ||
""" | ||
Tests for PandasArray with nested data. Users typically won't create | ||
these objects via `pd.array`, but they can show up through `.array` | ||
on a Series with nested data. | ||
|
||
We partition these tests into their own file, as many of the base | ||
tests fail, as they aren't appropriate for nested data. It is easier | ||
to have a seperate file with its own data generating fixtures, than | ||
trying to skip based upon the value of a fixture. | ||
""" | ||
import pytest | ||
|
||
import pandas as pd | ||
from pandas.core.arrays.numpy_ import PandasArray, PandasDtype | ||
|
||
from .. import base | ||
|
||
# For NumPy <1.16, np.array([np.nan, (1,)]) raises | ||
# ValueError: setting an array element with a sequence. | ||
np = pytest.importorskip('numpy', minversion='1.16.0') | ||
|
||
|
||
@pytest.fixture | ||
def dtype(): | ||
return PandasDtype(np.dtype('object')) | ||
|
||
|
||
@pytest.fixture | ||
def data(allow_in_pandas, dtype): | ||
return pd.Series([(i,) for i in range(100)]).array | ||
|
||
|
||
@pytest.fixture | ||
def data_missing(allow_in_pandas): | ||
return PandasArray(np.array([np.nan, (1,)])) | ||
|
||
|
||
@pytest.fixture | ||
def data_for_sorting(allow_in_pandas): | ||
"""Length-3 array with a known sort order. | ||
|
||
This should be three items [B, C, A] with | ||
A < B < C | ||
""" | ||
# Use an empty tuple for first element, then remove, | ||
# to disable np.array's shape inference. | ||
return PandasArray( | ||
np.array([(), (2,), (3,), (1,)])[1:] | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def data_missing_for_sorting(allow_in_pandas): | ||
"""Length-3 array with a known sort order. | ||
|
||
This should be three items [B, NA, A] with | ||
A < B and NA missing. | ||
""" | ||
return PandasArray( | ||
np.array([(1,), np.nan, (0,)]) | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def data_for_grouping(allow_in_pandas): | ||
"""Data for factorization, grouping, and unique tests. | ||
|
||
Expected to be like [B, B, NA, NA, A, A, B, C] | ||
|
||
Where A < B < C and NA is missing | ||
""" | ||
a, b, c = (1,), (2,), (3,) | ||
return PandasArray(np.array( | ||
[b, b, np.nan, np.nan, a, a, b, c] | ||
)) | ||
|
||
|
||
skip_nested = pytest.mark.skip(reason="Skipping for nested PandasArray") | ||
|
||
|
||
class BaseNumPyTests(object): | ||
pass | ||
|
||
|
||
class TestCasting(BaseNumPyTests, base.BaseCastingTests): | ||
|
||
@skip_nested | ||
def test_astype_str(self, data): | ||
pass | ||
|
||
|
||
class TestConstructors(BaseNumPyTests, base.BaseConstructorsTests): | ||
@pytest.mark.skip(reason="We don't register our dtype") | ||
# We don't want to register. This test should probably be split in two. | ||
def test_from_dtype(self, data): | ||
pass | ||
|
||
@skip_nested | ||
def test_array_from_scalars(self, data): | ||
pass | ||
|
||
|
||
class TestDtype(BaseNumPyTests, base.BaseDtypeTests): | ||
|
||
@pytest.mark.skip(reason="Incorrect expected.") | ||
# we unsurprisingly clash with a NumPy name. | ||
def test_check_dtype(self, data): | ||
pass | ||
|
||
|
||
class TestGetitem(BaseNumPyTests, base.BaseGetitemTests): | ||
|
||
@skip_nested | ||
def test_getitem_scalar(self, data): | ||
pass | ||
|
||
@skip_nested | ||
def test_take_series(self, data): | ||
pass | ||
|
||
|
||
class TestGroupby(BaseNumPyTests, base.BaseGroupbyTests): | ||
@skip_nested | ||
def test_groupby_extension_apply(self, data_for_grouping, op): | ||
pass | ||
|
||
|
||
class TestInterface(BaseNumPyTests, base.BaseInterfaceTests): | ||
@skip_nested | ||
def test_array_interface(self, data): | ||
# NumPy array shape inference | ||
pass | ||
|
||
|
||
class TestMethods(BaseNumPyTests, base.BaseMethodsTests): | ||
|
||
@pytest.mark.skip(reason="TODO: remove?") | ||
def test_value_counts(self, all_data, dropna): | ||
pass | ||
|
||
@pytest.mark.skip(reason="Incorrect expected") | ||
# We have a bool dtype, so the result is an ExtensionArray | ||
# but expected is not | ||
def test_combine_le(self, data_repeated): | ||
super(TestMethods, self).test_combine_le(data_repeated) | ||
|
||
@skip_nested | ||
def test_combine_add(self, data_repeated): | ||
# Not numeric | ||
pass | ||
|
||
@skip_nested | ||
def test_shift_fill_value(self, data): | ||
# np.array shape inference. Shift implementation fails. | ||
super().test_shift_fill_value(data) | ||
|
||
@skip_nested | ||
def test_unique(self, data, box, method): | ||
# Fails creating expected | ||
pass | ||
|
||
@skip_nested | ||
def test_fillna_copy_frame(self, data_missing): | ||
# The "scalar" for this array isn't a scalar. | ||
pass | ||
|
||
@skip_nested | ||
def test_fillna_copy_series(self, data_missing): | ||
# The "scalar" for this array isn't a scalar. | ||
pass | ||
|
||
@skip_nested | ||
def test_hash_pandas_object_works(self, data, as_frame): | ||
# ndarray of tuples not hashable | ||
pass | ||
|
||
@skip_nested | ||
def test_searchsorted(self, data_for_sorting, as_series): | ||
# Test setup fails. | ||
pass | ||
|
||
@skip_nested | ||
def test_where_series(self, data, na_value, as_frame): | ||
# Test setup fails. | ||
pass | ||
|
||
@skip_nested | ||
def test_repeat(self, data, repeats, as_series, use_numpy): | ||
# Fails creating expected | ||
pass | ||
|
||
|
||
class TestPrinting(BaseNumPyTests, base.BasePrintingTests): | ||
pass | ||
|
||
|
||
class TestMissing(BaseNumPyTests, base.BaseMissingTests): | ||
|
||
@skip_nested | ||
def test_fillna_scalar(self, data_missing): | ||
# Non-scalar "scalar" values. | ||
pass | ||
|
||
@skip_nested | ||
def test_fillna_series_method(self, data_missing, method): | ||
# Non-scalar "scalar" values. | ||
pass | ||
|
||
@skip_nested | ||
def test_fillna_series(self, data_missing): | ||
# Non-scalar "scalar" values. | ||
pass | ||
|
||
@skip_nested | ||
def test_fillna_frame(self, data_missing): | ||
# Non-scalar "scalar" values. | ||
pass | ||
|
||
|
||
class TestReshaping(BaseNumPyTests, base.BaseReshapingTests): | ||
|
||
@pytest.mark.skip("Incorrect parent test") | ||
# not actually a mixed concat, since we concat int and int. | ||
def test_concat_mixed_dtypes(self, data): | ||
super(TestReshaping, self).test_concat_mixed_dtypes(data) | ||
|
||
@skip_nested | ||
def test_merge(self, data, na_value): | ||
# Fails creating expected | ||
pass | ||
|
||
@skip_nested | ||
def test_merge_on_extension_array(self, data): | ||
# Fails creating expected | ||
pass | ||
|
||
@skip_nested | ||
def test_merge_on_extension_array_duplicates(self, data): | ||
# Fails creating expected | ||
pass | ||
|
||
|
||
class TestSetitem(BaseNumPyTests, base.BaseSetitemTests): | ||
|
||
@skip_nested | ||
def test_setitem_scalar_series(self, data, box_in_series): | ||
pass | ||
|
||
@skip_nested | ||
def test_setitem_sequence(self, data, box_in_series): | ||
pass | ||
|
||
@skip_nested | ||
def test_setitem_sequence_mismatched_length_raises(self, data, as_array): | ||
pass | ||
|
||
@skip_nested | ||
def test_setitem_sequence_broadcasts(self, data, box_in_series): | ||
pass | ||
|
||
@skip_nested | ||
def test_setitem_loc_scalar_mixed(self, data): | ||
pass | ||
|
||
@skip_nested | ||
def test_setitem_loc_scalar_multiple_homogoneous(self, data): | ||
pass | ||
|
||
@skip_nested | ||
def test_setitem_iloc_scalar_mixed(self, data): | ||
pass | ||
|
||
@skip_nested | ||
def test_setitem_iloc_scalar_multiple_homogoneous(self, data): | ||
pass | ||
|
||
@skip_nested | ||
def test_setitem_mask_broadcast(self, data, setter): | ||
pass | ||
|
||
@skip_nested | ||
def test_setitem_scalar_key_sequence_raise(self, data): | ||
pass | ||
|
||
|
||
# Skip Arithmetics, NumericReduce, BooleanReduce, Parsing |
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.
can you add a comment here on what this particular file is testing. (as a casual glance makes it look very similar to test_numpy.py)
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.
@TomAugspurger but still this does not answer the question of why you duplicated things
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.
does https://github.com/pandas-dev/pandas/pull/24993/files#diff-c77963c2757e522c9ed516402633c932R6 make sense?
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.
it makes sense, I suppose enough. Problem is a future reader may not understand exactly what you are getting at here.
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.
@TomAugspurger I fear that I may have led you in the wrong direction with the first example on just including the fixture that you want the value of in the function signature. that was just to show that fixtures can be added to the function signature and that additional unwanted permutations would not occur.
from the pytest docs on request.getfixturevalue.. "Declaring fixtures via function argument is recommended where possible. But if you can only decide whether to use another fixture at test setup time, you may use this function to retrieve it inside a fixture or test function body."
hence why i mentioned the function argument approach first.
@jreback is right about adding markers, and the
pytest.mark.usefixtures
is probably the appropriate marker to use.if this marker was used only on tests that depended on the
dtype
fixture, then the autouse fixture I suggested could be used without therequest.getfixturevalue('dtype')
anddtype
included in the fixture signature along with therequest
fixture which gives access to the class, instance and function to determine if the test should be skipped.My comments should not be taken as a reason not to merge. A follow-on PR could look into this approach.
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'm relatively happy with current approach. It's lower-tech which is fine for me in tests.