Skip to content

Commit 1078363

Browse files
jbrockmendeljreback
authored andcommitted
CLN: NDFrame.setup_axes (#30064)
1 parent dd31670 commit 1078363

File tree

4 files changed

+35
-70
lines changed

4 files changed

+35
-70
lines changed

pandas/core/frame.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,8 @@ class DataFrame(NDFrame):
377377
2 7 8 9
378378
"""
379379

380+
_typ = "dataframe"
381+
380382
@property
381383
def _constructor(self) -> Type["DataFrame"]:
382384
return DataFrame
@@ -8143,10 +8145,6 @@ def isin(self, values):
81438145

81448146
DataFrame._setup_axes(
81458147
["index", "columns"],
8146-
info_axis=1,
8147-
stat_axis=0,
8148-
axes_are_reversed=True,
8149-
aliases={"rows": 0},
81508148
docs={
81518149
"index": "The index (row labels) of the DataFrame.",
81528150
"columns": "The column labels of the DataFrame.",

pandas/core/generic.py

Lines changed: 28 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ class NDFrame(PandasObject, SelectionMixin):
177177
_is_copy = None
178178
_data: BlockManager
179179
_attrs: Dict[Optional[Hashable], Any]
180+
_typ: str
180181

181182
# ----------------------------------------------------------------------
182183
# Constructors
@@ -283,71 +284,52 @@ def _constructor_expanddim(self):
283284

284285
# ----------------------------------------------------------------------
285286
# Axis
287+
_AXIS_ALIASES = {"rows": 0}
288+
_AXIS_IALIASES = {0: "rows"}
289+
_stat_axis_number = 0
290+
_stat_axis_name = "index"
291+
_ix = None
292+
_AXIS_ORDERS: List[str]
293+
_AXIS_NUMBERS: Dict[str, int]
294+
_AXIS_NAMES: Dict[int, str]
295+
_AXIS_REVERSED: bool
296+
_info_axis_number: int
297+
_info_axis_name: str
298+
_AXIS_LEN: int
286299

287300
@classmethod
288-
def _setup_axes(
289-
cls,
290-
axes,
291-
info_axis=None,
292-
stat_axis=None,
293-
aliases=None,
294-
axes_are_reversed=False,
295-
build_axes=True,
296-
ns=None,
297-
docs=None,
298-
):
301+
def _setup_axes(cls, axes: List[str], docs: Dict[str, str]):
299302
"""
300303
Provide axes setup for the major PandasObjects.
301304
302305
Parameters
303306
----------
304307
axes : the names of the axes in order (lowest to highest)
305-
info_axis_num : the axis of the selector dimension (int)
306-
stat_axis_num : the number of axis for the default stats (int)
307-
aliases : other names for a single axis (dict)
308-
axes_are_reversed : bool
309-
Whether to treat passed axes as reversed (DataFrame).
310-
build_axes : setup the axis properties (default True)
308+
docs : docstrings for the axis properties
311309
"""
310+
info_axis = len(axes) - 1
311+
axes_are_reversed = len(axes) > 1
312312

313313
cls._AXIS_ORDERS = axes
314314
cls._AXIS_NUMBERS = {a: i for i, a in enumerate(axes)}
315315
cls._AXIS_LEN = len(axes)
316-
cls._AXIS_ALIASES = aliases or dict()
317-
cls._AXIS_IALIASES = {v: k for k, v in cls._AXIS_ALIASES.items()}
318316
cls._AXIS_NAMES = dict(enumerate(axes))
319317
cls._AXIS_REVERSED = axes_are_reversed
320318

321-
# typ
322-
setattr(cls, "_typ", cls.__name__.lower())
323-
324-
# indexing support
325-
cls._ix = None
326-
327-
if info_axis is not None:
328-
cls._info_axis_number = info_axis
329-
cls._info_axis_name = axes[info_axis]
330-
331-
if stat_axis is not None:
332-
cls._stat_axis_number = stat_axis
333-
cls._stat_axis_name = axes[stat_axis]
319+
cls._info_axis_number = info_axis
320+
cls._info_axis_name = axes[info_axis]
334321

335322
# setup the actual axis
336-
if build_axes:
337-
338-
def set_axis(a, i):
339-
setattr(cls, a, properties.AxisProperty(i, docs.get(a, a)))
340-
cls._internal_names_set.add(a)
341-
342-
if axes_are_reversed:
343-
m = cls._AXIS_LEN - 1
344-
for i, a in cls._AXIS_NAMES.items():
345-
set_axis(a, m - i)
346-
else:
347-
for i, a in cls._AXIS_NAMES.items():
348-
set_axis(a, i)
323+
def set_axis(a, i):
324+
setattr(cls, a, properties.AxisProperty(i, docs.get(a, a)))
325+
cls._internal_names_set.add(a)
349326

350-
assert not isinstance(ns, dict)
327+
if axes_are_reversed:
328+
for i, a in cls._AXIS_NAMES.items():
329+
set_axis(a, 1 - i)
330+
else:
331+
for i, a in cls._AXIS_NAMES.items():
332+
set_axis(a, i)
351333

352334
def _construct_axes_dict(self, axes=None, **kwargs):
353335
"""Return an axes dictionary for myself."""
@@ -379,19 +361,6 @@ def _construct_axes_from_arguments(
379361
args = list(args)
380362
for a in self._AXIS_ORDERS:
381363

382-
# if we have an alias for this axis
383-
alias = self._AXIS_IALIASES.get(a)
384-
if alias is not None:
385-
if a in kwargs:
386-
if alias in kwargs:
387-
raise TypeError(
388-
f"arguments are mutually exclusive for [{a},{alias}]"
389-
)
390-
continue
391-
if alias in kwargs:
392-
kwargs[a] = kwargs.pop(alias)
393-
continue
394-
395364
# look for a argument by position
396365
if a not in kwargs:
397366
try:

pandas/core/series.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
153153
Copy input data.
154154
"""
155155

156+
_typ = "series"
157+
156158
_metadata: List[str] = []
157159
_accessors = {"dt", "cat", "str", "sparse"}
158160
_deprecations = (
@@ -4413,11 +4415,7 @@ def to_period(self, freq=None, copy=True):
44134415

44144416

44154417
Series._setup_axes(
4416-
["index"],
4417-
info_axis=0,
4418-
stat_axis=0,
4419-
aliases={"rows": 0},
4420-
docs={"index": "The index (axis labels) of the Series."},
4418+
["index"], docs={"index": "The index (axis labels) of the Series."},
44214419
)
44224420
Series._add_numeric_operations()
44234421
Series._add_series_only_operations()

pandas/tests/indexing/test_indexing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,12 +1139,12 @@ def test_extension_array_cross_section_converts():
11391139
(
11401140
lambda x: x.loc,
11411141
AttributeError,
1142-
"type object 'NDFrame' has no attribute '_AXIS_ALIASES'",
1142+
"type object 'NDFrame' has no attribute '_AXIS_NAMES'",
11431143
),
11441144
(
11451145
lambda x: x.iloc,
11461146
AttributeError,
1147-
"type object 'NDFrame' has no attribute '_AXIS_ALIASES'",
1147+
"type object 'NDFrame' has no attribute '_AXIS_NAMES'",
11481148
),
11491149
],
11501150
)

0 commit comments

Comments
 (0)