Skip to content

Commit abf587c

Browse files
authored
collect Index constructor tests (pandas-dev#33354)
1 parent 70086c5 commit abf587c

File tree

2 files changed

+70
-53
lines changed

2 files changed

+70
-53
lines changed

pandas/tests/indexes/test_base.py

-52
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,6 @@ def test_constructor_from_series_dtlike(self, index, has_tz):
148148
if has_tz:
149149
assert result.tz == index.tz
150150

151-
@pytest.mark.parametrize("klass", [Index, DatetimeIndex])
152-
def test_constructor_from_series(self, klass):
153-
expected = DatetimeIndex(
154-
[Timestamp("20110101"), Timestamp("20120101"), Timestamp("20130101")]
155-
)
156-
s = Series(
157-
[Timestamp("20110101"), Timestamp("20120101"), Timestamp("20130101")]
158-
)
159-
result = klass(s)
160-
tm.assert_index_equal(result, expected)
161-
162151
def test_constructor_from_series_freq(self):
163152
# GH 6273
164153
# create from a series, passing a freq
@@ -255,47 +244,6 @@ def test_index_ctor_infer_nan_nat(self, klass, dtype, na_val):
255244
result = Index(np.array(na_list))
256245
tm.assert_index_equal(result, expected)
257246

258-
@pytest.mark.parametrize("pos", [0, 1])
259-
@pytest.mark.parametrize(
260-
"klass,dtype,ctor",
261-
[
262-
(pd.DatetimeIndex, "datetime64[ns]", np.datetime64("nat")),
263-
(pd.TimedeltaIndex, "timedelta64[ns]", np.timedelta64("nat")),
264-
],
265-
)
266-
def test_index_ctor_infer_nat_dt_like(self, pos, klass, dtype, ctor, nulls_fixture):
267-
expected = klass([pd.NaT, pd.NaT])
268-
assert expected.dtype == dtype
269-
data = [ctor]
270-
data.insert(pos, nulls_fixture)
271-
272-
if nulls_fixture is pd.NA:
273-
expected = Index([pd.NA, pd.NaT])
274-
pytest.xfail("Broken with np.NaT ctor; see GH 31884")
275-
276-
result = Index(data)
277-
tm.assert_index_equal(result, expected)
278-
279-
result = Index(np.array(data, dtype=object))
280-
tm.assert_index_equal(result, expected)
281-
282-
@pytest.mark.parametrize("swap_objs", [True, False])
283-
def test_index_ctor_nat_result(self, swap_objs):
284-
# mixed np.datetime64/timedelta64 nat results in object
285-
data = [np.datetime64("nat"), np.timedelta64("nat")]
286-
if swap_objs:
287-
data = data[::-1]
288-
289-
expected = pd.Index(data, dtype=object)
290-
tm.assert_index_equal(Index(data), expected)
291-
tm.assert_index_equal(Index(np.array(data, dtype=object)), expected)
292-
293-
def test_index_ctor_infer_periodindex(self):
294-
xp = period_range("2012-1-1", freq="M", periods=3)
295-
rs = Index(xp)
296-
tm.assert_index_equal(rs, xp)
297-
assert isinstance(rs, PeriodIndex)
298-
299247
@pytest.mark.parametrize(
300248
"vals,dtype",
301249
[

pandas/tests/indexes/test_index_new.py

+70-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,21 @@
66

77
from pandas.core.dtypes.common import is_unsigned_integer_dtype
88

9-
from pandas import CategoricalIndex, Index, Int64Index, MultiIndex, UInt64Index
9+
from pandas import (
10+
NA,
11+
CategoricalIndex,
12+
DatetimeIndex,
13+
Index,
14+
Int64Index,
15+
MultiIndex,
16+
NaT,
17+
PeriodIndex,
18+
Series,
19+
TimedeltaIndex,
20+
Timestamp,
21+
UInt64Index,
22+
period_range,
23+
)
1024
import pandas._testing as tm
1125

1226

@@ -53,3 +67,58 @@ def test_constructor_categorical_to_object(self):
5367
ci = CategoricalIndex(range(5))
5468
result = Index(ci, dtype=object)
5569
assert not isinstance(result, CategoricalIndex)
70+
71+
def test_constructor_infer_periodindex(self):
72+
xp = period_range("2012-1-1", freq="M", periods=3)
73+
rs = Index(xp)
74+
tm.assert_index_equal(rs, xp)
75+
assert isinstance(rs, PeriodIndex)
76+
77+
@pytest.mark.parametrize("pos", [0, 1])
78+
@pytest.mark.parametrize(
79+
"klass,dtype,ctor",
80+
[
81+
(DatetimeIndex, "datetime64[ns]", np.datetime64("nat")),
82+
(TimedeltaIndex, "timedelta64[ns]", np.timedelta64("nat")),
83+
],
84+
)
85+
def test_constructor_infer_nat_dt_like(
86+
self, pos, klass, dtype, ctor, nulls_fixture
87+
):
88+
expected = klass([NaT, NaT])
89+
assert expected.dtype == dtype
90+
data = [ctor]
91+
data.insert(pos, nulls_fixture)
92+
93+
if nulls_fixture is NA:
94+
expected = Index([NA, NaT])
95+
pytest.xfail("Broken with np.NaT ctor; see GH 31884")
96+
97+
result = Index(data)
98+
tm.assert_index_equal(result, expected)
99+
100+
result = Index(np.array(data, dtype=object))
101+
tm.assert_index_equal(result, expected)
102+
103+
@pytest.mark.parametrize("swap_objs", [True, False])
104+
def test_constructor_mixed_nat_objs_infers_object(self, swap_objs):
105+
# mixed np.datetime64/timedelta64 nat results in object
106+
data = [np.datetime64("nat"), np.timedelta64("nat")]
107+
if swap_objs:
108+
data = data[::-1]
109+
110+
expected = Index(data, dtype=object)
111+
tm.assert_index_equal(Index(data), expected)
112+
tm.assert_index_equal(Index(np.array(data, dtype=object)), expected)
113+
114+
115+
class TestIndexConstructorUnwrapping:
116+
# Test passing different arraylike values to pd.Index
117+
118+
@pytest.mark.parametrize("klass", [Index, DatetimeIndex])
119+
def test_constructor_from_series_dt64(self, klass):
120+
stamps = [Timestamp("20110101"), Timestamp("20120101"), Timestamp("20130101")]
121+
expected = DatetimeIndex(stamps)
122+
ser = Series(stamps)
123+
result = klass(ser)
124+
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)