diff --git a/pandas/tests/indexes/categorical/test_indexing.py b/pandas/tests/indexes/categorical/test_indexing.py index 5d89fd3bb4bc3..798aa7188cb9a 100644 --- a/pandas/tests/indexes/categorical/test_indexing.py +++ b/pandas/tests/indexes/categorical/test_indexing.py @@ -300,8 +300,9 @@ def test_get_indexer_same_categories_different_order(self): class TestWhere: - @pytest.mark.parametrize("klass", [list, tuple, np.array, pd.Series]) - def test_where(self, klass): + def test_where(self, listlike_box_with_tuple): + klass = listlike_box_with_tuple + i = CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=False) cond = [True] * len(i) expected = i diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index a8684ca4d3c25..7ef210e200380 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -378,8 +378,9 @@ def test_numpy_repeat(self, simple_index): with pytest.raises(ValueError, match=msg): np.repeat(idx, rep, axis=0) - @pytest.mark.parametrize("klass", [list, tuple, np.array, Series]) - def test_where(self, klass, simple_index): + def test_where(self, listlike_box_with_tuple, simple_index): + klass = listlike_box_with_tuple + idx = simple_index if isinstance(idx, (DatetimeIndex, TimedeltaIndex)): # where does not preserve freq diff --git a/pandas/tests/indexes/conftest.py b/pandas/tests/indexes/conftest.py index ac4477e60d5dc..2eae51c62aa0d 100644 --- a/pandas/tests/indexes/conftest.py +++ b/pandas/tests/indexes/conftest.py @@ -1,5 +1,11 @@ +import numpy as np import pytest +from pandas import ( + Series, + array, +) + @pytest.fixture(params=[None, False]) def sort(request): @@ -25,3 +31,21 @@ def freq_sample(request): timedelta_range.. """ return request.param + + +@pytest.fixture(params=[list, np.array, array, Series]) +def listlike_box(request): + """ + Types that may be passed as the indexer to searchsorted. + """ + return request.param + + +# TODO: not clear if this _needs_ to be different from listlike_box or +# if that is just a historical artifact +@pytest.fixture(params=[list, tuple, np.array, Series]) +def listlike_box_with_tuple(request): + """ + Types that may be passed as the indexer to searchsorted. + """ + return request.param diff --git a/pandas/tests/indexes/datetimes/test_datetime.py b/pandas/tests/indexes/datetimes/test_datetime.py index b220ce486f80b..5c85221c5a753 100644 --- a/pandas/tests/indexes/datetimes/test_datetime.py +++ b/pandas/tests/indexes/datetimes/test_datetime.py @@ -17,29 +17,6 @@ class TestDatetimeIndex: - def test_time_loc(self): # GH8667 - from datetime import time - - from pandas._libs.index import _SIZE_CUTOFF - - ns = _SIZE_CUTOFF + np.array([-100, 100], dtype=np.int64) - key = time(15, 11, 30) - start = key.hour * 3600 + key.minute * 60 + key.second - step = 24 * 3600 - - for n in ns: - idx = date_range("2014-11-26", periods=n, freq="S") - ts = pd.Series(np.random.randn(n), index=idx) - i = np.arange(start, n, step) - - tm.assert_numpy_array_equal(ts.index.get_loc(key), i, check_dtype=False) - tm.assert_series_equal(ts[key], ts.iloc[i]) - - left, right = ts.copy(), ts.copy() - left[key] *= -10 - right.iloc[i] *= -10 - tm.assert_series_equal(left, right) - def test_time_overflow_for_32bit_machines(self): # GH8943. On some machines NumPy defaults to np.int32 (for example, # 32-bit Linux machines). In the function _generate_regular_range @@ -78,13 +55,6 @@ def test_week_of_month_frequency(self): expected = DatetimeIndex(dates, freq="WOM-1SAT") tm.assert_index_equal(result, expected) - def test_stringified_slice_with_tz(self): - # GH#2658 - start = "2013-01-07" - idx = date_range(start=start, freq="1d", periods=10, tz="US/Eastern") - df = DataFrame(np.arange(10), index=idx) - df["2013-01-14 23:44:34.437768-05:00":] # no exception here - def test_append_nondatetimeindex(self): rng = date_range("1/1/2000", periods=10) idx = Index(["a", "b", "c", "d"]) @@ -137,16 +107,6 @@ def test_misc_coverage(self): result = rng.groupby(rng.day) assert isinstance(list(result.values())[0][0], Timestamp) - def test_string_index_series_name_converted(self): - # #1644 - df = DataFrame(np.random.randn(10, 4), index=date_range("1/1/2000", periods=10)) - - result = df.loc["1/3/2000"] - assert result.name == df.index[2] - - result = df.T["1/3/2000"] - assert result.name == df.index[2] - def test_groupby_function_tuple_1677(self): df = DataFrame(np.random.rand(100), index=date_range("1/1/2000", periods=100)) monthly_group = df.groupby(lambda x: (x.year, x.month)) diff --git a/pandas/tests/indexes/datetimes/test_indexing.py b/pandas/tests/indexes/datetimes/test_indexing.py index 4ad85f7d4e30f..c3152b77d39df 100644 --- a/pandas/tests/indexes/datetimes/test_indexing.py +++ b/pandas/tests/indexes/datetimes/test_indexing.py @@ -507,6 +507,30 @@ def test_get_loc_time_obj(self): with tm.assert_produces_warning(FutureWarning, match="deprecated"): idx.get_loc(time(12, 30), method="pad") + def test_get_loc_time_obj2(self): + # GH#8667 + + from pandas._libs.index import _SIZE_CUTOFF + + ns = _SIZE_CUTOFF + np.array([-100, 100], dtype=np.int64) + key = time(15, 11, 30) + start = key.hour * 3600 + key.minute * 60 + key.second + step = 24 * 3600 + + for n in ns: + idx = date_range("2014-11-26", periods=n, freq="S") + ts = pd.Series(np.random.randn(n), index=idx) + locs = np.arange(start, n, step, dtype=np.intp) + + result = ts.index.get_loc(key) + tm.assert_numpy_array_equal(result, locs) + tm.assert_series_equal(ts[key], ts.iloc[locs]) + + left, right = ts.copy(), ts.copy() + left[key] *= -10 + right.iloc[locs] *= -10 + tm.assert_series_equal(left, right) + def test_get_loc_time_nat(self): # GH#35114 # Case where key's total microseconds happens to match iNaT % 1e6 // 1000 @@ -705,7 +729,7 @@ def test_maybe_cast_slice_duplicate_monotonic(self): assert result == expected -class TestDatetimeIndex: +class TestGetValue: def test_get_value(self): # specifically make sure we have test for np.datetime64 key dti = date_range("2016-01-01", periods=3) diff --git a/pandas/tests/indexes/datetimes/test_partial_slicing.py b/pandas/tests/indexes/datetimes/test_partial_slicing.py index c5b47053471eb..896c43db5e356 100644 --- a/pandas/tests/indexes/datetimes/test_partial_slicing.py +++ b/pandas/tests/indexes/datetimes/test_partial_slicing.py @@ -19,6 +19,23 @@ class TestSlicing: + def test_string_index_series_name_converted(self): + # GH#1644 + df = DataFrame(np.random.randn(10, 4), index=date_range("1/1/2000", periods=10)) + + result = df.loc["1/3/2000"] + assert result.name == df.index[2] + + result = df.T["1/3/2000"] + assert result.name == df.index[2] + + def test_stringified_slice_with_tz(self): + # GH#2658 + start = "2013-01-07" + idx = date_range(start=start, freq="1d", periods=10, tz="US/Eastern") + df = DataFrame(np.arange(10), index=idx) + df["2013-01-14 23:44:34.437768-05:00":] # no exception here + def test_return_type_doesnt_depend_on_monotonicity(self): # GH#24892 we get Series back regardless of whether our DTI is monotonic dti = date_range(start="2015-5-13 23:59:00", freq="min", periods=3) diff --git a/pandas/tests/indexes/interval/test_base.py b/pandas/tests/indexes/interval/test_base.py index aa88bca2faec9..411e76ca5d8b7 100644 --- a/pandas/tests/indexes/interval/test_base.py +++ b/pandas/tests/indexes/interval/test_base.py @@ -1,10 +1,7 @@ import numpy as np import pytest -from pandas import ( - IntervalIndex, - Series, -) +from pandas import IntervalIndex import pandas._testing as tm from pandas.tests.indexes.common import Base @@ -46,8 +43,9 @@ def test_take(self, closed): expected = IntervalIndex.from_arrays([0, 0, 1], [1, 1, 2], closed=closed) tm.assert_index_equal(result, expected) - @pytest.mark.parametrize("klass", [list, tuple, np.array, Series]) - def test_where(self, simple_index, klass): + def test_where(self, simple_index, listlike_box_with_tuple): + klass = listlike_box_with_tuple + idx = simple_index cond = [True] * len(idx) expected = idx diff --git a/pandas/tests/indexes/interval/test_interval.py b/pandas/tests/indexes/interval/test_interval.py index ce8323199ce62..321d1aa34b9af 100644 --- a/pandas/tests/indexes/interval/test_interval.py +++ b/pandas/tests/indexes/interval/test_interval.py @@ -934,15 +934,14 @@ def test_dir(): assert "str" not in result -@pytest.mark.parametrize("klass", [list, np.array, pd.array, pd.Series]) -def test_searchsorted_different_argument_classes(klass): +def test_searchsorted_different_argument_classes(listlike_box): # https://github.com/pandas-dev/pandas/issues/32762 values = IntervalIndex([Interval(0, 1), Interval(1, 2)]) - result = values.searchsorted(klass(values)) + result = values.searchsorted(listlike_box(values)) expected = np.array([0, 1], dtype=result.dtype) tm.assert_numpy_array_equal(result, expected) - result = values._data.searchsorted(klass(values)) + result = values._data.searchsorted(listlike_box(values)) tm.assert_numpy_array_equal(result, expected) diff --git a/pandas/tests/indexes/multi/test_indexing.py b/pandas/tests/indexes/multi/test_indexing.py index 405b41c829a2f..99322f474dd9e 100644 --- a/pandas/tests/indexes/multi/test_indexing.py +++ b/pandas/tests/indexes/multi/test_indexing.py @@ -720,13 +720,12 @@ def test_where(self): with pytest.raises(NotImplementedError, match=msg): i.where(True) - @pytest.mark.parametrize("klass", [list, tuple, np.array, pd.Series]) - def test_where_array_like(self, klass): - i = MultiIndex.from_tuples([("A", 1), ("A", 2)]) + def test_where_array_like(self, listlike_box_with_tuple): + mi = MultiIndex.from_tuples([("A", 1), ("A", 2)]) cond = [False, True] msg = r"\.where is not supported for MultiIndex operations" with pytest.raises(NotImplementedError, match=msg): - i.where(klass(cond)) + mi.where(listlike_box_with_tuple(cond)) class TestContains: diff --git a/pandas/tests/indexes/numeric/test_indexing.py b/pandas/tests/indexes/numeric/test_indexing.py index be05d5d8a9cae..727dd4baf2318 100644 --- a/pandas/tests/indexes/numeric/test_indexing.py +++ b/pandas/tests/indexes/numeric/test_indexing.py @@ -395,15 +395,14 @@ class TestWhere: UInt64Index(np.arange(5, dtype="uint64")), ], ) - @pytest.mark.parametrize("klass", [list, tuple, np.array, Series]) - def test_where(self, klass, index): + def test_where(self, listlike_box_with_tuple, index): cond = [True] * len(index) expected = index - result = index.where(klass(cond)) + result = index.where(listlike_box_with_tuple(cond)) cond = [False] + [True] * (len(index) - 1) expected = Float64Index([index._na_value] + index[1:].tolist()) - result = index.where(klass(cond)) + result = index.where(listlike_box_with_tuple(cond)) tm.assert_index_equal(result, expected) def test_where_uint64(self): diff --git a/pandas/tests/indexes/period/test_indexing.py b/pandas/tests/indexes/period/test_indexing.py index cef045766efcc..78afcf2fdc78a 100644 --- a/pandas/tests/indexes/period/test_indexing.py +++ b/pandas/tests/indexes/period/test_indexing.py @@ -602,17 +602,16 @@ def test_get_indexer2(self): class TestWhere: - @pytest.mark.parametrize("klass", [list, tuple, np.array, Series]) - def test_where(self, klass): + def test_where(self, listlike_box_with_tuple): i = period_range("20130101", periods=5, freq="D") cond = [True] * len(i) expected = i - result = i.where(klass(cond)) + result = i.where(listlike_box_with_tuple(cond)) tm.assert_index_equal(result, expected) cond = [False] + [True] * (len(i) - 1) expected = PeriodIndex([NaT] + i[1:].tolist(), freq="D") - result = i.where(klass(cond)) + result = i.where(listlike_box_with_tuple(cond)) tm.assert_index_equal(result, expected) def test_where_other(self): diff --git a/pandas/tests/indexes/period/test_searchsorted.py b/pandas/tests/indexes/period/test_searchsorted.py index 27e998284c189..b9863d1bb019a 100644 --- a/pandas/tests/indexes/period/test_searchsorted.py +++ b/pandas/tests/indexes/period/test_searchsorted.py @@ -7,8 +7,6 @@ NaT, Period, PeriodIndex, - Series, - array, ) import pandas._testing as tm @@ -37,17 +35,16 @@ def test_searchsorted(self, freq): with pytest.raises(IncompatibleFrequency, match=msg): pidx.searchsorted(Period("2014-01-01", freq="5D")) - @pytest.mark.parametrize("klass", [list, np.array, array, Series]) - def test_searchsorted_different_argument_classes(self, klass): + def test_searchsorted_different_argument_classes(self, listlike_box): pidx = PeriodIndex( ["2014-01-01", "2014-01-02", "2014-01-03", "2014-01-04", "2014-01-05"], freq="D", ) - result = pidx.searchsorted(klass(pidx)) + result = pidx.searchsorted(listlike_box(pidx)) expected = np.arange(len(pidx), dtype=result.dtype) tm.assert_numpy_array_equal(result, expected) - result = pidx._data.searchsorted(klass(pidx)) + result = pidx._data.searchsorted(listlike_box(pidx)) tm.assert_numpy_array_equal(result, expected) def test_searchsorted_invalid(self): diff --git a/pandas/tests/indexes/timedeltas/test_searchsorted.py b/pandas/tests/indexes/timedeltas/test_searchsorted.py index 8a48da91ef31d..710571ef38397 100644 --- a/pandas/tests/indexes/timedeltas/test_searchsorted.py +++ b/pandas/tests/indexes/timedeltas/test_searchsorted.py @@ -2,23 +2,20 @@ import pytest from pandas import ( - Series, TimedeltaIndex, Timestamp, - array, ) import pandas._testing as tm class TestSearchSorted: - @pytest.mark.parametrize("klass", [list, np.array, array, Series]) - def test_searchsorted_different_argument_classes(self, klass): + def test_searchsorted_different_argument_classes(self, listlike_box): idx = TimedeltaIndex(["1 day", "2 days", "3 days"]) - result = idx.searchsorted(klass(idx)) + result = idx.searchsorted(listlike_box(idx)) expected = np.arange(len(idx), dtype=result.dtype) tm.assert_numpy_array_equal(result, expected) - result = idx._data.searchsorted(klass(idx)) + result = idx._data.searchsorted(listlike_box(idx)) tm.assert_numpy_array_equal(result, expected) @pytest.mark.parametrize(