Skip to content

Commit d585a6c

Browse files
authored
TST/REF: misplaced Series.reindex test (#38570)
1 parent 904331f commit d585a6c

File tree

5 files changed

+104
-89
lines changed

5 files changed

+104
-89
lines changed

pandas/tests/frame/methods/test_set_index.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,3 +684,14 @@ def __str__(self) -> str:
684684
with pytest.raises(TypeError, match=msg):
685685
# custom label wrapped in list
686686
df.set_index([thing2])
687+
688+
def test_set_index_periodindex(self):
689+
# GH#6631
690+
df = DataFrame(np.random.random(6))
691+
idx1 = period_range("2011/01/01", periods=6, freq="M")
692+
idx2 = period_range("2013", periods=6, freq="A")
693+
694+
df = df.set_index(idx1)
695+
tm.assert_index_equal(df.index, idx1)
696+
df = df.set_index(idx2)
697+
tm.assert_index_equal(df.index, idx2)

pandas/tests/indexes/datetimes/test_datetime.py

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import date, timedelta
1+
from datetime import date
22

33
import dateutil
44
import numpy as np
@@ -12,51 +12,6 @@
1212

1313

1414
class TestDatetimeIndex:
15-
def test_reindex_preserves_tz_if_target_is_empty_list_or_array(self):
16-
# GH7774
17-
index = date_range("20130101", periods=3, tz="US/Eastern")
18-
assert str(index.reindex([])[0].tz) == "US/Eastern"
19-
assert str(index.reindex(np.array([]))[0].tz) == "US/Eastern"
20-
21-
def test_reindex_with_same_tz(self):
22-
# GH 32740
23-
rng_a = date_range("2010-01-01", "2010-01-02", periods=24, tz="utc")
24-
rng_b = date_range("2010-01-01", "2010-01-02", periods=23, tz="utc")
25-
result1, result2 = rng_a.reindex(
26-
rng_b, method="nearest", tolerance=timedelta(seconds=20)
27-
)
28-
expected_list1 = [
29-
"2010-01-01 00:00:00",
30-
"2010-01-01 01:05:27.272727272",
31-
"2010-01-01 02:10:54.545454545",
32-
"2010-01-01 03:16:21.818181818",
33-
"2010-01-01 04:21:49.090909090",
34-
"2010-01-01 05:27:16.363636363",
35-
"2010-01-01 06:32:43.636363636",
36-
"2010-01-01 07:38:10.909090909",
37-
"2010-01-01 08:43:38.181818181",
38-
"2010-01-01 09:49:05.454545454",
39-
"2010-01-01 10:54:32.727272727",
40-
"2010-01-01 12:00:00",
41-
"2010-01-01 13:05:27.272727272",
42-
"2010-01-01 14:10:54.545454545",
43-
"2010-01-01 15:16:21.818181818",
44-
"2010-01-01 16:21:49.090909090",
45-
"2010-01-01 17:27:16.363636363",
46-
"2010-01-01 18:32:43.636363636",
47-
"2010-01-01 19:38:10.909090909",
48-
"2010-01-01 20:43:38.181818181",
49-
"2010-01-01 21:49:05.454545454",
50-
"2010-01-01 22:54:32.727272727",
51-
"2010-01-02 00:00:00",
52-
]
53-
expected1 = DatetimeIndex(
54-
expected_list1, dtype="datetime64[ns, UTC]", freq=None
55-
)
56-
expected2 = np.array([0] + [-1] * 21 + [23], dtype=np.dtype("intp"))
57-
tm.assert_index_equal(result1, expected1)
58-
tm.assert_numpy_array_equal(result2, expected2)
59-
6015
def test_time_loc(self): # GH8667
6116
from datetime import time
6217

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from datetime import timedelta
2+
3+
import numpy as np
4+
5+
from pandas import DatetimeIndex, date_range
6+
import pandas._testing as tm
7+
8+
9+
class TestDatetimeIndexReindex:
10+
def test_reindex_preserves_tz_if_target_is_empty_list_or_array(self):
11+
# GH#7774
12+
index = date_range("2013-01-01", periods=3, tz="US/Eastern")
13+
assert str(index.reindex([])[0].tz) == "US/Eastern"
14+
assert str(index.reindex(np.array([]))[0].tz) == "US/Eastern"
15+
16+
def test_reindex_with_same_tz_nearest(self):
17+
# GH#32740
18+
rng_a = date_range("2010-01-01", "2010-01-02", periods=24, tz="utc")
19+
rng_b = date_range("2010-01-01", "2010-01-02", periods=23, tz="utc")
20+
result1, result2 = rng_a.reindex(
21+
rng_b, method="nearest", tolerance=timedelta(seconds=20)
22+
)
23+
expected_list1 = [
24+
"2010-01-01 00:00:00",
25+
"2010-01-01 01:05:27.272727272",
26+
"2010-01-01 02:10:54.545454545",
27+
"2010-01-01 03:16:21.818181818",
28+
"2010-01-01 04:21:49.090909090",
29+
"2010-01-01 05:27:16.363636363",
30+
"2010-01-01 06:32:43.636363636",
31+
"2010-01-01 07:38:10.909090909",
32+
"2010-01-01 08:43:38.181818181",
33+
"2010-01-01 09:49:05.454545454",
34+
"2010-01-01 10:54:32.727272727",
35+
"2010-01-01 12:00:00",
36+
"2010-01-01 13:05:27.272727272",
37+
"2010-01-01 14:10:54.545454545",
38+
"2010-01-01 15:16:21.818181818",
39+
"2010-01-01 16:21:49.090909090",
40+
"2010-01-01 17:27:16.363636363",
41+
"2010-01-01 18:32:43.636363636",
42+
"2010-01-01 19:38:10.909090909",
43+
"2010-01-01 20:43:38.181818181",
44+
"2010-01-01 21:49:05.454545454",
45+
"2010-01-01 22:54:32.727272727",
46+
"2010-01-02 00:00:00",
47+
]
48+
expected1 = DatetimeIndex(
49+
expected_list1, dtype="datetime64[ns, UTC]", freq=None
50+
)
51+
expected2 = np.array([0] + [-1] * 21 + [23], dtype=np.dtype("intp"))
52+
tm.assert_index_equal(result1, expected1)
53+
tm.assert_numpy_array_equal(result2, expected2)

pandas/tests/indexes/period/test_period.py

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -274,46 +274,6 @@ def _check_all_fields(self, periodindex):
274274
for x, val in zip(periods, field_s):
275275
assert getattr(x, field) == val
276276

277-
def test_period_set_index_reindex(self):
278-
# GH 6631
279-
df = DataFrame(np.random.random(6))
280-
idx1 = period_range("2011/01/01", periods=6, freq="M")
281-
idx2 = period_range("2013", periods=6, freq="A")
282-
283-
df = df.set_index(idx1)
284-
tm.assert_index_equal(df.index, idx1)
285-
df = df.set_index(idx2)
286-
tm.assert_index_equal(df.index, idx2)
287-
288-
@pytest.mark.parametrize(
289-
"p_values, o_values, values, expected_values",
290-
[
291-
(
292-
[Period("2019Q1", "Q-DEC"), Period("2019Q2", "Q-DEC")],
293-
[Period("2019Q1", "Q-DEC"), Period("2019Q2", "Q-DEC"), "All"],
294-
[1.0, 1.0],
295-
[1.0, 1.0, np.nan],
296-
),
297-
(
298-
[Period("2019Q1", "Q-DEC"), Period("2019Q2", "Q-DEC")],
299-
[Period("2019Q1", "Q-DEC"), Period("2019Q2", "Q-DEC")],
300-
[1.0, 1.0],
301-
[1.0, 1.0],
302-
),
303-
],
304-
)
305-
def test_period_reindex_with_object(
306-
self, p_values, o_values, values, expected_values
307-
):
308-
# GH 28337
309-
period_index = PeriodIndex(p_values)
310-
object_index = Index(o_values)
311-
312-
s = Series(values, index=period_index)
313-
result = s.reindex(object_index)
314-
expected = Series(expected_values, index=object_index)
315-
tm.assert_series_equal(result, expected)
316-
317277
def test_is_(self):
318278
create_index = lambda: period_range(freq="A", start="1/1/2001", end="12/1/2009")
319279
index = create_index()

pandas/tests/series/methods/test_reindex.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import numpy as np
22
import pytest
33

4-
import pandas as pd
5-
from pandas import Categorical, Series, date_range, isna
4+
from pandas import (
5+
Categorical,
6+
Index,
7+
NaT,
8+
Period,
9+
PeriodIndex,
10+
Series,
11+
date_range,
12+
isna,
13+
)
614
import pandas._testing as tm
715

816

@@ -293,5 +301,33 @@ def test_reindex_datetimeindexes_tz_naive_and_aware():
293301
def test_reindex_empty_series_tz_dtype():
294302
# GH 20869
295303
result = Series(dtype="datetime64[ns, UTC]").reindex([0, 1])
296-
expected = Series([pd.NaT] * 2, dtype="datetime64[ns, UTC]")
304+
expected = Series([NaT] * 2, dtype="datetime64[ns, UTC]")
297305
tm.assert_equal(result, expected)
306+
307+
308+
@pytest.mark.parametrize(
309+
"p_values, o_values, values, expected_values",
310+
[
311+
(
312+
[Period("2019Q1", "Q-DEC"), Period("2019Q2", "Q-DEC")],
313+
[Period("2019Q1", "Q-DEC"), Period("2019Q2", "Q-DEC"), "All"],
314+
[1.0, 1.0],
315+
[1.0, 1.0, np.nan],
316+
),
317+
(
318+
[Period("2019Q1", "Q-DEC"), Period("2019Q2", "Q-DEC")],
319+
[Period("2019Q1", "Q-DEC"), Period("2019Q2", "Q-DEC")],
320+
[1.0, 1.0],
321+
[1.0, 1.0],
322+
),
323+
],
324+
)
325+
def test_reindex_periodindex_with_object(p_values, o_values, values, expected_values):
326+
# GH#28337
327+
period_index = PeriodIndex(p_values)
328+
object_index = Index(o_values)
329+
330+
ser = Series(values, index=period_index)
331+
result = ser.reindex(object_index)
332+
expected = Series(expected_values, index=object_index)
333+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)