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 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,7 @@ Other
- Fixed metadata propagation in the :class:`Series.dt`, :class:`Series.str` accessors, :class:`DataFrame.duplicated`, :class:`DataFrame.stack`, :class:`DataFrame.unstack`, :class:`DataFrame.pivot`, :class:`DataFrame.append`, :class:`DataFrame.diff`, :class:`DataFrame.applymap` and :class:`DataFrame.update` methods (:issue:`28283`, :issue:`37381`)
- Fixed metadata propagation when selecting columns with ``DataFrame.__getitem__`` (:issue:`28283`)
- Bug in :meth:`Index.intersection` with non-:class:`Index` failing to set the correct name on the returned :class:`Index` (:issue:`38111`)
- Bug in :meth:`RangeIndex.intersection` failing to set the correct name on the returned :class:`Index` in some corner cases (:issue:`38197`)
- Bug in :meth:`Index.union` behaving differently depending on whether operand is an :class:`Index` or other list-like (:issue:`36384`)
- Bug in :meth:`Index.intersection` with non-matching numeric dtypes casting to ``object`` dtype instead of minimal common dtype (:issue:`38122`)
- Passing an array with 2 or more dimensions to the :class:`Series` constructor now raises the more specific ``ValueError`` rather than a bare ``Exception`` (:issue:`35744`)
Expand Down
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
19 changes: 11 additions & 8 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,19 +508,22 @@ def intersection(self, other, sort=False):
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)

return self._intersection(other, sort=sort)
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):
if is_dtype_equal(other.dtype, self.dtype):
# Int64Index
result = super()._intersection(other, sort=sort)
return self._wrap_setop_result(other, result)
return super().intersection(other, sort=sort)
# 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 @@ -562,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