10
10
import numpy as np
11
11
import pytest
12
12
13
+ import pandas .util ._test_decorators as td
14
+
13
15
from pandas import (
14
16
Categorical ,
15
17
CategoricalDtype ,
@@ -63,26 +65,30 @@ class TestiLocBaseIndependent:
63
65
],
64
66
)
65
67
@pytest .mark .parametrize ("indexer" , [tm .loc , tm .iloc ])
66
- def test_iloc_setitem_fullcol_categorical (self , indexer , key ):
68
+ def test_iloc_setitem_fullcol_categorical (self , indexer , key , using_array_manager ):
67
69
frame = DataFrame ({0 : range (3 )}, dtype = object )
68
70
69
71
cat = Categorical (["alpha" , "beta" , "gamma" ])
70
72
71
- assert frame ._mgr .blocks [0 ]._can_hold_element (cat )
73
+ if not using_array_manager :
74
+ assert frame ._mgr .blocks [0 ]._can_hold_element (cat )
72
75
73
76
df = frame .copy ()
74
77
orig_vals = df .values
75
78
indexer (df )[key , 0 ] = cat
76
79
77
80
overwrite = isinstance (key , slice ) and key == slice (None )
78
81
79
- if overwrite :
82
+ if overwrite or using_array_manager :
83
+ # TODO(ArrayManager) we always overwrite because ArrayManager takes
84
+ # the "split" path, which still overwrites
80
85
# TODO: GH#39986 this probably shouldn't behave differently
81
86
expected = DataFrame ({0 : cat })
82
87
assert not np .shares_memory (df .values , orig_vals )
83
88
else :
84
89
expected = DataFrame ({0 : cat }).astype (object )
85
- assert np .shares_memory (df .values , orig_vals )
90
+ if not using_array_manager :
91
+ assert np .shares_memory (df [0 ].values , orig_vals )
86
92
87
93
tm .assert_frame_equal (df , expected )
88
94
@@ -93,13 +99,27 @@ def test_iloc_setitem_fullcol_categorical(self, indexer, key):
93
99
else :
94
100
assert cat [0 ] != "gamma"
95
101
102
+ # TODO with mixed dataframe ("split" path), we always overwrite the column
103
+ frame = DataFrame ({0 : np .array ([0 , 1 , 2 ], dtype = object ), 1 : range (3 )})
104
+ df = frame .copy ()
105
+ orig_vals = df .values
106
+ indexer (df )[key , 0 ] = cat
107
+ expected = DataFrame ({0 : cat , 1 : range (3 )})
108
+ tm .assert_frame_equal (df , expected )
109
+
110
+ # TODO(ArrayManager) does not yet update parent
111
+ @td .skip_array_manager_not_yet_implemented
96
112
@pytest .mark .parametrize ("box" , [array , Series ])
97
- def test_iloc_setitem_ea_inplace (self , frame_or_series , box ):
113
+ def test_iloc_setitem_ea_inplace (self , frame_or_series , box , using_array_manager ):
98
114
# GH#38952 Case with not setting a full column
99
115
# IntegerArray without NAs
100
116
arr = array ([1 , 2 , 3 , 4 ])
101
117
obj = frame_or_series (arr .to_numpy ("i8" ))
102
- values = obj .values
118
+
119
+ if frame_or_series is Series or not using_array_manager :
120
+ values = obj .values
121
+ else :
122
+ values = obj [0 ].values
103
123
104
124
obj .iloc [:2 ] = box (arr [2 :])
105
125
expected = frame_or_series (np .array ([3 , 4 , 3 , 4 ], dtype = "i8" ))
@@ -109,7 +129,10 @@ def test_iloc_setitem_ea_inplace(self, frame_or_series, box):
109
129
if frame_or_series is Series :
110
130
assert obj .values is values
111
131
else :
112
- assert obj .values .base is values .base and values .base is not None
132
+ if using_array_manager :
133
+ assert obj [0 ].values is values
134
+ else :
135
+ assert obj .values .base is values .base and values .base is not None
113
136
114
137
def test_is_scalar_access (self ):
115
138
# GH#32085 index with duplicates doesn't matter for _is_scalar_access
@@ -481,13 +504,16 @@ def test_iloc_setitem_dups(self):
481
504
df .iloc [[1 , 0 ], [0 , 1 ]] = df .iloc [[1 , 0 ], [0 , 1 ]].reset_index (drop = True )
482
505
tm .assert_frame_equal (df , expected )
483
506
484
- def test_iloc_setitem_frame_duplicate_columns_multiple_blocks (self ):
507
+ def test_iloc_setitem_frame_duplicate_columns_multiple_blocks (
508
+ self , using_array_manager
509
+ ):
485
510
# Same as the "assign back to self" check in test_iloc_setitem_dups
486
511
# but on a DataFrame with multiple blocks
487
512
df = DataFrame ([[0 , 1 ], [2 , 3 ]], columns = ["B" , "B" ])
488
513
489
514
df .iloc [:, 0 ] = df .iloc [:, 0 ].astype ("f8" )
490
- assert len (df ._mgr .blocks ) == 2
515
+ if not using_array_manager :
516
+ assert len (df ._mgr .blocks ) == 2
491
517
expected = df .copy ()
492
518
493
519
# assign back to self
@@ -577,7 +603,7 @@ def test_iloc_getitem_labelled_frame(self):
577
603
with pytest .raises (ValueError , match = msg ):
578
604
df .iloc ["j" , "D" ]
579
605
580
- def test_iloc_getitem_doc_issue (self ):
606
+ def test_iloc_getitem_doc_issue (self , using_array_manager ):
581
607
582
608
# multi axis slicing issue with single block
583
609
# surfaced in GH 6059
@@ -612,7 +638,8 @@ def test_iloc_getitem_doc_issue(self):
612
638
columns = list (range (0 , 8 , 2 ))
613
639
df = DataFrame (arr , index = index , columns = columns )
614
640
615
- df ._mgr .blocks [0 ].mgr_locs
641
+ if not using_array_manager :
642
+ df ._mgr .blocks [0 ].mgr_locs
616
643
result = df .iloc [1 :5 , 2 :4 ]
617
644
str (result )
618
645
result .dtypes
@@ -793,15 +820,20 @@ def test_iloc_empty_list_indexer_is_ok(self):
793
820
df .iloc [[]], df .iloc [:0 , :], check_index_type = True , check_column_type = True
794
821
)
795
822
796
- def test_identity_slice_returns_new_object (self ):
823
+ def test_identity_slice_returns_new_object (self , using_array_manager ):
797
824
# GH13873
798
825
original_df = DataFrame ({"a" : [1 , 2 , 3 ]})
799
826
sliced_df = original_df .iloc [:]
800
827
assert sliced_df is not original_df
801
828
802
829
# should be a shallow copy
803
830
original_df ["a" ] = [4 , 4 , 4 ]
804
- assert (sliced_df ["a" ] == 4 ).all ()
831
+ if using_array_manager :
832
+ # TODO(ArrayManager) verify it is expected that the original didn't change
833
+ # setitem is replacing full column, so doesn't update "viewing" dataframe
834
+ assert not (sliced_df ["a" ] == 4 ).all ()
835
+ else :
836
+ assert (sliced_df ["a" ] == 4 ).all ()
805
837
806
838
original_series = Series ([1 , 2 , 3 , 4 , 5 , 6 ])
807
839
sliced_series = original_series .iloc [:]
@@ -932,6 +964,9 @@ def test_iloc_getitem_readonly_key(self):
932
964
expected = df ["data" ].loc [[1 , 3 , 6 ]]
933
965
tm .assert_series_equal (result , expected )
934
966
967
+ # TODO(ArrayManager) setting single item with an iterable doesn't work yet
968
+ # in the "split" path
969
+ @td .skip_array_manager_not_yet_implemented
935
970
def test_iloc_assign_series_to_df_cell (self ):
936
971
# GH 37593
937
972
df = DataFrame (columns = ["a" ], index = [0 ])
@@ -1088,6 +1123,8 @@ def test_iloc_getitem_setitem_fancy_exceptions(self, float_frame):
1088
1123
# GH#32257 we let numpy do validation, get their exception
1089
1124
float_frame .iloc [:, :, :] = 1
1090
1125
1126
+ # TODO(ArrayManager) "split" path doesn't properly implement DataFrame indexer
1127
+ @td .skip_array_manager_not_yet_implemented
1091
1128
def test_iloc_frame_indexer (self ):
1092
1129
# GH#39004
1093
1130
df = DataFrame ({"a" : [1 , 2 , 3 ]})
0 commit comments