Skip to content

Commit f4e7fdc

Browse files
authored
TST/REF: fixturize (#43918)
1 parent 3e8b7a4 commit f4e7fdc

13 files changed

+94
-79
lines changed

pandas/tests/indexes/categorical/test_indexing.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,9 @@ def test_get_indexer_same_categories_different_order(self):
300300

301301

302302
class TestWhere:
303-
@pytest.mark.parametrize("klass", [list, tuple, np.array, pd.Series])
304-
def test_where(self, klass):
303+
def test_where(self, listlike_box_with_tuple):
304+
klass = listlike_box_with_tuple
305+
305306
i = CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=False)
306307
cond = [True] * len(i)
307308
expected = i

pandas/tests/indexes/common.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,9 @@ def test_numpy_repeat(self, simple_index):
378378
with pytest.raises(ValueError, match=msg):
379379
np.repeat(idx, rep, axis=0)
380380

381-
@pytest.mark.parametrize("klass", [list, tuple, np.array, Series])
382-
def test_where(self, klass, simple_index):
381+
def test_where(self, listlike_box_with_tuple, simple_index):
382+
klass = listlike_box_with_tuple
383+
383384
idx = simple_index
384385
if isinstance(idx, (DatetimeIndex, TimedeltaIndex)):
385386
# where does not preserve freq

pandas/tests/indexes/conftest.py

+24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
import numpy as np
12
import pytest
23

4+
from pandas import (
5+
Series,
6+
array,
7+
)
8+
39

