Skip to content

Commit cd52920

Browse files
authored
TST: move to indices fixture instead of create_index (#32916)
1 parent ff91535 commit cd52920

File tree

10 files changed

+78
-135
lines changed

10 files changed

+78
-135
lines changed

pandas/conftest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,17 @@ def _create_multiindex():
368368
return mi
369369

370370

371+
def _create_mi_with_dt64tz_level():
372+
"""
373+
MultiIndex with a level that is a tzaware DatetimeIndex.
374+
"""
375+
# GH#8367 round trip with pickle
376+
return MultiIndex.from_product(
377+
[[1, 2], ["a", "b"], pd.date_range("20130101", periods=3, tz="US/Eastern")],
378+
names=["one", "two", "three"],
379+
)
380+
381+
371382
indices_dict = {
372383
"unicode": tm.makeUnicodeIndex(100),
373384
"string": tm.makeStringIndex(100),
@@ -384,6 +395,7 @@ def _create_multiindex():
384395
"interval": tm.makeIntervalIndex(100),
385396
"empty": Index([]),
386397
"tuples": MultiIndex.from_tuples(zip(["foo", "bar", "baz"], [1, 2, 3])),
398+
"mi-with-dt64tz-level": _create_mi_with_dt64tz_level(),
387399
"multi": _create_multiindex(),
388400
"repeats": Index([0, 0, 1, 1, 2, 2]),
389401
}

pandas/tests/indexes/common.py

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -49,34 +49,6 @@ def test_pickle_compat_construction(self):
4949
with pytest.raises(TypeError, match=msg):
5050
self._holder()
5151

52-
def test_to_series(self):
53-
# assert that we are creating a copy of the index
54-
55-
idx = self.create_index()
56-
s = idx.to_series()
57-
assert s.values is not idx.values
58-
assert s.index is not idx
59-
assert s.name == idx.name
60-
61-
def test_to_series_with_arguments(self):
62-
# GH18699
63-
64-
# index kwarg
65-
idx = self.create_index()
66-
s = idx.to_series(index=idx)
67-
68-
assert s.values is not idx.values
69-
assert s.index is idx
70-
assert s.name == idx.name
71-
72-
# name kwarg
73-
idx = self.create_index()
74-
s = idx.to_series(name="__test")
75-
76-
assert s.values is not idx.values
77-
assert s.index is not idx
78-
assert s.name != idx.name
79-
8052
@pytest.mark.parametrize("name", [None, "new_name"])
8153
def test_to_frame(self, name):
8254
# see GH-15230, GH-22580
@@ -198,15 +170,6 @@ def test_logical_compat(self):
198170
with pytest.raises(TypeError, match="cannot perform any"):
199171
idx.any()
200172

201-
def test_boolean_context_compat(self):
202-
203-
# boolean context compat
204-
idx = self.create_index()
205-
206-
with pytest.raises(ValueError, match="The truth value of a"):
207-
if idx:
208-
pass
209-
210173
def test_reindex_base(self):
211174
idx = self.create_index()
212175
expected = np.arange(idx.size, dtype=np.intp)
@@ -253,14 +216,6 @@ def test_repr_roundtrip(self):
253216
idx = self.create_index()
254217
tm.assert_index_equal(eval(repr(idx)), idx)
255218

256-
def test_str(self):
257-
258-
# test the string repr
259-
idx = self.create_index()
260-
idx.name = "foo"
261-
assert "'foo'" in str(idx)
262-
assert type(idx).__name__ in str(idx)
263-
264219
def test_repr_max_seq_item_setting(self):
265220
# GH10182
266221
idx = self.create_index()

pandas/tests/indexes/datetimes/test_datetime.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,6 @@ def test_week_of_month_frequency(self):
104104
expected = DatetimeIndex(dates, freq="WOM-1SAT")
105105
tm.assert_index_equal(result, expected)
106106

107-
def test_hash_error(self):
108-
index = date_range("20010101", periods=10)
109-
with pytest.raises(
110-
TypeError, match=f"unhashable type: '{type(index).__name__}'"
111-
):
112-
hash(index)
113-
114107
def test_stringified_slice_with_tz(self):
115108
# GH#2658
116109
start = "2013-01-07"

pandas/tests/indexes/multi/test_conversion.py

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@
22
import pytest
33

44
import pandas as pd
5-
from pandas import DataFrame, MultiIndex, date_range
5+
from pandas import DataFrame, MultiIndex
66
import pandas._testing as tm
77

88

9-
def test_tolist(idx):
10-
result = idx.tolist()
11-
exp = list(idx.values)
12-
assert result == exp
13-
14-
159
def test_to_numpy(idx):
1610
result = idx.to_numpy()
1711
exp = idx.values
@@ -129,47 +123,6 @@ def test_to_frame_resulting_column_order():
129123
assert result == expected
130124

131125

132-
def test_roundtrip_pickle_with_tz():
133-
return # FIXME: this can't be right?
134-
135-
# GH 8367
136-
# round-trip of timezone
137-
index = MultiIndex.from_product(
138-
[[1, 2], ["a", "b"], date_range("20130101", periods=3, tz="US/Eastern")],
139-
names=["one", "two", "three"],
140-
)
141-
unpickled = tm.round_trip_pickle(index)
142-
assert index.equal_levels(unpickled)
143-
144-
145-
def test_to_series(idx):
146-
# assert that we are creating a copy of the index
147-
148-
s = idx.to_series()
149-
assert s.values is not idx.values
150-
assert s.index is not idx
151-
assert s.name == idx.name
152-
153-
154-
def test_to_series_with_arguments(idx):
155-
# GH18699
156-
157-
# index kwarg
158-
s = idx.to_series(index=idx)
159-
160-
assert s.values is not idx.values
161-
assert s.index is idx
162-
assert s.name == idx.name
163-
164-
# name kwarg
165-
idx = idx
166-
s = idx.to_series(name="__test")
167-
168-
assert s.values is not idx.values
169-
assert s.index is not idx
170-
assert s.name != idx.name
171-
172-
173126
def test_to_flat_index(idx):
174127
expected = pd.Index(
175128
(

pandas/tests/indexes/period/test_period.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,6 @@ def test_no_millisecond_field(self):
105105
with pytest.raises(AttributeError, match=msg):
106106
DatetimeIndex([]).millisecond
107107

108-
def test_hash_error(self):
109-
index = period_range("20010101", periods=10)
110-
msg = f"unhashable type: '{type(index).__name__}'"
111-
with pytest.raises(TypeError, match=msg):
112-
hash(index)
113-
114108
def test_make_time_series(self):
115109
index = period_range(freq="A", start="1/1/2001", end="12/1/2009")
116110
series = Series(1, index=index)

pandas/tests/indexes/test_any_index.py

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
"""
66
import pytest
77

8+
import pandas._testing as tm
9+
10+
11+
def test_boolean_context_compat(indices):
12+
with pytest.raises(ValueError, match="The truth value of a"):
13+
if indices:
14+
pass
15+
816

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

2836

2937
def test_wrong_number_names(indices):
38+
names = indices.nlevels * ["apple", "banana", "carrot"]
3039
with pytest.raises(ValueError, match="^Length"):
31-
indices.names = ["apple", "banana", "carrot"]
40+
indices.names = names
41+
42+
43+
class TestConversion:
44+
def test_to_series(self, indices):
45+
# assert that we are creating a copy of the index
46+
47+
ser = indices.to_series()
48+
assert ser.values is not indices.values
49+
assert ser.index is not indices
50+
assert ser.name == indices.name
51+
52+
def test_to_series_with_arguments(self, indices):
53+
# GH#18699
54+
55+
# index kwarg
56+
ser = indices.to_series(index=indices)
57+
58+
assert ser.values is not indices.values
59+
assert ser.index is indices
60+
assert ser.name == indices.name
61+
62+
# name kwarg
63+
ser = indices.to_series(name="__test")
64+
65+
assert ser.values is not indices.values
66+
assert ser.index is not indices
67+
assert ser.name != indices.name
68+
69+
def test_tolist_matches_list(self, indices):
70+
assert indices.tolist() == list(indices)
71+
72+
73+
class TestRoundTrips:
74+
def test_pickle_roundtrip(self, indices):
75+
result = tm.round_trip_pickle(indices)
76+
tm.assert_index_equal(result, indices)
77+
if result.nlevels > 1:
78+
# GH#8367 round-trip with timezone
79+
assert indices.equal_levels(result)
80+
81+
82+
class TestIndexing:
83+
def test_slice_keeps_name(self, indices):
84+
assert indices.name == indices[1:].name
3285

3386

34-
def test_tolist_matches_list(indices):
35-
assert indices.tolist() == list(indices)
87+
class TestRendering:
88+
def test_str(self, indices):
89+
# test the string repr
90+
indices.name = "foo"
91+
assert "'foo'" in str(indices)
92+
assert type(indices).__name__ in str(indices)

pandas/tests/indexes/test_base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,17 +1823,17 @@ def test_isin_level_kwarg(self, level, index):
18231823
index.name = "foobar"
18241824
tm.assert_numpy_array_equal(expected, index.isin(values, level="foobar"))
18251825

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

18321832
@pytest.mark.parametrize("label", [1.0, "foobar", "xyzzy", np.nan])
18331833
def test_isin_level_kwarg_bad_label_raises(self, label, indices):
18341834
index = indices
18351835
if isinstance(index, MultiIndex):
1836-
index = index.rename(["foo", "bar"])
1836+
index = index.rename(["foo", "bar"] + index.names[2:])
18371837
msg = f"'Level {label} not found'"
18381838
else:
18391839
index = index.rename("foo")

pandas/tests/indexes/test_common.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,6 @@ def test_to_flat_index(self, indices):
125125
result = indices.to_flat_index()
126126
tm.assert_index_equal(result, indices)
127127

128-
def test_wrong_number_names(self, indices):
129-
with pytest.raises(ValueError, match="^Length"):
130-
indices.names = ["apple", "banana", "carrot"]
131-
132128
def test_set_name_methods(self, indices):
133129
new_name = "This is the new name for this index"
134130

pandas/tests/indexes/test_numeric.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -648,10 +648,6 @@ def test_take_fill_value(self):
648648
with pytest.raises(IndexError):
649649
idx.take(np.array([1, -5]))
650650

651-
def test_slice_keep_name(self):
652-
idx = self._holder([1, 2], name="asdf")
653-
assert idx.name == idx[1:].name
654-
655651

656652
class TestInt64Index(NumericInt):
657653
_dtype = "int64"

pandas/tests/indexes/timedeltas/test_timedelta.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,6 @@ def test_pass_TimedeltaIndex_to_index(self):
147147

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

150-
def test_pickle(self):
151-
152-
rng = timedelta_range("1 days", periods=10)
153-
rng_p = tm.round_trip_pickle(rng)
154-
tm.assert_index_equal(rng, rng_p)
155-
156-
def test_hash_error(self):
157-
index = timedelta_range("1 days", periods=10)
158-
with pytest.raises(
159-
TypeError, match=(f"unhashable type: {repr(type(index).__name__)}")
160-
):
161-
hash(index)
162-
163150
def test_append_numpy_bug_1681(self):
164151

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

0 commit comments

Comments
 (0)