Skip to content

REF: organize base class Index tests #31864

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 13 commits into from
Feb 22, 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
61 changes: 61 additions & 0 deletions pandas/tests/indexes/base_class/test_reshape.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""
Tests for ndarray-like method on the base Index class
"""
import pytest

import pandas as pd
from pandas import Index
import pandas._testing as tm


class TestReshape:
def test_repeat(self):
repeats = 2
index = pd.Index([1, 2, 3])
expected = pd.Index([1, 1, 2, 2, 3, 3])

result = index.repeat(repeats)
tm.assert_index_equal(result, expected)

def test_insert(self):

# GH 7256
# validate neg/pos inserts
result = Index(["b", "c", "d"])

# test 0th element
tm.assert_index_equal(Index(["a", "b", "c", "d"]), result.insert(0, "a"))

# test Nth element that follows Python list behavior
tm.assert_index_equal(Index(["b", "c", "e", "d"]), result.insert(-1, "e"))

# test loc +/- neq (0, -1)
tm.assert_index_equal(result.insert(1, "z"), result.insert(-2, "z"))

# test empty
null_index = Index([])
tm.assert_index_equal(Index(["a"]), null_index.insert(0, "a"))

@pytest.mark.parametrize(
"pos,expected",
[
(0, Index(["b", "c", "d"], name="index")),
(-1, Index(["a", "b", "c"], name="index")),
],
)
def test_delete(self, pos, expected):
index = Index(["a", "b", "c", "d"], name="index")
result = index.delete(pos)
tm.assert_index_equal(result, expected)
assert result.name == expected.name

def test_append_multiple(self):
index = Index(["a", "b", "c", "d", "e", "f"])

foos = [index[:2], index[2:4], index[4:]]
result = foos[0].append(foos[1:])
tm.assert_index_equal(result, index)

# empty
result = index.append([])
tm.assert_index_equal(result, index)
73 changes: 73 additions & 0 deletions pandas/tests/indexes/base_class/test_setops.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,49 @@
import numpy as np
import pytest

import pandas as pd
from pandas import Index, Series
import pandas._testing as tm
from pandas.core.algorithms import safe_sort


class TestIndexSetOps:
@pytest.mark.parametrize(
"method", ["union", "intersection", "difference", "symmetric_difference"]
)
def test_setops_disallow_true(self, method):
idx1 = pd.Index(["a", "b"])
idx2 = pd.Index(["b", "c"])

with pytest.raises(ValueError, match="The 'sort' keyword only takes"):
getattr(idx1, method)(idx2, sort=True)

def test_setops_preserve_object_dtype(self):
idx = pd.Index([1, 2, 3], dtype=object)
result = idx.intersection(idx[1:])
expected = idx[1:]
tm.assert_index_equal(result, expected)

# if other is not monotonic increasing, intersection goes through
# a different route
result = idx.intersection(idx[1:][::-1])
tm.assert_index_equal(result, expected)

result = idx._union(idx[1:], sort=None)
expected = idx
tm.assert_index_equal(result, expected)

result = idx.union(idx[1:], sort=None)
tm.assert_index_equal(result, expected)

# if other is not monotonic increasing, _union goes through
# a different route
result = idx._union(idx[1:][::-1], sort=None)
tm.assert_index_equal(result, expected)

result = idx.union(idx[1:][::-1], sort=None)
tm.assert_index_equal(result, expected)

def test_union_base(self):
index = Index([0, "a", 1, "b", 2, "c"])
first = index[3:]
Expand All @@ -28,6 +65,32 @@ def test_union_different_type_base(self, klass):

assert tm.equalContents(result, index)

def test_union_sort_other_incomparable(self):
# https://github.com/pandas-dev/pandas/issues/24959
idx = pd.Index([1, pd.Timestamp("2000")])
# default (sort=None)
with tm.assert_produces_warning(RuntimeWarning):
result = idx.union(idx[:1])

tm.assert_index_equal(result, idx)

# sort=None
with tm.assert_produces_warning(RuntimeWarning):
result = idx.union(idx[:1], sort=None)
tm.assert_index_equal(result, idx)

# sort=False
result = idx.union(idx[:1], sort=False)
tm.assert_index_equal(result, idx)

@pytest.mark.xfail(reason="Not implemented")
def test_union_sort_other_incomparable_true(self):
# TODO decide on True behaviour
# sort=True
idx = pd.Index([1, pd.Timestamp("2000")])
with pytest.raises(TypeError, match=".*"):
idx.union(idx[:1], sort=True)

@pytest.mark.parametrize("sort", [None, False])
def test_intersection_base(self, sort):
# (same results for py2 and py3 but sortedness not tested elsewhere)
Expand All @@ -50,6 +113,16 @@ def test_intersection_different_type_base(self, klass, sort):
result = first.intersection(klass(second.values), sort=sort)
assert tm.equalContents(result, second)

def test_intersect_nosort(self):
result = pd.Index(["c", "b", "a"]).intersection(["b", "a"])
expected = pd.Index(["b", "a"])
tm.assert_index_equal(result, expected)

def test_intersection_equal_sort(self):
idx = pd.Index(["c", "a", "b"])
tm.assert_index_equal(idx.intersection(idx, sort=False), idx)
tm.assert_index_equal(idx.intersection(idx, sort=None), idx)

@pytest.mark.parametrize("sort", [None, False])
def test_difference_base(self, sort):
# (same results for py2 and py3 but sortedness not tested elsewhere)
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/test_any_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@


def test_sort(indices):
with pytest.raises(TypeError):
msg = "cannot sort an Index object in-place, use sort_values instead"
with pytest.raises(TypeError, match=msg):
indices.sort()


Expand Down
Loading