diff --git a/pandas/core/frame.py b/pandas/core/frame.py index fde3d1657b4f2..601dac3a1208b 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -377,6 +377,8 @@ class DataFrame(NDFrame): 2 7 8 9 """ + _typ = "dataframe" + @property def _constructor(self) -> Type["DataFrame"]: return DataFrame @@ -8143,10 +8145,6 @@ def isin(self, values): DataFrame._setup_axes( ["index", "columns"], - info_axis=1, - stat_axis=0, - axes_are_reversed=True, - aliases={"rows": 0}, docs={ "index": "The index (row labels) of the DataFrame.", "columns": "The column labels of the DataFrame.", diff --git a/pandas/core/generic.py b/pandas/core/generic.py index c52b1c65ad08d..15c7b1610788f 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -177,6 +177,7 @@ class NDFrame(PandasObject, SelectionMixin): _is_copy = None _data: BlockManager _attrs: Dict[Optional[Hashable], Any] + _typ: str # ---------------------------------------------------------------------- # Constructors @@ -283,71 +284,52 @@ def _constructor_expanddim(self): # ---------------------------------------------------------------------- # Axis + _AXIS_ALIASES = {"rows": 0} + _AXIS_IALIASES = {0: "rows"} + _stat_axis_number = 0 + _stat_axis_name = "index" + _ix = None + _AXIS_ORDERS: List[str] + _AXIS_NUMBERS: Dict[str, int] + _AXIS_NAMES: Dict[int, str] + _AXIS_REVERSED: bool + _info_axis_number: int + _info_axis_name: str + _AXIS_LEN: int @classmethod - def _setup_axes( - cls, - axes, - info_axis=None, - stat_axis=None, - aliases=None, - axes_are_reversed=False, - build_axes=True, - ns=None, - docs=None, - ): + def _setup_axes(cls, axes: List[str], docs: Dict[str, str]): """ Provide axes setup for the major PandasObjects. Parameters ---------- axes : the names of the axes in order (lowest to highest) - info_axis_num : the axis of the selector dimension (int) - stat_axis_num : the number of axis for the default stats (int) - aliases : other names for a single axis (dict) - axes_are_reversed : bool - Whether to treat passed axes as reversed (DataFrame). - build_axes : setup the axis properties (default True) + docs : docstrings for the axis properties """ + info_axis = len(axes) - 1 + axes_are_reversed = len(axes) > 1 cls._AXIS_ORDERS = axes cls._AXIS_NUMBERS = {a: i for i, a in enumerate(axes)} cls._AXIS_LEN = len(axes) - cls._AXIS_ALIASES = aliases or dict() - cls._AXIS_IALIASES = {v: k for k, v in cls._AXIS_ALIASES.items()} cls._AXIS_NAMES = dict(enumerate(axes)) cls._AXIS_REVERSED = axes_are_reversed - # typ - setattr(cls, "_typ", cls.__name__.lower()) - - # indexing support - cls._ix = None - - if info_axis is not None: - cls._info_axis_number = info_axis - cls._info_axis_name = axes[info_axis] - - if stat_axis is not None: - cls._stat_axis_number = stat_axis - cls._stat_axis_name = axes[stat_axis] + cls._info_axis_number = info_axis + cls._info_axis_name = axes[info_axis] # setup the actual axis - if build_axes: - - def set_axis(a, i): - setattr(cls, a, properties.AxisProperty(i, docs.get(a, a))) - cls._internal_names_set.add(a) - - if axes_are_reversed: - m = cls._AXIS_LEN - 1 - for i, a in cls._AXIS_NAMES.items(): - set_axis(a, m - i) - else: - for i, a in cls._AXIS_NAMES.items(): - set_axis(a, i) + def set_axis(a, i): + setattr(cls, a, properties.AxisProperty(i, docs.get(a, a))) + cls._internal_names_set.add(a) - assert not isinstance(ns, dict) + if axes_are_reversed: + for i, a in cls._AXIS_NAMES.items(): + set_axis(a, 1 - i) + else: + for i, a in cls._AXIS_NAMES.items(): + set_axis(a, i) def _construct_axes_dict(self, axes=None, **kwargs): """Return an axes dictionary for myself.""" @@ -379,19 +361,6 @@ def _construct_axes_from_arguments( args = list(args) for a in self._AXIS_ORDERS: - # if we have an alias for this axis - alias = self._AXIS_IALIASES.get(a) - if alias is not None: - if a in kwargs: - if alias in kwargs: - raise TypeError( - f"arguments are mutually exclusive for [{a},{alias}]" - ) - continue - if alias in kwargs: - kwargs[a] = kwargs.pop(alias) - continue - # look for a argument by position if a not in kwargs: try: diff --git a/pandas/core/series.py b/pandas/core/series.py index 410b10a69ecd5..6939571f41c9f 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -153,6 +153,8 @@ class Series(base.IndexOpsMixin, generic.NDFrame): Copy input data. """ + _typ = "series" + _metadata: List[str] = [] _accessors = {"dt", "cat", "str", "sparse"} _deprecations = ( @@ -4424,11 +4426,7 @@ def to_period(self, freq=None, copy=True): Series._setup_axes( - ["index"], - info_axis=0, - stat_axis=0, - aliases={"rows": 0}, - docs={"index": "The index (axis labels) of the Series."}, + ["index"], docs={"index": "The index (axis labels) of the Series."}, ) Series._add_numeric_operations() Series._add_series_only_operations() diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py index e53e02ed750cb..25b8713eb0307 100644 --- a/pandas/tests/indexing/test_indexing.py +++ b/pandas/tests/indexing/test_indexing.py @@ -1139,12 +1139,12 @@ def test_extension_array_cross_section_converts(): ( lambda x: x.loc, AttributeError, - "type object 'NDFrame' has no attribute '_AXIS_ALIASES'", + "type object 'NDFrame' has no attribute '_AXIS_NAMES'", ), ( lambda x: x.iloc, AttributeError, - "type object 'NDFrame' has no attribute '_AXIS_ALIASES'", + "type object 'NDFrame' has no attribute '_AXIS_NAMES'", ), ], )