From d638d9b9f16b165c4e2535edf368fd650f841289 Mon Sep 17 00:00:00 2001 From: patrick Date: Thu, 7 Jul 2022 20:04:05 +0200 Subject: [PATCH] Clean tests from closed usage --- pandas/tests/arrays/interval/test_interval.py | 10 ++-- pandas/tests/arrays/test_array.py | 2 +- pandas/tests/dtypes/test_dtypes.py | 10 ++-- .../indexes/interval/test_constructors.py | 24 ++++----- .../tests/indexes/interval/test_indexing.py | 8 +-- pandas/tests/indexes/interval/test_pickle.py | 7 +-- pandas/tests/indexes/interval/test_setops.py | 50 +++++++++---------- pandas/tests/series/test_constructors.py | 4 +- 8 files changed, 56 insertions(+), 59 deletions(-) diff --git a/pandas/tests/arrays/interval/test_interval.py b/pandas/tests/arrays/interval/test_interval.py index 7ca86408a7f59..a68e77167d113 100644 --- a/pandas/tests/arrays/interval/test_interval.py +++ b/pandas/tests/arrays/interval/test_interval.py @@ -60,12 +60,12 @@ def test_is_empty(self, constructor, left, right, closed): class TestMethods: - @pytest.mark.parametrize("new_closed", ["left", "right", "both", "neither"]) - def test_set_closed(self, closed, new_closed): + @pytest.mark.parametrize("new_inclusive", ["left", "right", "both", "neither"]) + def test_set_inclusive(self, closed, new_inclusive): # GH 21670 array = IntervalArray.from_breaks(range(10), inclusive=closed) - result = array.set_closed(new_closed) - expected = IntervalArray.from_breaks(range(10), inclusive=new_closed) + result = array.set_closed(new_inclusive) + expected = IntervalArray.from_breaks(range(10), inclusive=new_inclusive) tm.assert_extension_array_equal(result, expected) @pytest.mark.parametrize( @@ -134,7 +134,7 @@ def test_set_na(self, left_right_dtypes): tm.assert_extension_array_equal(result, expected) - def test_setitem_mismatched_closed(self): + def test_setitem_mismatched_inclusive(self): arr = IntervalArray.from_breaks(range(4), "right") orig = arr.copy() other = arr.set_closed("both") diff --git a/pandas/tests/arrays/test_array.py b/pandas/tests/arrays/test_array.py index f7f015cbe4a23..79e73fec706f1 100644 --- a/pandas/tests/arrays/test_array.py +++ b/pandas/tests/arrays/test_array.py @@ -298,7 +298,7 @@ def test_array_inference(data, expected): [ # mix of frequencies [pd.Period("2000", "D"), pd.Period("2001", "A")], - # mix of closed + # mix of inclusive [pd.Interval(0, 1, "left"), pd.Interval(1, 2, "right")], # Mix of timezones [pd.Timestamp("2000", tz="CET"), pd.Timestamp("2000", tz="UTC")], diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index e127fe27b6209..00fdf5af3dbe7 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -593,13 +593,13 @@ def test_construction_string_regex(self, subtype): @pytest.mark.parametrize( "subtype", ["interval[int64]", "Interval[int64]", "int64", np.dtype("int64")] ) - def test_construction_allows_closed_none(self, subtype): + def test_construction_allows_inclusive_none(self, subtype): # GH#38394 dtype = IntervalDtype(subtype) assert dtype.inclusive is None - def test_closed_mismatch(self): + def test_inclusive_mismatch(self): msg = "'inclusive' keyword does not match value specified in dtype string" with pytest.raises(ValueError, match=msg): IntervalDtype("interval[int64, left]", "right") @@ -638,7 +638,7 @@ def test_construction_errors(self, subtype): with pytest.raises(TypeError, match=msg): IntervalDtype(subtype) - def test_closed_must_match(self): + def test_inclusive_must_match(self): # GH#37933 dtype = IntervalDtype(np.float64, "left") @@ -646,7 +646,7 @@ def test_closed_must_match(self): with pytest.raises(ValueError, match=msg): IntervalDtype(dtype, inclusive="both") - def test_closed_invalid(self): + def test_inclusive_invalid(self): with pytest.raises(ValueError, match="inclusive must be one of"): IntervalDtype(np.float64, "foo") @@ -822,7 +822,7 @@ def test_not_string(self): # GH30568: though IntervalDtype has object kind, it cannot be string assert not is_string_dtype(IntervalDtype()) - def test_unpickling_without_closed(self): + def test_unpickling_without_inclusive(self): # GH#38394 dtype = IntervalDtype("interval") diff --git a/pandas/tests/indexes/interval/test_constructors.py b/pandas/tests/indexes/interval/test_constructors.py index 1966f344356a3..a23f66d241cd9 100644 --- a/pandas/tests/indexes/interval/test_constructors.py +++ b/pandas/tests/indexes/interval/test_constructors.py @@ -104,8 +104,8 @@ def test_constructor_dtype(self, constructor, breaks, subtype): timedelta_range("1 day", periods=5), ], ) - def test_constructor_pass_closed(self, constructor, breaks): - # not passing closed to IntervalDtype, but to IntervalArray constructor + def test_constructor_pass_inclusive(self, constructor, breaks): + # not passing inclusive to IntervalDtype, but to IntervalArray constructor warn = None if isinstance(constructor, partial) and constructor.func is Index: # passing kwargs to Index is deprecated @@ -193,7 +193,7 @@ def test_generic_errors(self, constructor): # filler input data to be used when supplying invalid kwargs filler = self.get_kwargs_from_breaks(range(10)) - # invalid closed + # invalid inclusive msg = "inclusive must be one of 'right', 'left', 'both', 'neither'" with pytest.raises(ValueError, match=msg): constructor(inclusive="invalid", **filler) @@ -399,9 +399,9 @@ def test_constructor_string(self): pass def test_constructor_errors(self, constructor): - # mismatched closed within intervals with no constructor override + # mismatched inclusive within intervals with no constructor override ivs = [Interval(0, 1, inclusive="right"), Interval(2, 3, inclusive="left")] - msg = "intervals must all be closed on the same side" + msg = "intervals must all be inclusive on the same side" with pytest.raises(ValueError, match=msg): constructor(ivs) @@ -420,7 +420,7 @@ def test_constructor_errors(self, constructor): @pytest.mark.filterwarnings("ignore:Passing keywords other:FutureWarning") @pytest.mark.parametrize( - "data, closed", + "data, inclusive", [ ([], "both"), ([np.nan, np.nan], "neither"), @@ -438,14 +438,14 @@ def test_constructor_errors(self, constructor): (IntervalIndex.from_breaks(range(5), inclusive="both"), "right"), ], ) - def test_override_inferred_closed(self, constructor, data, closed): + def test_override_inferred_inclusive(self, constructor, data, inclusive): # GH 19370 if isinstance(data, IntervalIndex): tuples = data.to_tuples() else: tuples = [(iv.left, iv.right) if notna(iv) else iv for iv in data] - expected = IntervalIndex.from_tuples(tuples, inclusive=closed) - result = constructor(data, inclusive=closed) + expected = IntervalIndex.from_tuples(tuples, inclusive=inclusive) + result = constructor(data, inclusive=inclusive) tm.assert_index_equal(result, expected) @pytest.mark.parametrize( @@ -460,7 +460,7 @@ def test_index_object_dtype(self, values_constructor): assert type(result) is Index tm.assert_numpy_array_equal(result.values, np.array(values)) - def test_index_mixed_closed(self): + def test_index_mixed_inclusive(self): # GH27172 intervals = [ Interval(0, 1, inclusive="left"), @@ -473,8 +473,8 @@ def test_index_mixed_closed(self): tm.assert_index_equal(result, expected) -def test_dtype_closed_mismatch(): - # GH#38394 closed specified in both dtype and IntervalIndex constructor +def test_dtype_inclusive_mismatch(): + # GH#38394 dtype = IntervalDtype(np.int64, "left") diff --git a/pandas/tests/indexes/interval/test_indexing.py b/pandas/tests/indexes/interval/test_indexing.py index 4cf754a7e52e0..e05cb73cfe446 100644 --- a/pandas/tests/indexes/interval/test_indexing.py +++ b/pandas/tests/indexes/interval/test_indexing.py @@ -76,12 +76,12 @@ def test_get_loc_length_one_scalar(self, scalar, closed): with pytest.raises(KeyError, match=str(scalar)): index.get_loc(scalar) - @pytest.mark.parametrize("other_closed", ["left", "right", "both", "neither"]) + @pytest.mark.parametrize("other_inclusive", ["left", "right", "both", "neither"]) @pytest.mark.parametrize("left, right", [(0, 5), (-1, 4), (-1, 6), (6, 7)]) - def test_get_loc_length_one_interval(self, left, right, closed, other_closed): + def test_get_loc_length_one_interval(self, left, right, closed, other_inclusive): # GH 20921 index = IntervalIndex.from_tuples([(0, 5)], inclusive=closed) - interval = Interval(left, right, inclusive=other_closed) + interval = Interval(left, right, inclusive=other_inclusive) if interval == index[0]: result = index.get_loc(interval) assert result == 0 @@ -89,7 +89,7 @@ def test_get_loc_length_one_interval(self, left, right, closed, other_closed): with pytest.raises( KeyError, match=re.escape( - f"Interval({left}, {right}, inclusive='{other_closed}')" + f"Interval({left}, {right}, inclusive='{other_inclusive}')" ), ): index.get_loc(interval) diff --git a/pandas/tests/indexes/interval/test_pickle.py b/pandas/tests/indexes/interval/test_pickle.py index 7f5784b6d76b9..ef6db9c8a0513 100644 --- a/pandas/tests/indexes/interval/test_pickle.py +++ b/pandas/tests/indexes/interval/test_pickle.py @@ -1,13 +1,10 @@ -import pytest - from pandas import IntervalIndex import pandas._testing as tm class TestPickle: - @pytest.mark.parametrize("inclusive", ["left", "right", "both"]) - def test_pickle_round_trip_closed(self, inclusive): + def test_pickle_round_trip_inclusive(self, closed): # https://github.com/pandas-dev/pandas/issues/35658 - idx = IntervalIndex.from_tuples([(1, 2), (2, 3)], inclusive=inclusive) + idx = IntervalIndex.from_tuples([(1, 2), (2, 3)], inclusive=closed) result = tm.round_trip_pickle(idx) tm.assert_index_equal(result, idx) diff --git a/pandas/tests/indexes/interval/test_setops.py b/pandas/tests/indexes/interval/test_setops.py index 5933961cc0f9d..2e1f6f7925374 100644 --- a/pandas/tests/indexes/interval/test_setops.py +++ b/pandas/tests/indexes/interval/test_setops.py @@ -10,22 +10,22 @@ import pandas._testing as tm -def monotonic_index(start, end, dtype="int64", closed="right"): +def monotonic_index(start, end, dtype="int64", inclusive="right"): return IntervalIndex.from_breaks( - np.arange(start, end, dtype=dtype), inclusive=closed + np.arange(start, end, dtype=dtype), inclusive=inclusive ) -def empty_index(dtype="int64", closed="right"): - return IntervalIndex(np.array([], dtype=dtype), inclusive=closed) +def empty_index(dtype="int64", inclusive="right"): + return IntervalIndex(np.array([], dtype=dtype), inclusive=inclusive) class TestIntervalIndex: def test_union(self, closed, sort): - index = monotonic_index(0, 11, closed=closed) - other = monotonic_index(5, 13, closed=closed) + index = monotonic_index(0, 11, inclusive=closed) + other = monotonic_index(5, 13, inclusive=closed) - expected = monotonic_index(0, 13, closed=closed) + expected = monotonic_index(0, 13, inclusive=closed) result = index[::-1].union(other, sort=sort) if sort is None: tm.assert_index_equal(result, expected) @@ -41,12 +41,12 @@ def test_union(self, closed, sort): def test_union_empty_result(self, closed, sort): # GH 19101: empty result, same dtype - index = empty_index(dtype="int64", closed=closed) + index = empty_index(dtype="int64", inclusive=closed) result = index.union(index, sort=sort) tm.assert_index_equal(result, index) # GH 19101: empty result, different numeric dtypes -> common dtype is f8 - other = empty_index(dtype="float64", closed=closed) + other = empty_index(dtype="float64", inclusive=closed) result = index.union(other, sort=sort) expected = other tm.assert_index_equal(result, expected) @@ -54,7 +54,7 @@ def test_union_empty_result(self, closed, sort): other = index.union(index, sort=sort) tm.assert_index_equal(result, expected) - other = empty_index(dtype="uint64", closed=closed) + other = empty_index(dtype="uint64", inclusive=closed) result = index.union(other, sort=sort) tm.assert_index_equal(result, expected) @@ -62,10 +62,10 @@ def test_union_empty_result(self, closed, sort): tm.assert_index_equal(result, expected) def test_intersection(self, closed, sort): - index = monotonic_index(0, 11, closed=closed) - other = monotonic_index(5, 13, closed=closed) + index = monotonic_index(0, 11, inclusive=closed) + other = monotonic_index(5, 13, inclusive=closed) - expected = monotonic_index(5, 11, closed=closed) + expected = monotonic_index(5, 11, inclusive=closed) result = index[::-1].intersection(other, sort=sort) if sort is None: tm.assert_index_equal(result, expected) @@ -100,21 +100,21 @@ def test_intersection(self, closed, sort): tm.assert_index_equal(result, expected) def test_intersection_empty_result(self, closed, sort): - index = monotonic_index(0, 11, closed=closed) + index = monotonic_index(0, 11, inclusive=closed) # GH 19101: empty result, same dtype - other = monotonic_index(300, 314, closed=closed) - expected = empty_index(dtype="int64", closed=closed) + other = monotonic_index(300, 314, inclusive=closed) + expected = empty_index(dtype="int64", inclusive=closed) result = index.intersection(other, sort=sort) tm.assert_index_equal(result, expected) # GH 19101: empty result, different numeric dtypes -> common dtype is float64 - other = monotonic_index(300, 314, dtype="float64", closed=closed) + other = monotonic_index(300, 314, dtype="float64", inclusive=closed) result = index.intersection(other, sort=sort) expected = other[:0] tm.assert_index_equal(result, expected) - other = monotonic_index(300, 314, dtype="uint64", closed=closed) + other = monotonic_index(300, 314, dtype="uint64", inclusive=closed) result = index.intersection(other, sort=sort) tm.assert_index_equal(result, expected) @@ -136,7 +136,7 @@ def test_difference(self, closed, sort): # GH 19101: empty result, same dtype result = index.difference(index, sort=sort) - expected = empty_index(dtype="int64", closed=closed) + expected = empty_index(dtype="int64", inclusive=closed) tm.assert_index_equal(result, expected) # GH 19101: empty result, different dtypes @@ -147,7 +147,7 @@ def test_difference(self, closed, sort): tm.assert_index_equal(result, expected) def test_symmetric_difference(self, closed, sort): - index = monotonic_index(0, 11, closed=closed) + index = monotonic_index(0, 11, inclusive=closed) result = index[1:].symmetric_difference(index[:-1], sort=sort) expected = IntervalIndex([index[0], index[-1]]) if sort is None: @@ -156,7 +156,7 @@ def test_symmetric_difference(self, closed, sort): # GH 19101: empty result, same dtype result = index.symmetric_difference(index, sort=sort) - expected = empty_index(dtype="int64", closed=closed) + expected = empty_index(dtype="int64", inclusive=closed) if sort is None: tm.assert_index_equal(result, expected) assert tm.equalContents(result, expected) @@ -166,7 +166,7 @@ def test_symmetric_difference(self, closed, sort): index.left.astype("float64"), index.right, inclusive=closed ) result = index.symmetric_difference(other, sort=sort) - expected = empty_index(dtype="float64", closed=closed) + expected = empty_index(dtype="float64", inclusive=closed) tm.assert_index_equal(result, expected) @pytest.mark.filterwarnings("ignore:'<' not supported between:RuntimeWarning") @@ -174,7 +174,7 @@ def test_symmetric_difference(self, closed, sort): "op_name", ["union", "intersection", "difference", "symmetric_difference"] ) def test_set_incompatible_types(self, closed, op_name, sort): - index = monotonic_index(0, 11, closed=closed) + index = monotonic_index(0, 11, inclusive=closed) set_op = getattr(index, op_name) # TODO: standardize return type of non-union setops type(self vs other) @@ -187,8 +187,8 @@ def test_set_incompatible_types(self, closed, op_name, sort): tm.assert_index_equal(result, expected) # mixed closed -> cast to object - for other_closed in {"right", "left", "both", "neither"} - {closed}: - other = monotonic_index(0, 11, closed=other_closed) + for other_inclusive in {"right", "left", "both", "neither"} - {closed}: + other = monotonic_index(0, 11, inclusive=other_inclusive) expected = getattr(index.astype(object), op_name)(other, sort=sort) if op_name == "difference": expected = index diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 3dce22a06c1b2..1d0f3737fdf54 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -1181,8 +1181,8 @@ def test_constructor_infer_interval(self, data_constructor): @pytest.mark.parametrize( "data_constructor", [list, np.array], ids=["list", "ndarray[object]"] ) - def test_constructor_interval_mixed_closed(self, data_constructor): - # GH 23563: mixed closed results in object dtype (not interval dtype) + def test_constructor_interval_mixed_inclusive(self, data_constructor): + # GH 23563: mixed inclusive results in object dtype (not interval dtype) data = [Interval(0, 1, inclusive="both"), Interval(0, 2, inclusive="neither")] result = Series(data_constructor(data)) assert result.dtype == object