62
62
IndexKeyFunc ,
63
63
IndexLabel ,
64
64
Level ,
65
+ Manager ,
65
66
PythonFuncType ,
66
67
Renamer ,
67
68
StorageOptions ,
137
138
)
138
139
from pandas .core .indexes .multi import MultiIndex , maybe_droplevels
139
140
from pandas .core .indexing import check_bool_indexer , convert_to_index_sliceable
140
- from pandas .core .internals import BlockManager
141
+ from pandas .core .internals import ArrayManager , BlockManager
141
142
from pandas .core .internals .construction import (
142
143
arrays_to_mgr ,
143
144
dataclasses_to_dicts ,
144
145
init_dict ,
145
146
init_ndarray ,
146
147
masked_rec_array_to_mgr ,
148
+ mgr_to_mgr ,
147
149
nested_data_to_arrays ,
148
150
reorder_arrays ,
149
151
sanitize_index ,
@@ -523,7 +525,7 @@ def __init__(
523
525
if isinstance (data , DataFrame ):
524
526
data = data ._mgr
525
527
526
- if isinstance (data , BlockManager ):
528
+ if isinstance (data , ( BlockManager , ArrayManager ) ):
527
529
if index is None and columns is None and dtype is None and copy is False :
528
530
# GH#33357 fastpath
529
531
NDFrame .__init__ (self , data )
@@ -601,8 +603,31 @@ def __init__(
601
603
values , index , columns , dtype = values .dtype , copy = False
602
604
)
603
605
606
+ # ensure correct Manager type according to settings
607
+ manager = get_option ("mode.data_manager" )
608
+ mgr = mgr_to_mgr (mgr , typ = manager )
609
+
604
610
NDFrame .__init__ (self , mgr )
605
611
612
+ def _as_manager (self , typ : str ) -> DataFrame :
613
+ """
614
+ Private helper function to create a DataFrame with specific manager.
615
+
616
+ Parameters
617
+ ----------
618
+ typ : {"block", "array"}
619
+
620
+ Returns
621
+ -------
622
+ DataFrame
623
+ New DataFrame using specified manager type. Is not guaranteed
624
+ to be a copy or not.
625
+ """
626
+ new_mgr : Manager
627
+ new_mgr = mgr_to_mgr (self ._mgr , typ = typ )
628
+ # fastpath of passing a manager doesn't check the option/manager class
629
+ return DataFrame (new_mgr )
630
+
606
631
# ----------------------------------------------------------------------
607
632
608
633
@property
@@ -675,6 +700,8 @@ def _is_homogeneous_type(self) -> bool:
675
700
... "B": np.array([1, 2], dtype=np.int64)})._is_homogeneous_type
676
701
False
677
702
"""
703
+ if isinstance (self ._mgr , ArrayManager ):
704
+ return len ({arr .dtype for arr in self ._mgr .arrays }) == 1
678
705
if self ._mgr .any_extension_types :
679
706
return len ({block .dtype for block in self ._mgr .blocks }) == 1
680
707
else :
@@ -685,6 +712,8 @@ def _can_fast_transpose(self) -> bool:
685
712
"""
686
713
Can we transpose this DataFrame without creating any new array objects.
687
714
"""
715
+ if isinstance (self ._mgr , ArrayManager ):
716
+ return False
688
717
if self ._mgr .any_extension_types :
689
718
# TODO(EA2D) special case would be unnecessary with 2D EAs
690
719
return False
@@ -5506,7 +5535,7 @@ def sort_values( # type: ignore[override]
5506
5535
)
5507
5536
5508
5537
if ignore_index :
5509
- new_data .axes [ 1 ] = ibase .default_index (len (indexer ))
5538
+ new_data .set_axis ( 1 , ibase .default_index (len (indexer ) ))
5510
5539
5511
5540
result = self ._constructor (new_data )
5512
5541
if inplace :
@@ -6051,7 +6080,10 @@ def _dispatch_frame_op(self, right, func, axis: Optional[int] = None):
6051
6080
# fails in cases with empty columns reached via
6052
6081
# _frame_arith_method_with_reindex
6053
6082
6054
- bm = self ._mgr .operate_blockwise (right ._mgr , array_op )
6083
+ # TODO operate_blockwise expects a manager of the same type
6084
+ bm = self ._mgr .operate_blockwise (
6085
+ right ._mgr , array_op # type: ignore[arg-type]
6086
+ )
6055
6087
return type (self )(bm )
6056
6088
6057
6089
elif isinstance (right , Series ) and axis == 1 :
@@ -8894,11 +8926,11 @@ def func(values: np.ndarray):
8894
8926
# We only use this in the case that operates on self.values
8895
8927
return op (values , axis = axis , skipna = skipna , ** kwds )
8896
8928
8897
- def blk_func (values ):
8929
+ def blk_func (values , axis = 1 ):
8898
8930
if isinstance (values , ExtensionArray ):
8899
8931
return values ._reduce (name , skipna = skipna , ** kwds )
8900
8932
else :
8901
- return op (values , axis = 1 , skipna = skipna , ** kwds )
8933
+ return op (values , axis = axis , skipna = skipna , ** kwds )
8902
8934
8903
8935
def _get_data () -> DataFrame :
8904
8936
if filter_type is None :
0 commit comments