Skip to content

Commit 6716589

Browse files
authored
TST/REF: share Series setitem tests (#39831)
1 parent 0c3c56b commit 6716589

File tree

2 files changed

+23
-101
lines changed

2 files changed

+23
-101
lines changed

pandas/tests/series/indexing/test_indexing.py

-50
Original file line numberDiff line numberDiff line change
@@ -542,56 +542,6 @@ def test_setitem_td64_non_nano():
542542
tm.assert_series_equal(ser, expected)
543543

544544

545-
@pytest.mark.parametrize(
546-
"nat_val",
547-
[
548-
pd.NaT,
549-
np.timedelta64("NaT", "ns"),
550-
np.datetime64("NaT", "ns"),
551-
],
552-
)
553-
@pytest.mark.parametrize("tz", [None, "UTC"])
554-
def test_dt64_series_assign_nat(nat_val, tz, indexer_sli):
555-
# some nat-like values should be cast to datetime64 when inserting
556-
# into a datetime64 series. Others should coerce to object
557-
# and retain their dtypes.
558-
dti = pd.date_range("2016-01-01", periods=3, tz=tz)
559-
base = Series(dti)
560-
expected = Series([pd.NaT] + list(dti[1:]), dtype=dti.dtype)
561-
562-
should_cast = nat_val is pd.NaT or base.dtype == nat_val.dtype
563-
if not should_cast:
564-
expected = expected.astype(object)
565-
566-
ser = base.copy(deep=True)
567-
indexer_sli(ser)[0] = nat_val
568-
tm.assert_series_equal(ser, expected)
569-
570-
571-
@pytest.mark.parametrize(
572-
"nat_val",
573-
[
574-
pd.NaT,
575-
np.timedelta64("NaT", "ns"),
576-
np.datetime64("NaT", "ns"),
577-
],
578-
)
579-
def test_td64_series_assign_nat(nat_val, indexer_sli):
580-
# some nat-like values should be cast to timedelta64 when inserting
581-
# into a timedelta64 series. Others should coerce to object
582-
# and retain their dtypes.
583-
base = Series([0, 1, 2], dtype="m8[ns]")
584-
expected = Series([pd.NaT, 1, 2], dtype="m8[ns]")
585-
586-
should_cast = nat_val is pd.NaT or base.dtype == nat_val.dtype
587-
if not should_cast:
588-
expected = expected.astype(object)
589-
590-
ser = base.copy(deep=True)
591-
indexer_sli(ser)[0] = nat_val
592-
tm.assert_series_equal(ser, expected)
593-
594-
595545
def test_underlying_data_conversion():
596546
# GH 4080
597547
df = DataFrame({c: [1, 2, 3] for c in ["a", "b", "c"]})

pandas/tests/series/indexing/test_setitem.py

+23-51
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,6 @@ def test_setitem_boolean_python_list(self, func):
170170
expected = Series(["a", "b", "c"])
171171
tm.assert_series_equal(ser, expected)
172172

173-
@pytest.mark.parametrize("value", [None, NaT, np.nan])
174-
def test_setitem_boolean_td64_values_cast_na(self, value):
175-
# GH#18586
176-
series = Series([0, 1, 2], dtype="timedelta64[ns]")
177-
mask = series == series[0]
178-
series[mask] = value
179-
expected = Series([NaT, 1, 2], dtype="timedelta64[ns]")
180-
tm.assert_series_equal(series, expected)
181-
182173
def test_setitem_boolean_nullable_int_types(self, any_nullable_numeric_dtype):
183174
# GH: 26468
184175
ser = Series([5, 6, 7, 8], dtype=any_nullable_numeric_dtype)
@@ -643,62 +634,43 @@ def is_inplace(self):
643634
return True
644635

645636

646-
class TestSetitemNATimedelta64Dtype(SetitemCastingEquivalents):
647-
# some nat-like values should be cast to timedelta64 when inserting
648-
# into a timedelta64 series. Others should coerce to object
649-
# and retain their dtypes.
650-
651-
@pytest.fixture
652-
def obj(self):
653-
return Series([0, 1, 2], dtype="m8[ns]")
637+
class TestSetitemNADatetimeLikeDtype(SetitemCastingEquivalents):
638+
# some nat-like values should be cast to datetime64/timedelta64 when
639+
# inserting into a datetime64/timedelta64 series. Others should coerce
640+
# to object and retain their dtypes.
641+
# GH#18586 for td64 and boolean mask case
654642

655643
@pytest.fixture(
656-
params=[NaT, np.timedelta64("NaT", "ns"), np.datetime64("NaT", "ns")]
644+
params=["m8[ns]", "M8[ns]", "datetime64[ns, UTC]", "datetime64[ns, US/Central]"]
657645
)
658-
def val(self, request):
646+
def dtype(self, request):
659647
return request.param
660648

661649
@pytest.fixture
662-
def is_inplace(self, val):
663-
# cast to object iff val is datetime64("NaT")
664-
return val is NaT or val.dtype.kind == "m"
665-
666-
@pytest.fixture
667-
def expected(self, obj, val, is_inplace):
668-
dtype = obj.dtype if is_inplace else object
669-
expected = Series([val] + list(obj[1:]), dtype=dtype)
670-
return expected
671-
672-
@pytest.fixture
673-
def key(self):
674-
return 0
675-
676-
677-
class TestSetitemNADatetime64Dtype(SetitemCastingEquivalents):
678-
# some nat-like values should be cast to datetime64 when inserting
679-
# into a datetime64 series. Others should coerce to object
680-
# and retain their dtypes.
681-
682-
@pytest.fixture(params=[None, "UTC", "US/Central"])
683-
def obj(self, request):
684-
tz = request.param
685-
dti = date_range("2016-01-01", periods=3, tz=tz)
686-
return Series(dti)
650+
def obj(self, dtype):
651+
i8vals = date_range("2016-01-01", periods=3).asi8
652+
idx = Index(i8vals, dtype=dtype)
653+
assert idx.dtype == dtype
654+
return Series(idx)
687655

688656
@pytest.fixture(
689-
params=[NaT, np.timedelta64("NaT", "ns"), np.datetime64("NaT", "ns")]
657+
params=[
658+
None,
659+
np.nan,
660+
NaT,
661+
np.timedelta64("NaT", "ns"),
662+
np.datetime64("NaT", "ns"),
663+
]
690664
)
691665
def val(self, request):
692666
return request.param
693667

694668
@pytest.fixture
695669
def is_inplace(self, val, obj):
696-
if obj._values.tz is None:
697-
# cast to object iff val is timedelta64("NaT")
698-
return val is NaT or val.dtype.kind == "M"
699-
700-
# otherwise we have to exclude tznaive dt64("NaT")
701-
return val is NaT
670+
# td64 -> cast to object iff val is datetime64("NaT")
671+
# dt64 -> cast to object iff val is timedelta64("NaT")
672+
# dt64tz -> cast to object with anything _but_ NaT
673+
return val is NaT or val is None or val is np.nan or obj.dtype == val.dtype
702674

703675
@pytest.fixture
704676
def expected(self, obj, val, is_inplace):

0 commit comments

Comments
 (0)