Skip to content

TST/REF: share Series setitem tests #39831

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 4 commits into from
Feb 16, 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
50 changes: 0 additions & 50 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,56 +542,6 @@ def test_setitem_td64_non_nano():
tm.assert_series_equal(ser, expected)


@pytest.mark.parametrize(
"nat_val",
[
pd.NaT,
np.timedelta64("NaT", "ns"),
np.datetime64("NaT", "ns"),
],
)
@pytest.mark.parametrize("tz", [None, "UTC"])
def test_dt64_series_assign_nat(nat_val, tz, indexer_sli):
# some nat-like values should be cast to datetime64 when inserting
# into a datetime64 series. Others should coerce to object
# and retain their dtypes.
dti = pd.date_range("2016-01-01", periods=3, tz=tz)
base = Series(dti)
expected = Series([pd.NaT] + list(dti[1:]), dtype=dti.dtype)

should_cast = nat_val is pd.NaT or base.dtype == nat_val.dtype
if not should_cast:
expected = expected.astype(object)

ser = base.copy(deep=True)
indexer_sli(ser)[0] = nat_val
tm.assert_series_equal(ser, expected)


@pytest.mark.parametrize(
"nat_val",
[
pd.NaT,
np.timedelta64("NaT", "ns"),
np.datetime64("NaT", "ns"),
],
)
def test_td64_series_assign_nat(nat_val, indexer_sli):
# some nat-like values should be cast to timedelta64 when inserting
# into a timedelta64 series. Others should coerce to object
# and retain their dtypes.
base = Series([0, 1, 2], dtype="m8[ns]")
expected = Series([pd.NaT, 1, 2], dtype="m8[ns]")

should_cast = nat_val is pd.NaT or base.dtype == nat_val.dtype
if not should_cast:
expected = expected.astype(object)

ser = base.copy(deep=True)
indexer_sli(ser)[0] = nat_val
tm.assert_series_equal(ser, expected)


def test_underlying_data_conversion():
# GH 4080
df = DataFrame({c: [1, 2, 3] for c in ["a", "b", "c"]})
Expand Down
74 changes: 23 additions & 51 deletions pandas/tests/series/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,6 @@ def test_setitem_boolean_python_list(self, func):
expected = Series(["a", "b", "c"])
tm.assert_series_equal(ser, expected)

@pytest.mark.parametrize("value", [None, NaT, np.nan])
def test_setitem_boolean_td64_values_cast_na(self, value):
# GH#18586
series = Series([0, 1, 2], dtype="timedelta64[ns]")
mask = series == series[0]
series[mask] = value
expected = Series([NaT, 1, 2], dtype="timedelta64[ns]")
tm.assert_series_equal(series, expected)

def test_setitem_boolean_nullable_int_types(self, any_nullable_numeric_dtype):
# GH: 26468
ser = Series([5, 6, 7, 8], dtype=any_nullable_numeric_dtype)
Expand Down Expand Up @@ -640,62 +631,43 @@ def is_inplace(self):
return True


class TestSetitemNATimedelta64Dtype(SetitemCastingEquivalents):
# some nat-like values should be cast to timedelta64 when inserting
# into a timedelta64 series. Others should coerce to object
# and retain their dtypes.

@pytest.fixture
def obj(self):
return Series([0, 1, 2], dtype="m8[ns]")
class TestSetitemNADatetimeLikeDtype(SetitemCastingEquivalents):
# some nat-like values should be cast to datetime64/timedelta64 when
# inserting into a datetime64/timedelta64 series. Others should coerce
# to object and retain their dtypes.
# GH#18586 for td64 and boolean mask case

@pytest.fixture(
params=[NaT, np.timedelta64("NaT", "ns"), np.datetime64("NaT", "ns")]
params=["m8[ns]", "M8[ns]", "datetime64[ns, UTC]", "datetime64[ns, US/Central]"]
)
def val(self, request):
def dtype(self, request):
return request.param

@pytest.fixture
def is_inplace(self, val):
# cast to object iff val is datetime64("NaT")
return val is NaT or val.dtype.kind == "m"

@pytest.fixture
def expected(self, obj, val, is_inplace):
dtype = obj.dtype if is_inplace else object
expected = Series([val] + list(obj[1:]), dtype=dtype)
return expected

@pytest.fixture
def key(self):
return 0


class TestSetitemNADatetime64Dtype(SetitemCastingEquivalents):
# some nat-like values should be cast to datetime64 when inserting
# into a datetime64 series. Others should coerce to object
# and retain their dtypes.

@pytest.fixture(params=[None, "UTC", "US/Central"])
def obj(self, request):
tz = request.param
dti = date_range("2016-01-01", periods=3, tz=tz)
return Series(dti)
def obj(self, dtype):
i8vals = date_range("2016-01-01", periods=3).asi8
idx = Index(i8vals, dtype=dtype)
assert idx.dtype == dtype
return Series(idx)

@pytest.fixture(
params=[NaT, np.timedelta64("NaT", "ns"), np.datetime64("NaT", "ns")]
params=[
None,
np.nan,
NaT,
np.timedelta64("NaT", "ns"),
np.datetime64("NaT", "ns"),
]
)
def val(self, request):
return request.param

@pytest.fixture
def is_inplace(self, val, obj):
if obj._values.tz is None:
# cast to object iff val is timedelta64("NaT")
return val is NaT or val.dtype.kind == "M"

# otherwise we have to exclude tznaive dt64("NaT")
return val is NaT
# td64 -> cast to object iff val is datetime64("NaT")
# dt64 -> cast to object iff val is timedelta64("NaT")
# dt64tz -> cast to object with anything _but_ NaT
return val is NaT or val is None or val is np.nan or obj.dtype == val.dtype

@pytest.fixture
def expected(self, obj, val, is_inplace):
Expand Down