Skip to content

Commit bf6be7c

Browse files
authored
TST/REF: collect Index tests by method (#33870)
1 parent df6f84a commit bf6be7c

12 files changed

+229
-200
lines changed

pandas/tests/indexes/datetimes/test_datetime.py

+1-71
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,13 @@
55
import pytest
66

77
import pandas as pd
8-
from pandas import DataFrame, DatetimeIndex, Index, NaT, Timestamp, date_range, offsets
8+
from pandas import DataFrame, DatetimeIndex, Index, Timestamp, date_range, offsets
99
import pandas._testing as tm
1010

1111
randn = np.random.randn
1212

1313

1414
class TestDatetimeIndex:
15-
def test_roundtrip_pickle_with_tz(self):
16-
17-
# GH 8367
18-
# round-trip of timezone
19-
index = date_range("20130101", periods=3, tz="US/Eastern", name="foo")
20-
unpickled = tm.round_trip_pickle(index)
21-
tm.assert_index_equal(index, unpickled)
22-
23-
def test_pickle(self):
24-
25-
# GH#4606
26-
p = tm.round_trip_pickle(NaT)
27-
assert p is NaT
28-
29-
idx = pd.to_datetime(["2013-01-01", NaT, "2014-01-06"])
30-
idx_p = tm.round_trip_pickle(idx)
31-
assert idx_p[0] == idx[0]
32-
assert idx_p[1] is NaT
33-
assert idx_p[2] == idx[2]
34-
35-
# GH#11002
36-
# don't infer freq
37-
idx = date_range("1750-1-1", "2050-1-1", freq="7D")
38-
idx_p = tm.round_trip_pickle(idx)
39-
tm.assert_index_equal(idx, idx_p)
40-
41-
def test_pickle_after_set_freq(self):
42-
dti = date_range("20130101", periods=3, tz="US/Eastern", name="foo")
43-
dti = dti._with_freq(None)
44-
45-
res = tm.round_trip_pickle(dti)
46-
tm.assert_index_equal(res, dti)
47-
4815
def test_reindex_preserves_tz_if_target_is_empty_list_or_array(self):
4916
# GH7774
5017
index = date_range("20130101", periods=3, tz="US/Eastern")
@@ -164,23 +131,6 @@ def test_append_nondatetimeindex(self):
164131
result = rng.append(idx)
165132
assert isinstance(result[0], Timestamp)
166133

167-
def test_map(self):
168-
rng = date_range("1/1/2000", periods=10)
169-
170-
f = lambda x: x.strftime("%Y%m%d")
171-
result = rng.map(f)
172-
exp = Index([f(x) for x in rng], dtype="<U8")
173-
tm.assert_index_equal(result, exp)
174-
175-
def test_map_fallthrough(self, capsys):
176-
# GH#22067, check we don't get warnings about silently ignored errors
177-
dti = date_range("2017-01-01", "2018-01-01", freq="B")
178-
179-
dti.map(lambda x: pd.Period(year=x.year, month=x.month, freq="M"))
180-
181-
captured = capsys.readouterr()
182-
assert captured.err == ""
183-
184134
def test_iteration_preserves_tz(self):
185135
# see gh-8890
186136
index = date_range("2012-01-01", periods=3, freq="H", tz="US/Eastern")
@@ -264,14 +214,6 @@ def test_sort_values(self):
264214
assert ordered[::-1].is_monotonic
265215
tm.assert_numpy_array_equal(dexer, np.array([0, 2, 1], dtype=np.intp))
266216

267-
def test_map_bug_1677(self):
268-
index = DatetimeIndex(["2012-04-25 09:30:00.393000"])
269-
f = index.asof
270-
271-
result = index.map(f)
272-
expected = Index([f(index[0])])
273-
tm.assert_index_equal(result, expected)
274-
275217
def test_groupby_function_tuple_1677(self):
276218
df = DataFrame(np.random.rand(100), index=date_range("1/1/2000", periods=100))
277219
monthly_group = df.groupby(lambda x: (x.year, x.month))
@@ -454,18 +396,6 @@ def test_to_frame_datetime_tz(self):
454396
expected = DataFrame(idx, index=idx)
455397
tm.assert_frame_equal(result, expected)
456398

457-
@pytest.mark.parametrize("name", [None, "name"])
458-
def test_index_map(self, name):
459-
# see GH20990
460-
count = 6
461-
index = pd.date_range("2018-01-01", periods=count, freq="M", name=name).map(
462-
lambda x: (x.year, x.month)
463-
)
464-
exp_index = pd.MultiIndex.from_product(
465-
((2018,), range(1, 7)), names=[name, name]
466-
)
467-
tm.assert_index_equal(index, exp_index)
468-
469399
def test_split_non_utc(self):
470400
# GH 14042
471401
indices = pd.date_range("2016-01-01 00:00:00+0200", freq="S", periods=10)

pandas/tests/indexes/datetimes/test_indexing.py

+15
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,21 @@ def test_dti_contains_with_duplicates(self):
518518
ix = DatetimeIndex([d, d])
519519
assert d in ix
520520

521+
@pytest.mark.parametrize(
522+
"vals",
523+
[
524+
[0, 1, 0],
525+
[0, 0, -1],
526+
[0, -1, -1],
527+
["2015", "2015", "2016"],
528+
["2015", "2015", "2014"],
529+
],
530+
)
531+
def test_contains_nonunique(self, vals):
532+
# GH#9512
533+
idx = DatetimeIndex(vals)
534+
assert idx[0] in idx
535+
521536

522537
class TestGetIndexer:
523538
def test_get_indexer(self):
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import pytest
2+
3+
from pandas import DatetimeIndex, Index, MultiIndex, Period, date_range
4+
import pandas._testing as tm
5+
6+
7+
class TestMap:
8+
def test_map(self):
9+
rng = date_range("1/1/2000", periods=10)
10+
11+
f = lambda x: x.strftime("%Y%m%d")
12+
result = rng.map(f)
13+
exp = Index([f(x) for x in rng], dtype="<U8")
14+
tm.assert_index_equal(result, exp)
15+
16+
def test_map_fallthrough(self, capsys):
17+
# GH#22067, check we don't get warnings about silently ignored errors
18+
dti = date_range("2017-01-01", "2018-01-01", freq="B")
19+
20+
dti.map(lambda x: Period(year=x.year, month=x.month, freq="M"))
21+
22+
captured = capsys.readouterr()
23+
assert captured.err == ""
24+
25+
def test_map_bug_1677(self):
26+
index = DatetimeIndex(["2012-04-25 09:30:00.393000"])
27+
f = index.asof
28+
29+
result = index.map(f)
30+
expected = Index([f(index[0])])
31+
tm.assert_index_equal(result, expected)
32+
33+
@pytest.mark.parametrize("name", [None, "name"])
34+
def test_index_map(self, name):
35+
# see GH#20990
36+
count = 6
37+
index = date_range("2018-01-01", periods=count, freq="M", name=name).map(
38+
lambda x: (x.year, x.month)
39+
)
40+
exp_index = MultiIndex.from_product(((2018,), range(1, 7)), names=[name, name])
41+
tm.assert_index_equal(index, exp_index)

pandas/tests/indexes/datetimes/test_ops.py

-22
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,6 @@ def test_value_counts_unique(self, tz_naive_fixture):
170170

171171
tm.assert_index_equal(idx.unique(), exp_idx)
172172

173-
def test_nonunique_contains(self):
174-
# GH 9512
175-
for idx in map(
176-
DatetimeIndex,
177-
(
178-
[0, 1, 0],
179-
[0, 0, -1],
180-
[0, -1, -1],
181-
["2015", "2015", "2016"],
182-
["2015", "2015", "2014"],
183-
),
184-
):
185-
assert idx[0] in idx
186-
187173
@pytest.mark.parametrize(
188174
"idx",
189175
[
@@ -432,10 +418,6 @@ def test_comparison(self):
432418
assert comp[11]
433419
assert not comp[9]
434420

435-
def test_pickle_unpickle(self):
436-
unpickled = tm.round_trip_pickle(self.rng)
437-
assert unpickled.freq is not None
438-
439421
def test_copy(self):
440422
cp = self.rng.copy()
441423
repr(cp)
@@ -478,9 +460,5 @@ def test_copy(self):
478460
repr(cp)
479461
tm.assert_index_equal(cp, self.rng)
480462

481-
def test_pickle_unpickle(self):
482-
unpickled = tm.round_trip_pickle(self.rng)
483-
assert unpickled.freq is not None
484-
485463
def test_equals(self):
486464
assert not self.rng.equals(list(self.rng))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import pytest
2+
3+
from pandas import NaT, date_range, to_datetime
4+
import pandas._testing as tm
5+
6+
7+
class TestPickle:
8+
def test_pickle(self):
9+
# GH#4606
10+
idx = to_datetime(["2013-01-01", NaT, "2014-01-06"])
11+
idx_p = tm.round_trip_pickle(idx)
12+
assert idx_p[0] == idx[0]
13+
assert idx_p[1] is NaT
14+
assert idx_p[2] == idx[2]
15+
16+
def test_pickle_dont_infer_freq(self):
17+
# GH##11002
18+
# don't infer freq
19+
idx = date_range("1750-1-1", "2050-1-1", freq="7D")
20+
idx_p = tm.round_trip_pickle(idx)
21+
tm.assert_index_equal(idx, idx_p)
22+
23+
def test_pickle_after_set_freq(self):
24+
dti = date_range("20130101", periods=3, tz="US/Eastern", name="foo")
25+
dti = dti._with_freq(None)
26+
27+
res = tm.round_trip_pickle(dti)
28+
tm.assert_index_equal(res, dti)
29+
30+
def test_roundtrip_pickle_with_tz(self):
31+
# GH#8367
32+
# round-trip of timezone
33+
index = date_range("20130101", periods=3, tz="US/Eastern", name="foo")
34+
unpickled = tm.round_trip_pickle(index)
35+
tm.assert_index_equal(index, unpickled)
36+
37+
@pytest.mark.parametrize("freq", ["B", "C"])
38+
def test_pickle_unpickle(self, freq):
39+
rng = date_range("2009-01-01", "2010-01-01", freq=freq)
40+
unpickled = tm.round_trip_pickle(rng)
41+
assert unpickled.freq == freq
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import numpy as np
2+
3+
from pandas import PeriodIndex
4+
import pandas._testing as tm
5+
6+
7+
class TestFactorize:
8+
def test_factorize(self):
9+
idx1 = PeriodIndex(
10+
["2014-01", "2014-01", "2014-02", "2014-02", "2014-03", "2014-03"], freq="M"
11+
)
12+
13+
exp_arr = np.array([0, 0, 1, 1, 2, 2], dtype=np.intp)
14+
exp_idx = PeriodIndex(["2014-01", "2014-02", "2014-03"], freq="M")
15+
16+
arr, idx = idx1.factorize()
17+
tm.assert_numpy_array_equal(arr, exp_arr)
18+
tm.assert_index_equal(idx, exp_idx)
19+
20+
arr, idx = idx1.factorize(sort=True)
21+
tm.assert_numpy_array_equal(arr, exp_arr)
22+
tm.assert_index_equal(idx, exp_idx)
23+
24+
idx2 = PeriodIndex(
25+
["2014-03", "2014-03", "2014-02", "2014-01", "2014-03", "2014-01"], freq="M"
26+
)
27+
28+
exp_arr = np.array([2, 2, 1, 0, 2, 0], dtype=np.intp)
29+
arr, idx = idx2.factorize(sort=True)
30+
tm.assert_numpy_array_equal(arr, exp_arr)
31+
tm.assert_index_equal(idx, exp_idx)
32+
33+
exp_arr = np.array([0, 0, 1, 2, 0, 2], dtype=np.intp)
34+
exp_idx = PeriodIndex(["2014-03", "2014-02", "2014-01"], freq="M")
35+
arr, idx = idx2.factorize()
36+
tm.assert_numpy_array_equal(arr, exp_arr)
37+
tm.assert_index_equal(idx, exp_idx)

pandas/tests/indexes/period/test_indexing.py

+21
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,27 @@ def test_contains(self):
764764

765765
assert p3 not in idx0
766766

767+
def test_contains_freq_mismatch(self):
768+
rng = period_range("2007-01", freq="M", periods=10)
769+
770+
assert Period("2007-01", freq="M") in rng
771+
assert not Period("2007-01", freq="D") in rng
772+
assert not Period("2007-01", freq="2M") in rng
773+
774+
def test_contains_nat(self):
775+
# see gh-13582
776+
idx = period_range("2007-01", freq="M", periods=10)
777+
assert NaT not in idx
778+
assert None not in idx
779+
assert float("nan") not in idx
780+
assert np.nan not in idx
781+
782+
idx = PeriodIndex(["2011-01", "NaT", "2011-02"], freq="M")
783+
assert NaT in idx
784+
assert None in idx
785+
assert float("nan") in idx
786+
assert np.nan in idx
787+
767788

768789
class TestAsOfLocs:
769790
def test_asof_locs_mismatched_type(self):

pandas/tests/indexes/period/test_period.py

-52
Original file line numberDiff line numberDiff line change
@@ -318,37 +318,6 @@ def test_period_reindex_with_object(
318318
expected = pd.Series(expected_values, index=object_index)
319319
tm.assert_series_equal(result, expected)
320320

321-
def test_factorize(self):
322-
idx1 = PeriodIndex(
323-
["2014-01", "2014-01", "2014-02", "2014-02", "2014-03", "2014-03"], freq="M"
324-
)
325-
326-
exp_arr = np.array([0, 0, 1, 1, 2, 2], dtype=np.intp)
327-
exp_idx = PeriodIndex(["2014-01", "2014-02", "2014-03"], freq="M")
328-
329-
arr, idx = idx1.factorize()
330-
tm.assert_numpy_array_equal(arr, exp_arr)
331-
tm.assert_index_equal(idx, exp_idx)
332-
333-
arr, idx = idx1.factorize(sort=True)
334-
tm.assert_numpy_array_equal(arr, exp_arr)
335-
tm.assert_index_equal(idx, exp_idx)
336-
337-
idx2 = PeriodIndex(
338-
["2014-03", "2014-03", "2014-02", "2014-01", "2014-03", "2014-01"], freq="M"
339-
)
340-
341-
exp_arr = np.array([2, 2, 1, 0, 2, 0], dtype=np.intp)
342-
arr, idx = idx2.factorize(sort=True)
343-
tm.assert_numpy_array_equal(arr, exp_arr)
344-
tm.assert_index_equal(idx, exp_idx)
345-
346-
exp_arr = np.array([0, 0, 1, 2, 0, 2], dtype=np.intp)
347-
exp_idx = PeriodIndex(["2014-03", "2014-02", "2014-01"], freq="M")
348-
arr, idx = idx2.factorize()
349-
tm.assert_numpy_array_equal(arr, exp_arr)
350-
tm.assert_index_equal(idx, exp_idx)
351-
352321
def test_is_(self):
353322
create_index = lambda: period_range(freq="A", start="1/1/2001", end="12/1/2009")
354323
index = create_index()
@@ -367,27 +336,6 @@ def test_is_(self):
367336
assert not index.is_(index - 2)
368337
assert not index.is_(index - 0)
369338

370-
def test_contains(self):
371-
rng = period_range("2007-01", freq="M", periods=10)
372-
373-
assert Period("2007-01", freq="M") in rng
374-
assert not Period("2007-01", freq="D") in rng
375-
assert not Period("2007-01", freq="2M") in rng
376-
377-
def test_contains_nat(self):
378-
# see gh-13582
379-
idx = period_range("2007-01", freq="M", periods=10)
380-
assert NaT not in idx
381-
assert None not in idx
382-
assert float("nan") not in idx
383-
assert np.nan not in idx
384-
385-
idx = PeriodIndex(["2011-01", "NaT", "2011-02"], freq="M")
386-
assert NaT in idx
387-
assert None in idx
388-
assert float("nan") in idx
389-
assert np.nan in idx
390-
391339
def test_periods_number_check(self):
392340
msg = (
393341
"Of the three parameters: start, end, and periods, exactly two "

0 commit comments

Comments
 (0)