Skip to content

BUG: name attr in RangeIndex.intersection #38197

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 3 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 14 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1432,3 +1432,17 @@ def __init__(self, **kwargs):
registry.pop("testmem", None)
TestMemoryFS.test[0] = None
TestMemoryFS.store.clear()


@pytest.fixture(
params=[
("foo", None, None),
("Egon", "Venkman", None),
("NCC1701D", "NCC1701D", "NCC1701D"),
]
)
def names(request):
"""
A 3-tuple of names, the first two for operands, the last for a result.
"""
return request.param
20 changes: 17 additions & 3 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from pandas.core.dtypes.common import (
ensure_platform_int,
ensure_python_int,
is_dtype_equal,
is_float,
is_integer,
is_list_like,
Expand Down Expand Up @@ -504,13 +505,26 @@ def intersection(self, other, sort=False):
intersection : Index
"""
self._validate_sort_keyword(sort)
self._assert_can_do_setop(other)
other, _ = self._convert_can_do_setop(other)

if self.equals(other):
if self.equals(other) and not self.has_duplicates:
# has_duplicates check is unnecessary for RangeIndex, but
# used to match other subclasses.
return self._get_reconciled_name_object(other)

if not isinstance(other, RangeIndex):
if not is_dtype_equal(self.dtype, other.dtype):
return super().intersection(other, sort=sort)

result = self._intersection(other, sort=sort)
return self._wrap_setop_result(other, result)

def _intersection(self, other, sort=False):

if not isinstance(other, RangeIndex):
# Int64Index
return super()._intersection(other, sort=sort)

if not len(self) or not len(other):
return self._simple_new(_empty_range)

Expand Down Expand Up @@ -551,7 +565,7 @@ def intersection(self, other, sort=False):
if sort is None:
new_index = new_index.sort_values()

return self._wrap_setop_result(other, new_index)
return new_index

def _min_fitting_element(self, lower_limit: int) -> int:
"""Returns the smallest element greater than or equal to the limit"""
Expand Down
12 changes: 0 additions & 12 deletions pandas/tests/arithmetic/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,6 @@ def id_func(x):


# ------------------------------------------------------------------
@pytest.fixture(
params=[
("foo", None, None),
("Egon", "Venkman", None),
("NCC1701D", "NCC1701D", "NCC1701D"),
]
)
def names(request):
"""
A 3-tuple of names, the first two for operands, the last for a result.
"""
return request.param


@pytest.fixture(params=[1, np.array(1, dtype=np.int64)])
Expand Down
25 changes: 19 additions & 6 deletions pandas/tests/indexes/ranges/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ def test_intersection_mismatched_dtype(self, klass):
result = flt[:0].intersection(index)
tm.assert_index_equal(result, flt[:0], exact=True)

def test_intersection_empty(self, sort, names):
# name retention on empty intersections
index = RangeIndex(start=0, stop=20, step=2, name=names[0])

# empty other
result = index.intersection(index[:0].rename(names[1]), sort=sort)
tm.assert_index_equal(result, index[:0].rename(names[2]), exact=True)

# empty self
result = index[:0].intersection(index.rename(names[1]), sort=sort)
tm.assert_index_equal(result, index[:0].rename(names[2]), exact=True)

def test_intersection(self, sort):
# intersect with Int64Index
index = RangeIndex(start=0, stop=20, step=2)
Expand Down Expand Up @@ -78,12 +90,12 @@ def test_intersection(self, sort):
result = other.intersection(first, sort=sort).astype(int)
tm.assert_index_equal(result, expected)

index = RangeIndex(5)
index = RangeIndex(5, name="foo")

# intersect of non-overlapping indices
other = RangeIndex(5, 10, 1)
other = RangeIndex(5, 10, 1, name="foo")
result = index.intersection(other, sort=sort)
expected = RangeIndex(0, 0, 1)
expected = RangeIndex(0, 0, 1, name="foo")
tm.assert_index_equal(result, expected)

other = RangeIndex(-1, -5, -1)
Expand All @@ -100,11 +112,12 @@ def test_intersection(self, sort):
result = other.intersection(index, sort=sort)
tm.assert_index_equal(result, expected)

def test_intersection_non_overlapping_gcd(self, sort, names):
# intersection of non-overlapping values based on start value and gcd
index = RangeIndex(1, 10, 2)
other = RangeIndex(0, 10, 4)
index = RangeIndex(1, 10, 2, name=names[0])
other = RangeIndex(0, 10, 4, name=names[1])
result = index.intersection(other, sort=sort)
expected = RangeIndex(0, 0, 1)
expected = RangeIndex(0, 0, 1, name=names[2])
tm.assert_index_equal(result, expected)

def test_union_noncomparable(self, sort):
Expand Down