Skip to content

Commit aba63da

Browse files
committed
deduplicate test_duplicated
1 parent f10626f commit aba63da

File tree

2 files changed

+39
-40
lines changed

2 files changed

+39
-40
lines changed

pandas/tests/indexes/common.py

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -283,45 +283,6 @@ def test_ensure_copied_data(self):
283283
result._ndarray_values,
284284
check_same='same')
285285

286-
def test_has_duplicates(self, indices):
287-
if type(indices) is not self._holder:
288-
pytest.skip('Can only check if we have the correct type')
289-
if not len(indices) or isinstance(indices, MultiIndex):
290-
# MultiIndex tested separately in:
291-
# tests/indexes/multi/test_unique_and_duplicates
292-
pytest.skip('Skip check for empty Index and MultiIndex')
293-
294-
idx = self._holder([indices[0]] * 5)
295-
assert idx.is_unique is False
296-
assert idx.has_duplicates is True
297-
298-
@pytest.mark.parametrize('keep', ['first', 'last', False])
299-
def test_duplicated(self, indices, keep):
300-
if type(indices) is not self._holder:
301-
pytest.skip('Can only check if we know the index type')
302-
if not len(indices) or isinstance(indices, (MultiIndex, RangeIndex)):
303-
# MultiIndex tested separately in:
304-
# tests/indexes/multi/test_unique_and_duplicates
305-
pytest.skip('Skip check for empty Index, MultiIndex, RangeIndex')
306-
307-
idx = self._holder(indices)
308-
if idx.has_duplicates:
309-
# We are testing the duplicated-method here, so we need to know
310-
# exactly which indices are duplicate and how (for the result).
311-
# This is not possible if "idx" has duplicates already, which we
312-
# therefore remove. This is seemingly circular, as drop_duplicates
313-
# invokes duplicated, but in the end, it all works out because we
314-
# cross-check with Series.duplicated, which is tested separately.
315-
idx = idx.drop_duplicates()
316-
317-
n, k = len(idx), 10
318-
duplicated_selection = np.random.choice(n, k * n)
319-
expected = pd.Series(duplicated_selection).duplicated(keep=keep).values
320-
idx = self._holder(idx.values[duplicated_selection])
321-
322-
result = idx.duplicated(keep=keep)
323-
tm.assert_numpy_array_equal(result, expected)
324-
325286
def test_memory_usage(self):
326287
for name, index in compat.iteritems(self.indices):
327288
result = index.memory_usage()

pandas/tests/indexes/test_common.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from pandas.core.dtypes.common import needs_i8_conversion
1212

1313
import pandas as pd
14-
from pandas import CategoricalIndex, MultiIndex, compat
14+
from pandas import CategoricalIndex, MultiIndex, RangeIndex, compat
1515
import pandas.util.testing as tm
1616

1717

@@ -301,3 +301,41 @@ def test_pickle(self, indices):
301301
unpickled = tm.round_trip_pickle(indices)
302302
assert indices.equals(unpickled)
303303
indices.name = original_name
304+
305+
@pytest.mark.parametrize('keep', ['first', 'last', False])
306+
def test_duplicated(self, indices, keep):
307+
if not len(indices) or isinstance(indices, (MultiIndex, RangeIndex)):
308+
# MultiIndex tested separately in:
309+
# tests/indexes/multi/test_unique_and_duplicates
310+
pytest.skip('Skip check for empty Index, MultiIndex, RangeIndex')
311+
312+
holder = type(indices)
313+
314+
idx = holder(indices)
315+
if idx.has_duplicates:
316+
# We are testing the duplicated-method here, so we need to know
317+
# exactly which indices are duplicate and how (for the result).
318+
# This is not possible if "idx" has duplicates already, which we
319+
# therefore remove. This is seemingly circular, as drop_duplicates
320+
# invokes duplicated, but in the end, it all works out because we
321+
# cross-check with Series.duplicated, which is tested separately.
322+
idx = idx.drop_duplicates()
323+
324+
n, k = len(idx), 10
325+
duplicated_selection = np.random.choice(n, k * n)
326+
expected = pd.Series(duplicated_selection).duplicated(keep=keep).values
327+
idx = holder(idx.values[duplicated_selection])
328+
329+
result = idx.duplicated(keep=keep)
330+
tm.assert_numpy_array_equal(result, expected)
331+
332+
def test_has_duplicates(self, indices):
333+
holder = type(indices)
334+
if not len(indices) or isinstance(indices, MultiIndex):
335+
# MultiIndex tested separately in:
336+
# tests/indexes/multi/test_unique_and_duplicates
337+
pytest.skip('Skip check for empty Index and MultiIndex')
338+
339+
idx = holder([indices[0]] * 5)
340+
assert idx.is_unique is False
341+
assert idx.has_duplicates is True

0 commit comments

Comments
 (0)