Skip to content

REF: collect Index constructor tests #33354

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
Apr 7, 2020
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
52 changes: 0 additions & 52 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,6 @@ def test_constructor_from_series_dtlike(self, index, has_tz):
if has_tz:
assert result.tz == index.tz

@pytest.mark.parametrize("klass", [Index, DatetimeIndex])
def test_constructor_from_series(self, klass):
expected = DatetimeIndex(
[Timestamp("20110101"), Timestamp("20120101"), Timestamp("20130101")]
)
s = Series(
[Timestamp("20110101"), Timestamp("20120101"), Timestamp("20130101")]
)
result = klass(s)
tm.assert_index_equal(result, expected)

def test_constructor_from_series_freq(self):
# GH 6273
# create from a series, passing a freq
Expand Down Expand Up @@ -255,47 +244,6 @@ def test_index_ctor_infer_nan_nat(self, klass, dtype, na_val):
result = Index(np.array(na_list))
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize("pos", [0, 1])
@pytest.mark.parametrize(
"klass,dtype,ctor",
[
(pd.DatetimeIndex, "datetime64[ns]", np.datetime64("nat")),
(pd.TimedeltaIndex, "timedelta64[ns]", np.timedelta64("nat")),
],
)
def test_index_ctor_infer_nat_dt_like(self, pos, klass, dtype, ctor, nulls_fixture):
expected = klass([pd.NaT, pd.NaT])
assert expected.dtype == dtype
data = [ctor]
data.insert(pos, nulls_fixture)

if nulls_fixture is pd.NA:
expected = Index([pd.NA, pd.NaT])
pytest.xfail("Broken with np.NaT ctor; see GH 31884")

result = Index(data)
tm.assert_index_equal(result, expected)

result = Index(np.array(data, dtype=object))
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize("swap_objs", [True, False])
def test_index_ctor_nat_result(self, swap_objs):
# mixed np.datetime64/timedelta64 nat results in object
data = [np.datetime64("nat"), np.timedelta64("nat")]
if swap_objs:
data = data[::-1]

expected = pd.Index(data, dtype=object)
tm.assert_index_equal(Index(data), expected)
tm.assert_index_equal(Index(np.array(data, dtype=object)), expected)

def test_index_ctor_infer_periodindex(self):
xp = period_range("2012-1-1", freq="M", periods=3)
rs = Index(xp)
tm.assert_index_equal(rs, xp)
assert isinstance(rs, PeriodIndex)

@pytest.mark.parametrize(
"vals,dtype",
[
Expand Down
71 changes: 70 additions & 1 deletion pandas/tests/indexes/test_index_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,21 @@

from pandas.core.dtypes.common import is_unsigned_integer_dtype

from pandas import CategoricalIndex, Index, Int64Index, MultiIndex, UInt64Index
from pandas import (
NA,
CategoricalIndex,
DatetimeIndex,
Index,
Int64Index,
MultiIndex,
NaT,
PeriodIndex,
Series,
TimedeltaIndex,
Timestamp,
UInt64Index,
period_range,
)
import pandas._testing as tm


Expand Down Expand Up @@ -53,3 +67,58 @@ def test_constructor_categorical_to_object(self):
ci = CategoricalIndex(range(5))
result = Index(ci, dtype=object)
assert not isinstance(result, CategoricalIndex)

def test_constructor_infer_periodindex(self):
xp = period_range("2012-1-1", freq="M", periods=3)
rs = Index(xp)
tm.assert_index_equal(rs, xp)
assert isinstance(rs, PeriodIndex)

@pytest.mark.parametrize("pos", [0, 1])
@pytest.mark.parametrize(
"klass,dtype,ctor",
[
(DatetimeIndex, "datetime64[ns]", np.datetime64("nat")),
(TimedeltaIndex, "timedelta64[ns]", np.timedelta64("nat")),
],
)
def test_constructor_infer_nat_dt_like(
self, pos, klass, dtype, ctor, nulls_fixture
):
expected = klass([NaT, NaT])
assert expected.dtype == dtype
data = [ctor]
data.insert(pos, nulls_fixture)

if nulls_fixture is NA:
expected = Index([NA, NaT])
pytest.xfail("Broken with np.NaT ctor; see GH 31884")

result = Index(data)
tm.assert_index_equal(result, expected)

result = Index(np.array(data, dtype=object))
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize("swap_objs", [True, False])
def test_constructor_mixed_nat_objs_infers_object(self, swap_objs):
# mixed np.datetime64/timedelta64 nat results in object
data = [np.datetime64("nat"), np.timedelta64("nat")]
if swap_objs:
data = data[::-1]

expected = Index(data, dtype=object)
tm.assert_index_equal(Index(data), expected)
tm.assert_index_equal(Index(np.array(data, dtype=object)), expected)


class TestIndexConstructorUnwrapping:
# Test passing different arraylike values to pd.Index

@pytest.mark.parametrize("klass", [Index, DatetimeIndex])
def test_constructor_from_series_dt64(self, klass):
stamps = [Timestamp("20110101"), Timestamp("20120101"), Timestamp("20130101")]
expected = DatetimeIndex(stamps)
ser = Series(stamps)
result = klass(ser)
tm.assert_index_equal(result, expected)