8
8
import pytest
9
9
10
10
from pandas ._libs import iNaT
11
+ import pandas .util ._test_decorators as td
11
12
12
13
from pandas .core .dtypes .common import is_integer
13
14
@@ -534,6 +535,7 @@ def test_getitem_setitem_integer_slice_keyerrors(self):
534
535
with pytest .raises (KeyError , match = r"^3$" ):
535
536
df2 .loc [3 :11 ] = 0
536
537
538
+ @td .skip_array_manager_invalid_test # already covered in test_iloc_col_slice_view
537
539
def test_fancy_getitem_slice_mixed (self , float_frame , float_string_frame ):
538
540
sliced = float_string_frame .iloc [:, - 3 :]
539
541
assert sliced ["D" ].dtype == np .float64
@@ -592,6 +594,7 @@ def test_getitem_fancy_scalar(self, float_frame):
592
594
for idx in f .index [::5 ]:
593
595
assert ix [idx , col ] == ts [idx ]
594
596
597
+ @td .skip_array_manager_invalid_test # TODO(ArrayManager) rewrite not using .values
595
598
def test_setitem_fancy_scalar (self , float_frame ):
596
599
f = float_frame
597
600
expected = float_frame .copy ()
@@ -631,6 +634,7 @@ def test_getitem_fancy_boolean(self, float_frame):
631
634
expected = f .reindex (index = f .index [boolvec ], columns = ["C" , "D" ])
632
635
tm .assert_frame_equal (result , expected )
633
636
637
+ @td .skip_array_manager_invalid_test # TODO(ArrayManager) rewrite not using .values
634
638
def test_setitem_fancy_boolean (self , float_frame ):
635
639
# from 2d, set with booleans
636
640
frame = float_frame .copy ()
@@ -990,21 +994,29 @@ def test_iloc_row(self):
990
994
expected = df .loc [8 :14 ]
991
995
tm .assert_frame_equal (result , expected )
992
996
997
+ # list of integers
998
+ result = df .iloc [[1 , 2 , 4 , 6 ]]
999
+ expected = df .reindex (df .index [[1 , 2 , 4 , 6 ]])
1000
+ tm .assert_frame_equal (result , expected )
1001
+
1002
+ def test_iloc_row_slice_view (self , using_array_manager ):
1003
+ df = DataFrame (np .random .randn (10 , 4 ), index = range (0 , 20 , 2 ))
1004
+ original = df .copy ()
1005
+
993
1006
# verify slice is view
994
1007
# setting it makes it raise/warn
1008
+ subset = df .iloc [slice (4 , 8 )]
1009
+
995
1010
msg = r"\nA value is trying to be set on a copy of a slice from a DataFrame"
996
1011
with pytest .raises (com .SettingWithCopyError , match = msg ):
997
- result [2 ] = 0.0
1012
+ subset [2 ] = 0.0
998
1013
999
- exp_col = df [2 ].copy ()
1000
- exp_col [4 :8 ] = 0.0
1014
+ exp_col = original [2 ].copy ()
1015
+ # TODO(ArrayManager) verify it is expected that the original didn't change
1016
+ if not using_array_manager :
1017
+ exp_col [4 :8 ] = 0.0
1001
1018
tm .assert_series_equal (df [2 ], exp_col )
1002
1019
1003
- # list of integers
1004
- result = df .iloc [[1 , 2 , 4 , 6 ]]
1005
- expected = df .reindex (df .index [[1 , 2 , 4 , 6 ]])
1006
- tm .assert_frame_equal (result , expected )
1007
-
1008
1020
def test_iloc_col (self ):
1009
1021
1010
1022
df = DataFrame (np .random .randn (4 , 10 ), columns = range (0 , 20 , 2 ))
@@ -1022,19 +1034,32 @@ def test_iloc_col(self):
1022
1034
expected = df .loc [:, 8 :14 ]
1023
1035
tm .assert_frame_equal (result , expected )
1024
1036
1025
- # verify slice is view
1026
- # and that we are setting a copy
1027
- msg = r"\nA value is trying to be set on a copy of a slice from a DataFrame"
1028
- with pytest .raises (com .SettingWithCopyError , match = msg ):
1029
- result [8 ] = 0.0
1030
-
1031
- assert (df [8 ] == 0 ).all ()
1032
-
1033
1037
# list of integers
1034
1038
result = df .iloc [:, [1 , 2 , 4 , 6 ]]
1035
1039
expected = df .reindex (columns = df .columns [[1 , 2 , 4 , 6 ]])
1036
1040
tm .assert_frame_equal (result , expected )
1037
1041
1042
+ def test_iloc_col_slice_view (self , using_array_manager ):
1043
+ df = DataFrame (np .random .randn (4 , 10 ), columns = range (0 , 20 , 2 ))
1044
+ original = df .copy ()
1045
+ subset = df .iloc [:, slice (4 , 8 )]
1046
+
1047
+ if not using_array_manager :
1048
+ # verify slice is view
1049
+ # and that we are setting a copy
1050
+ msg = r"\nA value is trying to be set on a copy of a slice from a DataFrame"
1051
+ with pytest .raises (com .SettingWithCopyError , match = msg ):
1052
+ subset [8 ] = 0.0
1053
+
1054
+ assert (df [8 ] == 0 ).all ()
1055
+ else :
1056
+ # TODO(ArrayManager) verify this is the desired behaviour
1057
+ subset [8 ] = 0.0
1058
+ # subset changed
1059
+ assert (subset [8 ] == 0 ).all ()
1060
+ # but df itself did not change (setitem replaces full column)
1061
+ tm .assert_frame_equal (df , original )
1062
+
1038
1063
def test_loc_duplicates (self ):
1039
1064
# gh-17105
1040
1065
@@ -1218,7 +1243,7 @@ def test_setitem(self, uint64_frame):
1218
1243
)
1219
1244
1220
1245
1221
- def test_object_casting_indexing_wraps_datetimelike ():
1246
+ def test_object_casting_indexing_wraps_datetimelike (using_array_manager ):
1222
1247
# GH#31649, check the indexing methods all the way down the stack
1223
1248
df = DataFrame (
1224
1249
{
@@ -1240,6 +1265,10 @@ def test_object_casting_indexing_wraps_datetimelike():
1240
1265
assert isinstance (ser .values [1 ], Timestamp )
1241
1266
assert isinstance (ser .values [2 ], pd .Timedelta )
1242
1267
1268
+ if using_array_manager :
1269
+ # remainder of the test checking BlockManager internals
1270
+ return
1271
+
1243
1272
mgr = df ._mgr
1244
1273
mgr ._rebuild_blknos_and_blklocs ()
1245
1274
arr = mgr .fast_xs (0 )
0 commit comments