10
10
from pandas .core .dtypes .common import is_float_dtype , is_integer_dtype
11
11
12
12
import pandas as pd
13
- from pandas import DataFrame , Index , NaT , Series , date_range
13
+ from pandas import DataFrame , Index , NaT , Series , date_range , offsets , timedelta_range
14
14
import pandas ._testing as tm
15
15
from pandas .core .indexing import maybe_numeric_slice , non_reducing_slice
16
16
from pandas .tests .indexing .common import _mklbl
@@ -970,19 +970,22 @@ class TestDatetimelikeCoercion:
970
970
@pytest .mark .parametrize ("indexer" , [setitem , loc , iloc ])
971
971
def test_setitem_dt64_string_scalar (self , tz_naive_fixture , indexer ):
972
972
# dispatching _can_hold_element to underling DatetimeArray
973
- # TODO(EA2D) use tz_naive_fixture once DatetimeBlock is backed by DTA
974
973
tz = tz_naive_fixture
975
974
976
975
dti = date_range ("2016-01-01" , periods = 3 , tz = tz )
977
976
ser = Series (dti )
978
977
979
978
values = ser ._values
980
979
981
- indexer (ser )[0 ] = "2018-01-01"
980
+ newval = "2018-01-01"
981
+ values ._validate_setitem_value (newval )
982
+
983
+ indexer (ser )[0 ] = newval
982
984
983
985
if tz is None :
984
986
# TODO(EA2D): we can make this no-copy in tz-naive case too
985
987
assert ser .dtype == dti .dtype
988
+ assert ser ._values ._data is values ._data
986
989
else :
987
990
assert ser ._values is values
988
991
@@ -993,7 +996,6 @@ def test_setitem_dt64_string_scalar(self, tz_naive_fixture, indexer):
993
996
@pytest .mark .parametrize ("indexer" , [setitem , loc , iloc ])
994
997
def test_setitem_dt64_string_values (self , tz_naive_fixture , indexer , key , box ):
995
998
# dispatching _can_hold_element to underling DatetimeArray
996
- # TODO(EA2D) use tz_naive_fixture once DatetimeBlock is backed by DTA
997
999
tz = tz_naive_fixture
998
1000
999
1001
if isinstance (key , slice ) and indexer is loc :
@@ -1012,9 +1014,44 @@ def test_setitem_dt64_string_values(self, tz_naive_fixture, indexer, key, box):
1012
1014
if tz is None :
1013
1015
# TODO(EA2D): we can make this no-copy in tz-naive case too
1014
1016
assert ser .dtype == dti .dtype
1017
+ assert ser ._values ._data is values ._data
1015
1018
else :
1016
1019
assert ser ._values is values
1017
1020
1021
+ @pytest .mark .parametrize ("scalar" , ["3 Days" , offsets .Hour (4 )])
1022
+ @pytest .mark .parametrize ("indexer" , [setitem , loc , iloc ])
1023
+ def test_setitem_td64_scalar (self , indexer , scalar ):
1024
+ # dispatching _can_hold_element to underling TimedeltaArray
1025
+ tdi = timedelta_range ("1 Day" , periods = 3 )
1026
+ ser = Series (tdi )
1027
+
1028
+ values = ser ._values
1029
+ values ._validate_setitem_value (scalar )
1030
+
1031
+ indexer (ser )[0 ] = scalar
1032
+ assert ser ._values ._data is values ._data
1033
+
1034
+ @pytest .mark .parametrize ("box" , [list , np .array , pd .array ])
1035
+ @pytest .mark .parametrize (
1036
+ "key" , [[0 , 1 ], slice (0 , 2 ), np .array ([True , True , False ])]
1037
+ )
1038
+ @pytest .mark .parametrize ("indexer" , [setitem , loc , iloc ])
1039
+ def test_setitem_td64_string_values (self , indexer , key , box ):
1040
+ # dispatching _can_hold_element to underling TimedeltaArray
1041
+ if isinstance (key , slice ) and indexer is loc :
1042
+ key = slice (0 , 1 )
1043
+
1044
+ tdi = timedelta_range ("1 Day" , periods = 3 )
1045
+ ser = Series (tdi )
1046
+
1047
+ values = ser ._values
1048
+
1049
+ newvals = box (["10 Days" , "44 hours" ])
1050
+ values ._validate_setitem_value (newvals )
1051
+
1052
+ indexer (ser )[key ] = newvals
1053
+ assert ser ._values ._data is values ._data
1054
+
1018
1055
1019
1056
def test_extension_array_cross_section ():
1020
1057
# A cross-section of a homogeneous EA should be an EA
0 commit comments