Skip to content

TST: move to indices fixture instead of create_index #32916

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 6 commits into from
Mar 24, 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
12 changes: 12 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,17 @@ def _create_multiindex():
return mi


def _create_mi_with_dt64tz_level():
"""
MultiIndex with a level that is a tzaware DatetimeIndex.
"""
# GH#8367 round trip with pickle
return MultiIndex.from_product(
[[1, 2], ["a", "b"], pd.date_range("20130101", periods=3, tz="US/Eastern")],
names=["one", "two", "three"],
)


indices_dict = {
"unicode": tm.makeUnicodeIndex(100),
"string": tm.makeStringIndex(100),
Expand All @@ -384,6 +395,7 @@ def _create_multiindex():
"interval": tm.makeIntervalIndex(100),
"empty": Index([]),
"tuples": MultiIndex.from_tuples(zip(["foo", "bar", "baz"], [1, 2, 3])),
"mi-with-dt64tz-level": _create_mi_with_dt64tz_level(),
"multi": _create_multiindex(),
"repeats": Index([0, 0, 1, 1, 2, 2]),
}
Expand Down
45 changes: 0 additions & 45 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,6 @@ def test_pickle_compat_construction(self):
with pytest.raises(TypeError, match=msg):
self._holder()

def test_to_series(self):
# assert that we are creating a copy of the index

idx = self.create_index()
s = idx.to_series()
assert s.values is not idx.values
assert s.index is not idx
assert s.name == idx.name

def test_to_series_with_arguments(self):
# GH18699

# index kwarg
idx = self.create_index()
s = idx.to_series(index=idx)

assert s.values is not idx.values
assert s.index is idx
assert s.name == idx.name

# name kwarg
idx = self.create_index()
s = idx.to_series(name="__test")

assert s.values is not idx.values
assert s.index is not idx
assert s.name != idx.name

@pytest.mark.parametrize("name", [None, "new_name"])
def test_to_frame(self, name):
# see GH-15230, GH-22580
Expand Down Expand Up @@ -198,15 +170,6 @@ def test_logical_compat(self):
with pytest.raises(TypeError, match="cannot perform any"):
idx.any()

def test_boolean_context_compat(self):

# boolean context compat
idx = self.create_index()

with pytest.raises(ValueError, match="The truth value of a"):
if idx:
pass

def test_reindex_base(self):
idx = self.create_index()
expected = np.arange(idx.size, dtype=np.intp)
Expand Down Expand Up @@ -253,14 +216,6 @@ def test_repr_roundtrip(self):
idx = self.create_index()
tm.assert_index_equal(eval(repr(idx)), idx)

def test_str(self):

# test the string repr
idx = self.create_index()
idx.name = "foo"
assert "'foo'" in str(idx)
assert type(idx).__name__ in str(idx)

def test_repr_max_seq_item_setting(self):
# GH10182
idx = self.create_index()
Expand Down
7 changes: 0 additions & 7 deletions pandas/tests/indexes/datetimes/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,6 @@ def test_week_of_month_frequency(self):
expected = DatetimeIndex(dates, freq="WOM-1SAT")
tm.assert_index_equal(result, expected)

def test_hash_error(self):
index = date_range("20010101", periods=10)
with pytest.raises(
TypeError, match=f"unhashable type: '{type(index).__name__}'"
):
hash(index)

def test_stringified_slice_with_tz(self):
# GH#2658
start = "2013-01-07"
Expand Down
49 changes: 1 addition & 48 deletions pandas/tests/indexes/multi/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@
import pytest

import pandas as pd
from pandas import DataFrame, MultiIndex, date_range
from pandas import DataFrame, MultiIndex
import pandas._testing as tm


def test_tolist(idx):
result = idx.tolist()
exp = list(idx.values)
assert result == exp


def test_to_numpy(idx):
result = idx.to_numpy()
exp = idx.values
Expand Down Expand Up @@ -129,47 +123,6 @@ def test_to_frame_resulting_column_order():
assert result == expected


def test_roundtrip_pickle_with_tz():
return # FIXME: this can't be right?

# GH 8367
# round-trip of timezone
index = MultiIndex.from_product(
[[1, 2], ["a", "b"], date_range("20130101", periods=3, tz="US/Eastern")],
names=["one", "two", "three"],
)
unpickled = tm.round_trip_pickle(index)
assert index.equal_levels(unpickled)


def test_to_series(idx):
# assert that we are creating a copy of the index

s = idx.to_series()
assert s.values is not idx.values
assert s.index is not idx
assert s.name == idx.name


def test_to_series_with_arguments(idx):
# GH18699

# index kwarg
s = idx.to_series(index=idx)

assert s.values is not idx.values
assert s.index is idx
assert s.name == idx.name

# name kwarg
idx = idx
s = idx.to_series(name="__test")

assert s.values is not idx.values
assert s.index is not idx
assert s.name != idx.name


