Skip to content

Commit 6e932f8

Browse files
TomAugspurgerjreback
authored andcommitted
TST: Fix indexes test inheritance / fixturization (pandas-dev#23825)
1 parent 161e8f3 commit 6e932f8

File tree

6 files changed

+357
-350
lines changed

6 files changed

+357
-350
lines changed

pandas/tests/indexes/common.py

-233
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import pandas.compat as compat
88
from pandas.compat import PY3
99

10-
from pandas.core.dtypes.common import needs_i8_conversion
1110
from pandas.core.dtypes.dtypes import CategoricalDtype
1211

1312
import pandas as pd
@@ -29,10 +28,6 @@ def setup_indices(self):
2928
for name, idx in self.indices.items():
3029
setattr(self, name, idx)
3130

32-
def verify_pickle(self, indices):
33-
unpickled = tm.round_trip_pickle(indices)
34-
assert indices.equals(unpickled)
35-
3631
def test_pickle_compat_construction(self):
3732
# need an object to create with
3833
pytest.raises(TypeError, self._holder)
@@ -214,11 +209,6 @@ def test_str(self):
214209
assert "'foo'" in str(idx)
215210
assert idx.__class__.__name__ in str(idx)
216211

217-
def test_dtype_str(self, indices):
218-
dtype = indices.dtype_str
219-
assert isinstance(dtype, compat.string_types)
220-
assert dtype == str(indices.dtype)
221-
222212
def test_repr_max_seq_item_setting(self):
223213
# GH10182
224214
idx = self.create_index()
@@ -227,44 +217,6 @@ def test_repr_max_seq_item_setting(self):
227217
repr(idx)
228218
assert '...' not in str(idx)
229219

230-
def test_wrong_number_names(self, indices):
231-
with pytest.raises(ValueError, match="^Length"):
232-
indices.names = ["apple", "banana", "carrot"]
233-
234-
def test_set_name_methods(self, indices):
235-
new_name = "This is the new name for this index"
236-
237-
# don't tests a MultiIndex here (as its tested separated)
238-
if isinstance(indices, MultiIndex):
239-
pytest.skip('Skip check for MultiIndex')
240-
original_name = indices.name
241-
new_ind = indices.set_names([new_name])
242-
assert new_ind.name == new_name
243-
assert indices.name == original_name
244-
res = indices.rename(new_name, inplace=True)
245-
246-
# should return None
247-
assert res is None
248-
assert indices.name == new_name
249-
assert indices.names == [new_name]
250-
# with pytest.raises(TypeError, match="list-like"):
251-
# # should still fail even if it would be the right length
252-
# ind.set_names("a")
253-
with pytest.raises(ValueError, match="Level must be None"):
254-
indices.set_names("a", level=0)
255-
256-
# rename in place just leaves tuples and other containers alone
257-
name = ('A', 'B')
258-
indices.rename(name, inplace=True)
259-
assert indices.name == name
260-
assert indices.names == [name]
261-
262-
def test_hash_error(self, indices):
263-
index = indices
264-
with pytest.raises(TypeError, match=("unhashable type: %r" %
265-
type(index).__name__)):
266-
hash(indices)
267-
268220
def test_copy_name(self):
269221
# gh-12309: Check that the "name" argument
270222
# passed at initialization is honored.
@@ -331,140 +283,6 @@ def test_ensure_copied_data(self):
331283
result._ndarray_values,
332284
check_same='same')
333285

