Skip to content

Commit b977e52

Browse files
committed
Use fixtures in testing
1 parent a48f734 commit b977e52

File tree

4 files changed

+42
-37
lines changed

4 files changed

+42
-37
lines changed

doc/source/whatsnew/v0.24.0.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ Reshaping
411411
- Bug in :func:`pandas.concat` when joining resampled DataFrames with timezone aware index (:issue:`13783`)
412412
- Bug in :meth:`Series.combine_first` with ``datetime64[ns, tz]`` dtype which would return tz-naive result (:issue:`21469`)
413413
- Bug in :meth:`Series.where` and :meth:`DataFrame.where` with ``datetime64[ns, tz]`` dtype (:issue:`21546`)
414-
-
414+
- Bug in :func:`Index.union` and :func:`Index.intersection` where name of the ``Index`` of the result was not computed correctly for certain cases (:issue:`9943`, :issue:`9862`)
415415
-
416416

417417
Build Changes
@@ -425,6 +425,5 @@ Other
425425

426426
- :meth: `~pandas.io.formats.style.Styler.background_gradient` now takes a ``text_color_threshold`` parameter to automatically lighten the text color based on the luminance of the background color. This improves readability with dark background colors without the need to limit the background colormap range. (:issue:`21258`)
427427
- Require at least 0.28.2 version of ``cython`` to support read-only memoryviews (:issue:`21688`)
428-
- Bug in :func:`Index.union` and :func:`Index.intersection` where name of the ``Index`` of the result was not computed correctly for certain cases (:issue:`9943`, :issue:`9862`)
429428
-
430429
-

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2748,7 +2748,7 @@ def union(self, other):
27482748
return self._wrap_setop_result(other, result)
27492749

27502750
def _wrap_setop_result(self, other, result):
2751-
return self.__class__(result, name=get_op_result_name(self, other))
2751+
return self._constructor(result, name=get_op_result_name(self, other))
27522752

27532753
def intersection(self, other):
27542754
"""

pandas/tests/indexes/conftest.py

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
tm.makeTimedeltaIndex(100),
1515
tm.makeIntIndex(100),
1616
tm.makeUIntIndex(100),
17+
tm.makeRangeIndex(100),
1718
tm.makeFloatIndex(100),
1819
Index([True, False]),
1920
tm.makeCategoricalIndex(100),

pandas/tests/indexes/test_base.py

+39-34
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,10 @@ def test_empty_fancy_raises(self, attr):
713713

714714
@pytest.mark.parametrize("itm", [101, 'no_int'])
715715
def test_getitem_error(self, indices, itm):
716-
with pytest.raises(IndexError):
716+
error_type = IndexError
717+
if isinstance(indices, RangeIndex) and (itm == 'no_int'):
718+
error_type = ValueError
719+
with pytest.raises(error_type):
717720
indices[itm]
718721

719722
def test_intersection(self):
@@ -793,43 +796,41 @@ def test_intersect_str_dates(self):
793796
(None, 'B', None),
794797
(None, None, None),
795798
])
796-
def test_corner_union(self, fname, sname, expected_name):
799+
def test_corner_union(self, indices, fname, sname, expected_name):
797800
# GH 9943 9862
798801
# Test unions with various name combinations
799-
# Do not test MultiIndex
802+
# Do not test MultiIndex or repeats
800803

801-
skip_index_keys = ['tuples', 'repeats']
802-
for key, id in self.indices.items():
803-
if key in skip_index_keys:
804-
continue
804+
if isinstance(indices, MultiIndex) or not indices.is_unique:
805+
pytest.skip("Not for MultiIndex or repeated indices")
806+
807+
# Test copy.union(copy)
808+
first = indices.copy().set_names(fname)
809+
second = indices.copy().set_names(sname)
810+
union = first.union(second)
811+
expected = indices.copy().set_names(expected_name)
812+
tm.assert_index_equal(union, expected)
813+
814+
# Test copy.union(empty)
815+
first = indices.copy().set_names(fname)
816+
second = indices.drop(indices).set_names(sname)
817+
union = first.union(second)
818+
expected = indices.copy().set_names(expected_name)
819+
tm.assert_index_equal(union, expected)
820+
821+
# Test empty.union(copy)
822+
first = indices.drop(indices).set_names(fname)
823+
second = indices.copy().set_names(sname)
824+
union = first.union(second)
825+
expected = indices.copy().set_names(expected_name)
826+
tm.assert_index_equal(union, expected)
805827

806-
# Test copy.union(copy)
807-
first = id.copy().set_names(fname)
808-
second = id.copy().set_names(sname)
809-
union = first.union(second)
810-
expected = id.copy().set_names(expected_name)
811-
tm.assert_index_equal(union, expected)
812-
813-
# Test copy.union(empty)
814-
first = id.copy().set_names(fname)
815-
second = id.drop(id).set_names(sname)
816-
union = first.union(second)
817-
expected = id.copy().set_names(expected_name)
818-
tm.assert_index_equal(union, expected)
819-
820-
# Test empty.union(copy)
821-
first = id.drop(id).set_names(fname)
822-
second = id.copy().set_names(sname)
823-
union = first.union(second)
824-
expected = id.copy().set_names(expected_name)
825-
tm.assert_index_equal(union, expected)
826-
827-
# Test empty.union(empty)
828-
first = id.drop(id).set_names(fname)
829-
second = id.drop(id).set_names(sname)
830-
union = first.union(second)
831-
expected = id.drop(id).set_names(expected_name)
832-
tm.assert_index_equal(union, expected)
828+
# Test empty.union(empty)
829+
first = indices.drop(indices).set_names(fname)
830+
second = indices.drop(indices).set_names(sname)
831+
union = first.union(second)
832+
expected = indices.drop(indices).set_names(expected_name)
833+
tm.assert_index_equal(union, expected)
833834

834835
def test_chained_union(self):
835836
# Chained unions handles names correctly
@@ -2533,6 +2534,10 @@ def test_generated_op_names(opname, indices):
25332534
# pd.Index.__rsub__ does not exist; though the method does exist
25342535
# for subclasses. see GH#19723
25352536
return
2537+
if (isinstance(index, RangeIndex) and
2538+
opname in ['add', 'radd', 'sub', 'rsub',
2539+
'mul', 'rmul', 'truediv', 'rtruediv']):
2540+
pytest.skip("RangeIndex does operators differently")
25362541
opname = '__{name}__'.format(name=opname)
25372542
method = getattr(index, opname)
25382543
assert method.__name__ == opname

0 commit comments

Comments
 (0)