1010import numpy as np
1111import pytest
1212
13+ import pandas .util ._test_decorators as td
14+
1315from pandas import (
1416 Categorical ,
1517 CategoricalDtype ,
@@ -63,26 +65,30 @@ class TestiLocBaseIndependent:
6365 ],
6466 )
6567 @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 ):
6769 frame = DataFrame ({0 : range (3 )}, dtype = object )
6870
6971 cat = Categorical (["alpha" , "beta" , "gamma" ])
7072
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 )
7275
7376 df = frame .copy ()
7477 orig_vals = df .values
7578 indexer (df )[key , 0 ] = cat
7679
7780 overwrite = isinstance (key , slice ) and key == slice (None )
7881
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
8085 # TODO: GH#39986 this probably shouldn't behave differently
8186 expected = DataFrame ({0 : cat })
8287 assert not np .shares_memory (df .values , orig_vals )
8388 else :
8489 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 )
8692
8793 tm .assert_frame_equal (df , expected )
8894
@@ -93,13 +99,27 @@ def test_iloc_setitem_fullcol_categorical(self, indexer, key):
9399 else :
94100 assert cat [0 ] != "gamma"
95101
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
96112 @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 ):
98114 # GH#38952 Case with not setting a full column
99115 # IntegerArray without NAs
100116 arr = array ([1 , 2 , 3 , 4 ])
101117 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
103123
104124 obj .iloc [:2 ] = box (arr [2 :])
105125 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):
109129 if frame_or_series is Series :
110130 assert obj .values is values
111131 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
113136
114137 def test_is_scalar_access (self ):
115138 # GH#32085 index with duplicates doesn't matter for _is_scalar_access
@@ -481,13 +504,16 @@ def test_iloc_setitem_dups(self):
481504 df .iloc [[1 , 0 ], [0 , 1 ]] = df .iloc [[1 , 0 ], [0 , 1 ]].reset_index (drop = True )
482505 tm .assert_frame_equal (df , expected )
483506
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+ ):
485510 # Same as the "assign back to self" check in test_iloc_setitem_dups
486511 # but on a DataFrame with multiple blocks
487512 df = DataFrame ([[0 , 1 ], [2 , 3 ]], columns = ["B" , "B" ])
488513
489514 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
491517 expected = df .copy ()
492518
493519 # assign back to self
@@ -577,7 +603,7 @@ def test_iloc_getitem_labelled_frame(self):
577603 with pytest .raises (ValueError , match = msg ):
578604 df .iloc ["j" , "D" ]
579605
580- def test_iloc_getitem_doc_issue (self ):
606+ def test_iloc_getitem_doc_issue (self , using_array_manager ):
581607
582608 # multi axis slicing issue with single block
583609 # surfaced in GH 6059
@@ -612,7 +638,8 @@ def test_iloc_getitem_doc_issue(self):
612638 columns = list (range (0 , 8 , 2 ))
613639 df = DataFrame (arr , index = index , columns = columns )
614640
615- df ._mgr .blocks [0 ].mgr_locs
641+ if not using_array_manager :
642+ df ._mgr .blocks [0 ].mgr_locs
616643 result = df .iloc [1 :5 , 2 :4 ]
617644 str (result )
618645 result .dtypes
@@ -793,15 +820,20 @@ def test_iloc_empty_list_indexer_is_ok(self):
793820 df .iloc [[]], df .iloc [:0 , :], check_index_type = True , check_column_type = True
794821 )
795822
796- def test_identity_slice_returns_new_object (self ):
823+ def test_identity_slice_returns_new_object (self , using_array_manager ):
797824 # GH13873
798825 original_df = DataFrame ({"a" : [1 , 2 , 3 ]})
799826 sliced_df = original_df .iloc [:]
800827 assert sliced_df is not original_df
801828
802829 # should be a shallow copy
803830 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 ()
805837
806838 original_series = Series ([1 , 2 , 3 , 4 , 5 , 6 ])
807839 sliced_series = original_series .iloc [:]
@@ -932,6 +964,9 @@ def test_iloc_getitem_readonly_key(self):
932964 expected = df ["data" ].loc [[1 , 3 , 6 ]]
933965 tm .assert_series_equal (result , expected )
934966
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
935970 def test_iloc_assign_series_to_df_cell (self ):
936971 # GH 37593
937972 df = DataFrame (columns = ["a" ], index = [0 ])
@@ -1088,6 +1123,8 @@ def test_iloc_getitem_setitem_fancy_exceptions(self, float_frame):
10881123 # GH#32257 we let numpy do validation, get their exception
10891124 float_frame .iloc [:, :, :] = 1
10901125
1126+ # TODO(ArrayManager) "split" path doesn't properly implement DataFrame indexer
1127+ @td .skip_array_manager_not_yet_implemented
10911128 def test_iloc_frame_indexer (self ):
10921129 # GH#39004
10931130 df = DataFrame ({"a" : [1 , 2 , 3 ]})
0 commit comments