334-
def test_copy_and_deepcopy(self, indices):
335-
from copy import copy, deepcopy
336-
337-
if isinstance(indices, MultiIndex):
338-
pytest.skip('Skip check for MultiIndex')
339-
340-
for func in (copy, deepcopy):
341-
idx_copy = func(indices)
342-
assert idx_copy is not indices
343-
assert idx_copy.equals(indices)
344-
345-
new_copy = indices.copy(deep=True, name="banana")
346-
assert new_copy.name == "banana"
347-
348-
def test_has_duplicates(self, indices):
349-
if type(indices) is not self._holder:
350-
pytest.skip('Can only check if we have the correct type')
351-
if not len(indices) or isinstance(indices, MultiIndex):
352-
# MultiIndex tested separately in:
353-
# tests/indexes/multi/test_unique_and_duplicates
354-
pytest.skip('Skip check for empty Index and MultiIndex')
355-
356-
idx = self._holder([indices[0]] * 5)
357-
assert idx.is_unique is False
358-
assert idx.has_duplicates is True
359-
360-
@pytest.mark.parametrize('keep', ['first', 'last', False])
361-
def test_duplicated(self, indices, keep):
362-
if type(indices) is not self._holder:
363-
pytest.skip('Can only check if we know the index type')
364-
if not len(indices) or isinstance(indices, (MultiIndex, RangeIndex)):
365-
# MultiIndex tested separately in:
366-
# tests/indexes/multi/test_unique_and_duplicates
367-
pytest.skip('Skip check for empty Index, MultiIndex, RangeIndex')
368-
369-
idx = self._holder(indices)
370-
if idx.has_duplicates:
371-
# We are testing the duplicated-method here, so we need to know
372-
# exactly which indices are duplicate and how (for the result).
373-
# This is not possible if "idx" has duplicates already, which we
374-
# therefore remove. This is seemingly circular, as drop_duplicates
375-
# invokes duplicated, but in the end, it all works out because we
376-
# cross-check with Series.duplicated, which is tested separately.
377-
idx = idx.drop_duplicates()
378-
379-
n, k = len(idx), 10
380-
duplicated_selection = np.random.choice(n, k * n)
381-
expected = pd.Series(duplicated_selection).duplicated(keep=keep).values
382-
idx = self._holder(idx.values[duplicated_selection])
383-
384-
result = idx.duplicated(keep=keep)
385-
tm.assert_numpy_array_equal(result, expected)
386-
387-
def test_unique(self, indices):
388-
# don't test a MultiIndex here (as its tested separated)
389-
# don't test a CategoricalIndex because categories change (GH 18291)
390-
if isinstance(indices, (MultiIndex, CategoricalIndex)):
391-
pytest.skip('Skip check for MultiIndex/CategoricalIndex')
392-
393-
# GH 17896
394-
expected = indices.drop_duplicates()
395-
for level in 0, indices.name, None:
396-
result = indices.unique(level=level)
397-
tm.assert_index_equal(result, expected)
398-
399-
for level in 3, 'wrong':
400-
pytest.raises((IndexError, KeyError), indices.unique, level=level)
401-
402-
def test_unique_na(self):
403-
idx = pd.Index([2, np.nan, 2, 1], name='my_index')
404-
expected = pd.Index([2, np.nan, 1], name='my_index')
405-
result = idx.unique()
406-
tm.assert_index_equal(result, expected)
407-
408-
def test_get_unique_index(self, indices):
409-
# MultiIndex tested separately
410-
if not len(indices) or isinstance(indices, MultiIndex):
411-
pytest.skip('Skip check for empty Index and MultiIndex')
412-
413-
idx = indices[[0] * 5]
414-
idx_unique = indices[[0]]
415-
416-
# We test against `idx_unique`, so first we make sure it's unique
417-
# and doesn't contain nans.
418-
assert idx_unique.is_unique is True
419-
try:
420-
assert idx_unique.hasnans is False
421-
except NotImplementedError:
422-
pass
423-
424-
for dropna in [False, True]:
425-
result = idx._get_unique_index(dropna=dropna)
426-
tm.assert_index_equal(result, idx_unique)
427-
428-
# nans:
429-
if not indices._can_hold_na:
430-
pytest.skip('Skip na-check if index cannot hold na')
431-
432-
if needs_i8_conversion(indices):
433-
vals = indices.asi8[[0] * 5]
434-
vals[0] = iNaT
435-
else:
436-
vals = indices.values[[0] * 5]
437-
vals[0] = np.nan
438-
439-
vals_unique = vals[:2]
440-
idx_nan = indices._shallow_copy(vals)
441-
idx_unique_nan = indices._shallow_copy(vals_unique)
442-
assert idx_unique_nan.is_unique is True
443-
444-
assert idx_nan.dtype == indices.dtype
445-
assert idx_unique_nan.dtype == indices.dtype
446-
447-
for dropna, expected in zip([False, True],
448-
[idx_unique_nan,
449-
idx_unique]):
450-
for i in [idx_nan, idx_unique_nan]:
451-
result = i._get_unique_index(dropna=dropna)
452-
tm.assert_index_equal(result, expected)
453-
454-
def test_sort(self, indices):
455-
pytest.raises(TypeError, indices.sort)
456-
457-
def test_mutability(self, indices):
458-
if not len(indices):
459-
pytest.skip('Skip check for empty Index')
460-
pytest.raises(TypeError, indices.__setitem__, 0, indices[0])
461-
462-
def test_view(self, indices):
463-
assert indices.view().name == indices.name
464-
465-
def test_compat(self, indices):
466-
assert indices.tolist() == list(indices)
467-
468286
def test_memory_usage(self):
469287
for name, index in compat.iteritems(self.indices):
470288
result = index.memory_usage()
@@ -523,12 +341,6 @@ def test_numpy_argsort(self):
523341
with pytest.raises(ValueError, match=msg):
524342
np.argsort(ind, order=('a', 'b'))
525343

