Skip to content

TST/REF: fixturize #43918

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 2 commits into from
Oct 8, 2021
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
5 changes: 3 additions & 2 deletions pandas/tests/indexes/categorical/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/indexes/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import numpy as np
import pytest

from pandas import (
Series,
array,
)


@pytest.fixture(params=[None, False])
def sort(request):
Expand All @@ -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
40 changes: 0 additions & 40 deletions pandas/tests/indexes/datetimes/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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))
Expand Down
26 changes: 25 additions & 1 deletion pandas/tests/indexes/datetimes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reason you are importing here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bc that's how it is in master (best guess is that is due to a code check for private imports but i havent checked that)


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
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/indexes/datetimes/test_partial_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 4 additions & 6 deletions pandas/tests/indexes/interval/test_base.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions pandas/tests/indexes/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
7 changes: 3 additions & 4 deletions pandas/tests/indexes/multi/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 3 additions & 4 deletions pandas/tests/indexes/numeric/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 3 additions & 4 deletions pandas/tests/indexes/period/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 3 additions & 6 deletions pandas/tests/indexes/period/test_searchsorted.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
NaT,
Period,
PeriodIndex,
Series,
array,
)
import pandas._testing as tm

Expand Down Expand Up @@ -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):
Expand Down
9 changes: 3 additions & 6 deletions pandas/tests/indexes/timedeltas/test_searchsorted.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down