Skip to content

Commit a017402

Browse files
committed
REF: remove axes from Managers
1 parent ff441ec commit a017402

File tree

14 files changed

+199
-62
lines changed

14 files changed

+199
-62
lines changed

pandas/_libs/properties.pyx

+3-2
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ cdef class AxisProperty:
6161
if obj is None:
6262
# Only instances have _mgr, not classes
6363
return self
64+
if self.axis == 0:
65+
return obj._index
6466
else:
65-
axes = obj._mgr.axes
66-
return axes[self.axis]
67+
return obj._columns
6768

6869
def __set__(self, obj, value):
6970
obj._set_axis(self.axis, value)

pandas/core/arraylike.py

+6
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,12 @@ def _reconstruct(result):
358358
return result
359359
if isinstance(result, BlockManager):
360360
# we went through BlockManager.apply e.g. np.sqrt
361+
# TODO: any cases that aren't index/columns-preserving?
362+
if self.ndim == 1:
363+
reconstruct_kwargs["index"] = self.index
364+
else:
365+
reconstruct_kwargs["index"] = self.index
366+
reconstruct_kwargs["columns"] = self.columns
361367
result = self._constructor(result, **reconstruct_kwargs, copy=False)
362368
else:
363369
# we converted an array, lost our axes

pandas/core/frame.py

