Skip to content
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

started to fixturize pandas/tests/base #31701

Merged
merged 4 commits into from
Feb 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from pandas import DataFrame
import pandas._testing as tm
from pandas.core import ops
from pandas.core.indexes.api import Index, MultiIndex

hypothesis.settings.register_profile(
"ci",
Expand Down Expand Up @@ -953,3 +954,69 @@ def __len__(self):
return self._data.__len__()

return TestNonDictMapping


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you do 'sections' here e.g. some text markers so reading the file is easy (@jbrockmendel has added these in several files); this can be a followup PR.

indices_dict = {
"unicode": tm.makeUnicodeIndex(100),
"string": tm.makeStringIndex(100),
"datetime": tm.makeDateIndex(100),
"datetime-tz": tm.makeDateIndex(100, tz="US/Pacific"),
"period": tm.makePeriodIndex(100),
"timedelta": tm.makeTimedeltaIndex(100),
"int": tm.makeIntIndex(100),
"uint": tm.makeUIntIndex(100),
"range": tm.makeRangeIndex(100),
"float": tm.makeFloatIndex(100),
"bool": tm.makeBoolIndex(2),
"categorical": tm.makeCategoricalIndex(100),
"interval": tm.makeIntervalIndex(100),
"empty": Index([]),
"tuples": MultiIndex.from_tuples(zip(["foo", "bar", "baz"], [1, 2, 3])),
"repeats": Index([0, 0, 1, 1, 2, 2]),
}
jreback marked this conversation as resolved.
Show resolved Hide resolved


@pytest.fixture(params=indices_dict.keys())
jreback marked this conversation as resolved.
Show resolved Hide resolved
def indices(request):
# copy to avoid mutation, e.g. setting .name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next time around can you give these doc-strings

return indices_dict[request.param].copy()


def _create_series(index):
""" Helper for the _series dict """
size = len(index)
data = np.random.randn(size)
return pd.Series(data, index=index, name="a")


_series = {
f"series-with-{index_id}-index": _create_series(index)
for index_id, index in indices_dict.items()
}


_narrow_dtypes = [
np.float16,
np.float32,
np.int8,
np.int16,
np.int32,
np.uint8,
np.uint16,
np.uint32,
]
_narrow_series = {
f"{dtype.__name__}-series": tm.makeFloatSeries(name="a").astype(dtype)
for dtype in _narrow_dtypes
}

_index_or_series_objs = {**indices_dict, **_series, **_narrow_series}


@pytest.fixture(params=_index_or_series_objs.keys())
SaturnFromTitan marked this conversation as resolved.
Show resolved Hide resolved
def index_or_series_obj(request):
"""
Fixture for tests on indexes, series and series with a narrow dtype
copy to avoid mutation, e.g. setting .name
"""
return _index_or_series_objs[request.param].copy(deep=True)
30 changes: 15 additions & 15 deletions pandas/tests/base/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,26 +109,26 @@ def test_binary_ops(klass, op_name, op):
assert expected_str in getattr(klass, "r" + op_name).__doc__


class TestTranspose(Ops):
class TestTranspose:
errmsg = "the 'axes' parameter is not supported"

def test_transpose(self):
for obj in self.objs:
tm.assert_equal(obj.transpose(), obj)
def test_transpose(self, index_or_series_obj):
obj = index_or_series_obj
tm.assert_equal(obj.transpose(), obj)

def test_transpose_non_default_axes(self):
for obj in self.objs:
with pytest.raises(ValueError, match=self.errmsg):
obj.transpose(1)
with pytest.raises(ValueError, match=self.errmsg):
obj.transpose(axes=1)
def test_transpose_non_default_axes(self, index_or_series_obj):
obj = index_or_series_obj
with pytest.raises(ValueError, match=self.errmsg):
obj.transpose(1)
with pytest.raises(ValueError, match=self.errmsg):
obj.transpose(axes=1)

def test_numpy_transpose(self):
for obj in self.objs:
tm.assert_equal(np.transpose(obj), obj)
def test_numpy_transpose(self, index_or_series_obj):
obj = index_or_series_obj
tm.assert_equal(np.transpose(obj), obj)

with pytest.raises(ValueError, match=self.errmsg):
np.transpose(obj, axes=1)
with pytest.raises(ValueError, match=self.errmsg):
np.transpose(obj, axes=1)


class TestIndexOps(Ops):
Expand Down
29 changes: 0 additions & 29 deletions pandas/tests/indexes/conftest.py

This file was deleted.

2 changes: 1 addition & 1 deletion pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
period_range,
)
import pandas._testing as tm
from pandas.conftest import indices_dict
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we really want to avoid this import (but understand doing this in steps)

from pandas.core.indexes.api import (
Index,
MultiIndex,
Expand All @@ -42,7 +43,6 @@
ensure_index_from_sequences,
)
from pandas.tests.indexes.common import Base
from pandas.tests.indexes.conftest import indices_dict


class TestIndex(Base):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pandas import Float64Index, Int64Index, RangeIndex, UInt64Index
import pandas._testing as tm
from pandas.api.types import pandas_dtype
from pandas.tests.indexes.conftest import indices_dict
from pandas.conftest import indices_dict
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same


COMPATIBLE_INCONSISTENT_PAIRS = {
(Int64Index, RangeIndex): (tm.makeIntIndex, tm.makeRangeIndex),
Expand Down