def test_to_flat_index(idx):
expected = pd.Index(
(
Expand Down
6 changes: 0 additions & 6 deletions pandas/tests/indexes/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,6 @@ def test_no_millisecond_field(self):
with pytest.raises(AttributeError, match=msg):
DatetimeIndex([]).millisecond

def test_hash_error(self):
index = period_range("20010101", periods=10)
msg = f"unhashable type: '{type(index).__name__}'"
with pytest.raises(TypeError, match=msg):
hash(index)

def test_make_time_series(self):
index = period_range(freq="A", start="1/1/2001", end="12/1/2009")
series = Series(1, index=index)
Expand Down
63 changes: 60 additions & 3 deletions pandas/tests/indexes/test_any_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
"""
import pytest

import pandas._testing as tm


def test_boolean_context_compat(indices):
with pytest.raises(ValueError, match="The truth value of a"):
if indices:
pass


def test_sort(indices):
msg = "cannot sort an Index object in-place, use sort_values instead"
Expand All @@ -27,9 +35,58 @@ def test_mutability(indices):


def test_wrong_number_names(indices):
names = indices.nlevels * ["apple", "banana", "carrot"]
with pytest.raises(ValueError, match="^Length"):
indices.names = ["apple", "banana", "carrot"]
indices.names = names


class TestConversion:
def test_to_series(self, indices):
# assert that we are creating a copy of the index

ser = indices.to_series()
assert ser.values is not indices.values
assert ser.index is not indices
assert ser.name == indices.name

def test_to_series_with_arguments(self, indices):
# GH#18699

# index kwarg
ser = indices.to_series(index=indices)

assert ser.values is not indices.values
assert ser.index is indices
assert ser.name == indices.name

# name kwarg
ser = indices.to_series(name="__test")

assert ser.values is not indices.values
assert ser.index is not indices
assert ser.name != indices.name

def test_tolist_matches_list(self, indices):
assert indices.tolist() == list(indices)


class TestRoundTrips:
def test_pickle_roundtrip(self, indices):
result = tm.round_trip_pickle(indices)
tm.assert_index_equal(result, indices)
if result.nlevels > 1:
# GH#8367 round-trip with timezone
assert indices.equal_levels(result)


class TestIndexing:
def test_slice_keeps_name(self, indices):
assert indices.name == indices[1:].name


def test_tolist_matches_list(indices):
assert indices.tolist() == list(indices)
class TestRendering:
def test_str(self, indices):
Copy link
Member

Choose a reason for hiding this comment

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

hmm, may need to rethink this test now testing over all indexes since "tuples": MultiIndex.from_tuples(zip(["foo", "bar", "baz"], [1, 2, 3])), so "foo" is already in str(indices)

# test the string repr
indices.name = "foo"
assert "'foo'" in str(indices)
assert type(indices).__name__ in str(indices)
10 changes: 5 additions & 5 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1823,17 +1823,17 @@ def test_isin_level_kwarg(self, level, index):
index.name = "foobar"
tm.assert_numpy_array_equal(expected, index.isin(values, level="foobar"))

@pytest.mark.parametrize("level", [2, 10, -3])
def test_isin_level_kwarg_bad_level_raises(self, level, indices):
def test_isin_level_kwarg_bad_level_raises(self, indices):
index = indices
with pytest.raises(IndexError, match="Too many levels"):
index.isin([], level=level)
for level in [10, index.nlevels, -(index.nlevels + 1)]:
with pytest.raises(IndexError, match="Too many levels"):
index.isin([], level=level)

@pytest.mark.parametrize("label", [1.0, "foobar", "xyzzy", np.nan])
def test_isin_level_kwarg_bad_label_raises(self, label, indices):
index = indices
if isinstance(index, MultiIndex):
index = index.rename(["foo", "bar"])
index = index.rename(["foo", "bar"] + index.names[2:])
Copy link
Member

Choose a reason for hiding this comment

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

hmm, I think the intention of the explicitly renaming was to ensure that the labels being tested were not already in the index.

msg = f"'Level {label} not found'"
else:
index = index.rename("foo")
Expand Down
4 changes: 0 additions & 4 deletions pandas/tests/indexes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,6 @@ def test_to_flat_index(self, indices):
result = indices.to_flat_index()
tm.assert_index_equal(result, indices)

def test_wrong_number_names(self, indices):
with pytest.raises(ValueError, match="^Length"):
indices.names = ["apple", "banana", "carrot"]

def test_set_name_methods(self, indices):
new_name = "This is the new name for this index"

Expand Down
4 changes: 0 additions & 4 deletions pandas/tests/indexes/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,10 +648,6 @@ def test_take_fill_value(self):
with pytest.raises(IndexError):
idx.take(np.array([1, -5]))

def test_slice_keep_name(self):
idx = self._holder([1, 2], name="asdf")
assert idx.name == idx[1:].name


class TestInt64Index(NumericInt):
_dtype = "int64"
Expand Down
13 changes: 0 additions & 13 deletions pandas/tests/indexes/timedeltas/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,6 @@ def test_pass_TimedeltaIndex_to_index(self):

tm.assert_numpy_array_equal(idx.values, expected.values)

def test_pickle(self):

rng = timedelta_range("1 days", periods=10)
rng_p = tm.round_trip_pickle(rng)
tm.assert_index_equal(rng, rng_p)

def test_hash_error(self):
index = timedelta_range("1 days", periods=10)
with pytest.raises(
TypeError, match=(f"unhashable type: {repr(type(index).__name__)}")
):
hash(index)

def test_append_numpy_bug_1681(self):

td = timedelta_range("1 days", "10 days", freq="2D")
Expand Down