410
@pytest.fixture(params=[None, False])
511
def sort(request):
@@ -25,3 +31,21 @@ def freq_sample(request):
2531
timedelta_range..
2632
"""
2733
return request.param
34+
35+
36+
@pytest.fixture(params=[list, np.array, array, Series])
37+
def listlike_box(request):
38+
"""
39+
Types that may be passed as the indexer to searchsorted.
40+
"""
41+
return request.param
42+
43+
44+
# TODO: not clear if this _needs_ to be different from listlike_box or
45+
# if that is just a historical artifact
46+
@pytest.fixture(params=[list, tuple, np.array, Series])
47+
def listlike_box_with_tuple(request):
48+
"""
49+
Types that may be passed as the indexer to searchsorted.
50+
"""
51+
return request.param

pandas/tests/indexes/datetimes/test_datetime.py

-40
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,6 @@
1717

1818

1919
class TestDatetimeIndex:
20-
def test_time_loc(self): # GH8667
21-
from datetime import time
22-
23-
from pandas._libs.index import _SIZE_CUTOFF
24-
25-
ns = _SIZE_CUTOFF + np.array([-100, 100], dtype=np.int64)
26-
key = time(15, 11, 30)
27-
start = key.hour * 3600 + key.minute * 60 + key.second
28-
step = 24 * 3600
29-
30-
for n in ns:
31-
idx = date_range("2014-11-26", periods=n, freq="S")
32-
ts = pd.Series(np.random.randn(n), index=idx)
33-
i = np.arange(start, n, step)
34-
35-
tm.assert_numpy_array_equal(ts.index.get_loc(key), i, check_dtype=False)
36-
tm.assert_series_equal(ts[key], ts.iloc[i])
37-
38-
left, right = ts.copy(), ts.copy()
39-
left[key] *= -10
40-
right.iloc[i] *= -10
41-
tm.assert_series_equal(left, right)
42-
4320
def test_time_overflow_for_32bit_machines(self):
4421
# GH8943. On some machines NumPy defaults to np.int32 (for example,
4522
# 32-bit Linux machines). In the function _generate_regular_range
@@ -78,13 +55,6 @@ def test_week_of_month_frequency(self):
7855
expected = DatetimeIndex(dates, freq="WOM-1SAT")
7956
tm.assert_index_equal(result, expected)
8057

81-
def test_stringified_slice_with_tz(self):
82-
# GH#2658
83-
start = "2013-01-07"
84-
idx = date_range(start=start, freq="1d", periods=10, tz="US/Eastern")
85-
df = DataFrame(np.arange(10), index=idx)
86-
df["2013-01-14 23:44:34.437768-05:00":] # no exception here
87-
8858
def test_append_nondatetimeindex(self):
8959
rng = date_range("1/1/2000", periods=10)
9060
idx = Index(["a", "b", "c", "d"])
@@ -137,16 +107,6 @@ def test_misc_coverage(self):
137107
result = rng.groupby(rng.day)
138108
assert isinstance(list(result.values())[0][0], Timestamp)
139109

140-
def test_string_index_series_name_converted(self):
141-
# #1644
142-
df = DataFrame(np.random.randn(10, 4), index=date_range("1/1/2000", periods=10))
143-
144-
result = df.loc["1/3/2000"]
145-
assert result.name == df.index[2]
146-
147-
result = df.T["1/3/2000"]
148-
assert result.name == df.index[2]
149-
150110
def test_groupby_function_tuple_1677(self):
151111
df = DataFrame(np.random.rand(100), index=date_range("1/1/2000", periods=100))
152112
monthly_group = df.groupby(lambda x: (x.year, x.month))

pandas/tests/indexes/datetimes/test_indexing.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,30 @@ def test_get_loc_time_obj(self):
507507
with tm.assert_produces_warning(FutureWarning, match="deprecated"):
508508
idx.get_loc(time(12, 30), method="pad")
509509

510+
def test_get_loc_time_obj2(self):
511+
# GH#8667
512+
513+
from pandas._libs.index import _SIZE_CUTOFF
514+
515+
ns = _SIZE_CUTOFF + np.array([-100, 100], dtype=np.int64)
516+
key = time(15, 11, 30)
517+
start = key.hour * 3600 + key.minute * 60 + key.second
518+
step = 24 * 3600
519+
520+
for n in ns:
521+
idx = date_range("2014-11-26", periods=n, freq="S")
522+
ts = pd.Series(np.random.randn(n), index=idx)
523+
locs = np.arange(start, n, step, dtype=np.intp)
524+
525+
result = ts.index.get_loc(key)
526+
tm.assert_numpy_array_equal(result, locs)
527+
tm.assert_series_equal(ts[key], ts.iloc[locs])
528+
529+
left, right = ts.copy(), ts.copy()
530+
left[key] *= -10
531+
right.iloc[locs] *= -10
532+
tm.assert_series_equal(left, right)
533+
510534
def test_get_loc_time_nat(self):
511535
# GH#35114
512536
# Case where key's total microseconds happens to match iNaT % 1e6 // 1000
@@ -705,7 +729,7 @@ def test_maybe_cast_slice_duplicate_monotonic(self):
705729
assert result == expected
706730

707731

708-
class TestDatetimeIndex:
732+
class TestGetValue:
709733
def test_get_value(self):
710734
# specifically make sure we have test for np.datetime64 key
711735
dti = date_range("2016-01-01", periods=3)

pandas/tests/indexes/datetimes/test_partial_slicing.py

+17
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@
1919

2020

2121
class TestSlicing:
22+
def test_string_index_series_name_converted(self):
23+
# GH#1644
24+
df = DataFrame(np.random.randn(10, 4), index=date_range("1/1/2000", periods=10))
25+
26+
result = df.loc["1/3/2000"]
27+
assert result.name == df.index[2]
28+
29+
result = df.T["1/3/2000"]
30+
assert result.name == df.index[2]
31+
32+
def test_stringified_slice_with_tz(self):
33+
# GH#2658
34+
start = "2013-01-07"
35+
idx = date_range(start=start, freq="1d", periods=10, tz="US/Eastern")
36+
df = DataFrame(np.arange(10), index=idx)
37+
df["2013-01-14 23:44:34.437768-05:00":] # no exception here
38+
2239
def test_return_type_doesnt_depend_on_monotonicity(self):
2340
# GH#24892 we get Series back regardless of whether our DTI is monotonic
2441
dti = date_range(start="2015-5-13 23:59:00", freq="min", periods=3)

pandas/tests/indexes/interval/test_base.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import numpy as np
22
import pytest
33

4-
from pandas import (
5-
IntervalIndex,
6-
Series,
7-
)
4+
from pandas import IntervalIndex
85
import pandas._testing as tm
96
from pandas.tests.indexes.common import Base
107

@@ -46,8 +43,9 @@ def test_take(self, closed):
4643
expected = IntervalIndex.from_arrays([0, 0, 1], [1, 1, 2], closed=closed)
4744
tm.assert_index_equal(result, expected)
4845

49-
@pytest.mark.parametrize("klass", [list, tuple, np.array, Series])
50-
def test_where(self, simple_index, klass):
46+
def test_where(self, simple_index, listlike_box_with_tuple):
47+
klass = listlike_box_with_tuple
48+
5149
idx = simple_index
5250
cond = [True] * len(idx)
5351
expected = idx

pandas/tests/indexes/interval/test_interval.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -934,15 +934,14 @@ def test_dir():
934934
assert "str" not in result
935935

936936

937-
@pytest.mark.parametrize("klass", [list, np.array, pd.array, pd.Series])
938-
def test_searchsorted_different_argument_classes(klass):
937+
def test_searchsorted_different_argument_classes(listlike_box):
939938
# https://github.com/pandas-dev/pandas/issues/32762
940939
values = IntervalIndex([Interval(0, 1), Interval(1, 2)])
941-
result = values.searchsorted(klass(values))
940+
result = values.searchsorted(listlike_box(values))
942941
expected = np.array([0, 1], dtype=result.dtype)
943942
tm.assert_numpy_array_equal(result, expected)
944943

945-
result = values._data.searchsorted(klass(values))
944+
result = values._data.searchsorted(listlike_box(values))
946945
tm.assert_numpy_array_equal(result, expected)
947946

948947

pandas/tests/indexes/multi/test_indexing.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -720,13 +720,12 @@ def test_where(self):
720720
with pytest.raises(NotImplementedError, match=msg):
721721
i.where(True)
722722

723-
@pytest.mark.parametrize("klass", [list, tuple, np.array, pd.Series])
724-
def test_where_array_like(self, klass):
725-
i = MultiIndex.from_tuples([("A", 1), ("A", 2)])
723+
def test_where_array_like(self, listlike_box_with_tuple):
724+
mi = MultiIndex.from_tuples([("A", 1), ("A", 2)])
726725
cond = [False, True]
727726
msg = r"\.where is not supported for MultiIndex operations"
728727
with pytest.raises(NotImplementedError, match=msg):
729-
i.where(klass(cond))
728+
mi.where(listlike_box_with_tuple(cond))
730729

731730

732731
class TestContains:

pandas/tests/indexes/numeric/test_indexing.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -397,15 +397,14 @@ class TestWhere:
397397
UInt64Index(np.arange(5, dtype="uint64")),
398398
],
399399
)
400-
@pytest.mark.parametrize("klass", [list, tuple, np.array, Series])
401-
def test_where(self, klass, index):
400+
def test_where(self, listlike_box_with_tuple, index):
402401
cond = [True] * len(index)
403402
expected = index
404-
result = index.where(klass(cond))
403+
result = index.where(listlike_box_with_tuple(cond))
405404

406405
cond = [False] + [True] * (len(index) - 1)
407406
expected = Float64Index([index._na_value] + index[1:].tolist())
408-
result = index.where(klass(cond))
407+
result = index.where(listlike_box_with_tuple(cond))
409408
tm.assert_index_equal(result, expected)
410409

411410
def test_where_uint64(self):

pandas/tests/indexes/period/test_indexing.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -602,17 +602,16 @@ def test_get_indexer2(self):
602602

603603

604604
class TestWhere:
605-
@pytest.mark.parametrize("klass", [list, tuple, np.array, Series])
606-
def test_where(self, klass):
605+
def test_where(self, listlike_box_with_tuple):
607606
i = period_range("20130101", periods=5, freq="D")
608607
cond = [True] * len(i)
609608
expected = i
610-
result = i.where(klass(cond))
609+
result = i.where(listlike_box_with_tuple(cond))
611610
tm.assert_index_equal(result, expected)
612611

613612
cond = [False] + [True] * (len(i) - 1)
614613
expected = PeriodIndex([NaT] + i[1:].tolist(), freq="D")
615-
result = i.where(klass(cond))
614+
result = i.where(listlike_box_with_tuple(cond))
616615
tm.assert_index_equal(result, expected)
617616

618617
def test_where_other(self):

pandas/tests/indexes/period/test_searchsorted.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
NaT,
88
Period,
99
PeriodIndex,
10-
Series,
11-
array,
1210
)
1311
import pandas._testing as tm
1412

@@ -37,17 +35,16 @@ def test_searchsorted(self, freq):
3735
with pytest.raises(IncompatibleFrequency, match=msg):
3836
pidx.searchsorted(Period("2014-01-01", freq="5D"))
3937

40-
@pytest.mark.parametrize("klass", [list, np.array, array, Series])
41-
def test_searchsorted_different_argument_classes(self, klass):
38+
def test_searchsorted_different_argument_classes(self, listlike_box):
4239
pidx = PeriodIndex(
4340
["2014-01-01", "2014-01-02", "2014-01-03", "2014-01-04", "2014-01-05"],
4441
freq="D",
4542
)
46-
result = pidx.searchsorted(klass(pidx))
43+
result = pidx.searchsorted(listlike_box(pidx))
4744
expected = np.arange(len(pidx), dtype=result.dtype)
4845
tm.assert_numpy_array_equal(result, expected)
4946

50-
result = pidx._data.searchsorted(klass(pidx))
47+
result = pidx._data.searchsorted(listlike_box(pidx))
5148
tm.assert_numpy_array_equal(result, expected)
5249

5350
def test_searchsorted_invalid(self):

pandas/tests/indexes/timedeltas/test_searchsorted.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,20 @@
22
import pytest
33

44
from pandas import (
5-
Series,
65
TimedeltaIndex,
76
Timestamp,
8-
array,
97
)
108
import pandas._testing as tm
119

1210

1311
class TestSearchSorted:
14-
@pytest.mark.parametrize("klass", [list, np.array, array, Series])
15-
def test_searchsorted_different_argument_classes(self, klass):
12+
def test_searchsorted_different_argument_classes(self, listlike_box):
1613
idx = TimedeltaIndex(["1 day", "2 days", "3 days"])
17-
result = idx.searchsorted(klass(idx))
14+
result = idx.searchsorted(listlike_box(idx))
1815
expected = np.arange(len(idx), dtype=result.dtype)
1916
tm.assert_numpy_array_equal(result, expected)
2017

21-
result = idx._data.searchsorted(klass(idx))
18+
result = idx._data.searchsorted(listlike_box(idx))
2219
tm.assert_numpy_array_equal(result, expected)
2320

2421
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)