526-
def test_pickle(self, indices):
527-
self.verify_pickle(indices)
528-
original_name, indices.name = indices.name, 'foo'
529-
self.verify_pickle(indices)
530-
indices.name = original_name
531-
532344
def test_take(self):
533345
indexer = [4, 3, 0, 2]
534346
for k, ind in self.indices.items():
@@ -1015,51 +827,6 @@ def test_join_self_unique(self, join_type):
1015827
joined = index.join(index, how=join_type)
1016828
assert (index == joined).all()
1017829

1018-
def test_searchsorted_monotonic(self, indices):
1019-
# GH17271
1020-
# not implemented for tuple searches in MultiIndex
1021-
# or Intervals searches in IntervalIndex
1022-
if isinstance(indices, (MultiIndex, IntervalIndex)):
1023-
pytest.skip('Skip check for MultiIndex/IntervalIndex')
1024-
1025-
# nothing to test if the index is empty
1026-
if indices.empty:
1027-
pytest.skip('Skip check for empty Index')
1028-
value = indices[0]
1029-
1030-
# determine the expected results (handle dupes for 'right')
1031-
expected_left, expected_right = 0, (indices == value).argmin()
1032-
if expected_right == 0:
1033-
# all values are the same, expected_right should be length
1034-
expected_right = len(indices)
1035-
1036-
# test _searchsorted_monotonic in all cases
1037-
# test searchsorted only for increasing
1038-
if indices.is_monotonic_increasing:
1039-
ssm_left = indices._searchsorted_monotonic(value, side='left')
1040-
assert expected_left == ssm_left
1041-
1042-
ssm_right = indices._searchsorted_monotonic(value, side='right')
1043-
assert expected_right == ssm_right
1044-
1045-
ss_left = indices.searchsorted(value, side='left')
1046-
assert expected_left == ss_left
1047-
1048-
ss_right = indices.searchsorted(value, side='right')
1049-
assert expected_right == ss_right
1050-
1051-
elif indices.is_monotonic_decreasing:
1052-
ssm_left = indices._searchsorted_monotonic(value, side='left')
1053-
assert expected_left == ssm_left
1054-
1055-
ssm_right = indices._searchsorted_monotonic(value, side='right')
1056-
assert expected_right == ssm_right
1057-
1058-
else:
1059-
# non-monotonic should raise.
1060-
with pytest.raises(ValueError):
1061-
indices._searchsorted_monotonic(value, side='left')
1062-
1063830
def test_map(self):
1064831
# callable
1065832
index = self.create_index()

pandas/tests/indexes/datetimelike.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ def test_str(self):
4747
if hasattr(idx, 'freq'):
4848
assert "freq='%s'" % idx.freqstr in str(idx)
4949

50-
def test_view(self, indices):
51-
super(DatetimeLike, self).test_view(indices)
52-
50+
def test_view(self):
5351
i = self.create_index()
5452

5553
i_view = i.view('i8')

0 commit comments

Comments
 (0)