Skip to content

Commit

Permalink
seperate file
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAugspurger committed Jan 29, 2019
1 parent 642b01a commit 518315c
Show file tree
Hide file tree
Showing 3 changed files with 325 additions and 113 deletions.
89 changes: 36 additions & 53 deletions pandas/tests/extension/numpy_/conftest.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,38 @@
import numpy as np
import pytest

# A set of Base EA tests that are know to not work for
# the object-dtype PandasArray holding nested data.
skips = {
'TestCasting.test_astype_str',
'TestConstructors.test_array_from_scalars',
# tuple isn't instance of np.object
'TestGetitem.test_getitem_scalar',
# Can't pass tuples to _from_sequence
'TestGetitem.test_take_series',
# np.array shape inference
'TestInterface.test_array_interface',
# Can't construct expected.
'TestMethods.test_unique',
'TestMethods.test_combine_add',
'TestMethods.test_shift_fill_value',
'TestMethods.test_where_series',
'TestMethods.test_repeat',
# Can't hash ndarray[tuple]
'TestMethods.test_hash_pandas_object_works',
# Can't construct expected.
'TestReshaping.test_merge',
'TestReshaping.test_merge_on_extension_array',
'TestReshaping.test_merge_on_extension_array_duplicates',

# ndarray setting
'TestSetitem.test_setitem_scalar_series',
'TestSetitem.test_setitem_sequence',
'TestSetitem.test_setitem_sequence_mismatched_length_raises',
'TestSetitem.test_setitem_sequence_broadcasts',
'TestSetitem.test_setitem_sequence_broadcasts',
'TestSetitem.test_setitem_loc_scalar_mixed',
'TestSetitem.test_setitem_iloc_scalar_mixed',
'TestSetitem.test_setitem_loc_scalar_multiple_homogoneous',
'TestSetitem.test_setitem_iloc_scalar_multiple_homogoneous',
'TestSetitem.test_setitem_mask_broadcast',
'TestSetitem.test_setitem_scalar_key_sequence_raise',

# parsing differs.
'TestParsing.test_EA_types',
}


def pytest_collection_modifyitems(config, items):
skip = pytest.mark.skip(reason="Skipping for nested data.")
for item in items:
# TODO: See if pytest has a better way to resolve the *value*
# supplied to a fixture. Right now .keywords gets things
# like 'object' or 'data-object'.
parts = item.name.split("[")
qualname = item.parent.obj.__class__.__name__ + '.' + item.obj.__name__
if (len(parts) > 1 and 'object' in item.name.split('[')[1]
and qualname in skips):
item.add_marker(skip)
from pandas.core.arrays.numpy_ import PandasArray


@pytest.fixture
def allow_in_pandas(monkeypatch):
"""
A monkeypatch to tells 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
64 changes: 4 additions & 60 deletions pandas/tests/extension/numpy_/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,13 @@
from .. import base


@pytest.fixture(params=['float', 'object'])
def dtype(request):
return PandasDtype(np.dtype(request.param))


@pytest.fixture
def allow_in_pandas(monkeypatch):
"""
A monkeypatch to tells 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
def dtype():
return PandasDtype(np.dtype('float'))


@pytest.fixture
def data(allow_in_pandas, dtype):
if dtype.numpy_dtype == 'object':
return pd.Series([(i,) for i in range(100)]).array
return PandasArray(np.arange(1, 101, dtype=dtype._dtype))


Expand All @@ -48,18 +24,6 @@ def data_missing(allow_in_pandas):
return PandasArray(np.array([np.nan, 1.0]))


@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


@pytest.fixture
def data_for_sorting(allow_in_pandas):
"""Length-3 array with a known sort order.
Expand Down Expand Up @@ -152,19 +116,6 @@ class TestArithmetics(BaseNumPyTests, base.BaseArithmeticOpsTests):
frame_scalar_exc = None
series_array_exc = None

def _check_op(self, s, op, other, op_name, exc=NotImplementedError):
if s.dtype == 'object':
raise pytest.skip("Skipping for object dtype.")
super(TestArithmetics, self)._check_op(s, op, other, op_name, exc)

def _check_divmod_op(self, s, op, other, exc=Exception):
if isinstance(s, pd.Series) and s.dtype == 'object':
raise pytest.skip("Skipping for object dtype.")
elif isinstance(other, pd.Series) and other.dtype == 'object':
raise pytest.skip("Skipping for object dtype.")

super(TestArithmetics, self)._check_divmod_op(s, op, other, exc)

def test_divmod_series_array(self, data):
s = pd.Series(data)
self._check_divmod_op(s, divmod, data, exc=None)
Expand Down Expand Up @@ -201,24 +152,17 @@ class TestPrinting(BaseNumPyTests, base.BasePrintingTests):
class TestNumericReduce(BaseNumPyTests, base.BaseNumericReduceTests):

def check_reduce(self, s, op_name, skipna):
if s.dtype == 'object':
raise pytest.skip("Skipping for object dtype.")
result = getattr(s, op_name)(skipna=skipna)
# avoid coercing int -> float. Just cast to the actual numpy type.
expected = getattr(s.astype(s.dtype._dtype), op_name)(skipna=skipna)
tm.assert_almost_equal(result, expected)


class TestBooleanReduce(BaseNumPyTests, base.BaseBooleanReduceTests):

def check_reduce(self, s, op_name, skipna):
if s.dtype == 'object':
raise pytest.skip("Skipping for object dtype.")

super(TestBooleanReduce, self).check_reduce(s, op_name, skipna)
pass


class TestMissing(BaseNumPyTests, base.BaseMissingTests):
class TestMising(BaseNumPyTests, base.BaseMissingTests):
pass


Expand Down
Loading

0 comments on commit 518315c

Please sign in to comment.