+31-16
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ class DataFrame(NDFrame, OpsMixin):
586586
2 2 3
587587
"""
588588

589-
_internal_names_set = {"columns", "index"} | NDFrame._internal_names_set
589+
_internal_names_set = {"_columns", "columns", "_index", "index"} | NDFrame._internal_names_set
590590
_typ = "dataframe"
591591
_HANDLED_TYPES = (Series, Index, ExtensionArray, np.ndarray)
592592
_accessors: set[str] = {"sparse"}
@@ -617,11 +617,20 @@ def __init__(
617617
dtype = self._validate_dtype(dtype)
618618

619619
if isinstance(data, DataFrame):
620+
if index is None and columns is None:
621+
index = data.index
622+
columns = data.columns
620623
data = data._mgr
621624

622625
if isinstance(data, (BlockManager, ArrayManager)):
623626
# first check if a Manager is passed without any other arguments
624627
# -> use fastpath (without checking Manager type)
628+
if index is None or columns is None:
629+
assert False
630+
if not index.equals(data.axes[-1]):#index is not data.axes[-1]:
631+
assert False
632+
if not columns.equals(data.axes[0]):#columns is not data.axes[0]:
633+
assert False
625634
if index is None and columns is None and dtype is None and not copy:
626635
# GH#33357 fastpath
627636
NDFrame.__init__(self, data)
@@ -747,7 +756,7 @@ def __init__(
747756
index, # type: ignore[arg-type]
748757
dtype,
749758
)
750-
mgr = arrays_to_mgr(
759+
mgr, _, _ = arrays_to_mgr(
751760
arrays,
752761
columns,
753762
index,
@@ -790,7 +799,7 @@ def __init__(
790799
construct_1d_arraylike_from_scalar(data, len(index), dtype)
791800
for _ in range(len(columns))
792801
]
793-
mgr = arrays_to_mgr(values, columns, index, dtype=None, typ=manager)
802+
mgr, _, _ = arrays_to_mgr(values, columns, index, dtype=None, typ=manager)
794803
else:
795804
arr2d = construct_2d_arraylike_from_scalar(
796805
data,
@@ -2354,9 +2363,10 @@ def maybe_reorder(
23542363
columns = columns.drop(exclude)
23552364

23562365
manager = get_option("mode.data_manager")
2357-
mgr = arrays_to_mgr(arrays, columns, result_index, typ=manager)
2366+
mgr, index, columns = arrays_to_mgr(arrays, columns, result_index, typ=manager)
23582367

2359-
return cls(mgr)
2368+
# FIXME: get axes without mgr.axes
2369+
return cls(mgr, index=index, columns=columns)
23602370

23612371
def to_records(
23622372
self, index: bool = True, column_dtypes=None, index_dtypes=None
@@ -2558,15 +2568,15 @@ def _from_arrays(
25582568
columns = ensure_index(columns)
25592569
if len(columns) != len(arrays):
25602570
raise ValueError("len(columns) must match len(arrays)")
2561-
mgr = arrays_to_mgr(
2571+
mgr, index, columns = arrays_to_mgr(
25622572
arrays,
25632573
columns,
25642574
index,
25652575
dtype=dtype,
25662576
verify_integrity=verify_integrity,
25672577
typ=manager,
25682578
)
2569-
return cls(mgr)
2579+
return cls(mgr, index=index, columns=columns)
25702580

25712581
@doc(
25722582
storage_options=_shared_docs["storage_options"],
@@ -3683,7 +3693,7 @@ def _ixs(self, i: int, axis: int = 0) -> Series:
36833693

36843694
# if we are a copy, mark as such
36853695
copy = isinstance(new_mgr.array, np.ndarray) and new_mgr.array.base is None
3686-
result = self._constructor_sliced(new_mgr, name=self.index[i]).__finalize__(
3696+
result = self._constructor_sliced(new_mgr, index=self.columns, name=self.index[i]).__finalize__(
36873697
self
36883698
)
36893699
result._set_is_copy(self, copy=copy)
@@ -4198,7 +4208,7 @@ def _box_col_values(self, values: SingleDataManager, loc: int) -> Series:
41984208
name = self.columns[loc]
41994209
klass = self._constructor_sliced
42004210
# We get index=self.index bc values is a SingleDataManager
4201-
return klass(values, name=name, fastpath=True).__finalize__(self)
4211+
return klass(values, name=name, index=self.index, fastpath=True).__finalize__(self)
42024212

42034213
# ----------------------------------------------------------------------
42044214
# Lookup Caching
@@ -6884,8 +6894,12 @@ def sort_values( # type: ignore[override]
68846894
new_data.set_axis(
68856895
self._get_block_manager_axis(axis), default_index(len(indexer))
68866896
)
6887-
6888-
result = self._constructor(new_data)
6897+
# FIXME: get axes without mgr.axes
6898+
axes_dict = {}
6899+
axes_dict["index"] = new_data.axes[-1]
6900+
if self.ndim == 2:
6901+
axes_dict["columns"] = new_data.axes[0]
6902+
result = self._constructor(new_data, **axes_dict)
68896903
if inplace:
68906904
return self._update_inplace(result)
68916905
else:
@@ -7569,7 +7583,7 @@ def _dispatch_frame_op(self, right, func: Callable, axis: int | None = None):
75697583
# i.e. scalar, faster than checking np.ndim(right) == 0
75707584
with np.errstate(all="ignore"):
75717585
bm = self._mgr.apply(array_op, right=right)
7572-
return self._constructor(bm)
7586+
return self._constructor(bm, index=self.index, columns=self.columns)
75737587

75747588
elif isinstance(right, DataFrame):
75757589
assert self.index.equals(right.index)
@@ -7590,7 +7604,7 @@ def _dispatch_frame_op(self, right, func: Callable, axis: int | None = None):
75907604
right._mgr, # type: ignore[arg-type]
75917605
array_op,
75927606
)
7593-
return self._constructor(bm)
7607+
return self._constructor(bm, index=self.index, columns=self.columns)
75947608

75957609
elif isinstance(right, Series) and axis == 1:
75967610
# axis=1 means we want to operate row-by-row
@@ -10833,7 +10847,8 @@ def _get_data() -> DataFrame:
1083310847
# After possibly _get_data and transposing, we are now in the
1083410848
# simple case where we can use BlockManager.reduce
1083510849
res, _ = df._mgr.reduce(blk_func, ignore_failures=ignore_failures)
10836-
out = df._constructor(res).iloc[0]
10850+
# FIXME: get axes without mgr.axes
10851+
out = df._constructor(res, index=res.axes[1], columns=res.axes[0]).iloc[0]
1083710852
if out_dtype is not None:
1083810853
out = out.astype(out_dtype)
1083910854
if axis == 0 and len(self) == 0 and name in ["sum", "prod"]:
@@ -11541,9 +11556,9 @@ def isin(self, values: Series | DataFrame | Sequence | Mapping) -> DataFrame:
1154111556
_info_axis_name = "columns"
1154211557

1154311558
index = properties.AxisProperty(
11544-
axis=1, doc="The index (row labels) of the DataFrame."
11559+
axis=0, doc="The index (row labels) of the DataFrame."
1154511560
)
11546-
columns = properties.AxisProperty(axis=0, doc="The column labels of the DataFrame.")
11561+
columns = properties.AxisProperty(axis=1, doc="The column labels of the DataFrame.")
1154711562

1154811563
@property
1154911564
def _AXIS_NUMBERS(self) -> dict[str, int]:

0 commit comments

Comments
 (0)