Skip to content

TYP: Axis-related annotations #48907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ def agg_dict_like(self) -> DataFrame | Series:
ktu._set_names(selected_obj.columns.names)
keys_to_use = ktu

axis = 0 if isinstance(obj, ABCSeries) else 1
axis: AxisInt = 0 if isinstance(obj, ABCSeries) else 1
# error: Key expression in dictionary comprehension has incompatible type
# "Hashable"; expected type "NDFrame" [misc]
result = concat(
Expand Down Expand Up @@ -932,7 +932,7 @@ def apply_str(self) -> DataFrame | Series:


class FrameRowApply(FrameApply):
axis = 0
axis: AxisInt = 0

def apply_broadcast(self, target: DataFrame) -> DataFrame:
return super().apply_broadcast(target)
Expand Down Expand Up @@ -992,7 +992,7 @@ def wrap_results_for_axis(


class FrameColumnApply(FrameApply):
axis = 1
axis: AxisInt = 1

def apply_broadcast(self, target: DataFrame) -> DataFrame:
result = super().apply_broadcast(target.T)
Expand Down Expand Up @@ -1069,7 +1069,7 @@ def infer_to_same_shape(self, results: ResType, res_index: Index) -> DataFrame:

class SeriesApply(NDFrameApply):
obj: Series
axis = 0
axis: AxisInt = 0

def __init__(
self,
Expand Down Expand Up @@ -1199,7 +1199,7 @@ def transform(self):


class ResamplerWindowApply(Apply):
axis = 0
axis: AxisInt = 0
obj: Resampler | BaseWindow

def __init__(
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7586,7 +7586,7 @@ class diet
# Arithmetic Methods

def _cmp_method(self, other, op):
axis = 1 # only relevant for Series other case
axis: Literal[1] = 1 # only relevant for Series other case

self, other = ops.align_method_FRAME(self, other, axis, flex=False, level=None)

Expand All @@ -7598,7 +7598,7 @@ def _arith_method(self, other, op):
if ops.should_reindex_frame_op(self, other, op, 1, 1, None, None):
return ops.frame_arith_method_with_reindex(self, other, op)

axis = 1 # only relevant for Series other case
axis: Literal[1] = 1 # only relevant for Series other case
other = ops.maybe_prepare_scalar_for_op(other, (self.shape[axis],))

self, other = ops.align_method_FRAME(self, other, axis, flex=True, level=None)
Expand Down Expand Up @@ -10657,7 +10657,7 @@ def c(x):
if not drop:
# Find non-matching labels along the given axis
# and append missing correlations (GH 22375)
raxis = 1 if axis == 0 else 0
raxis: AxisInt = 1 if axis == 0 else 0
result_index = this._get_axis(raxis).union(other._get_axis(raxis))
idx_diff = result_index.difference(correl.index)

Expand Down Expand Up @@ -11666,7 +11666,7 @@ def isin(self, values: Series | DataFrame | Sequence | Mapping) -> DataFrame:
"columns": 1,
}
_AXIS_LEN = len(_AXIS_ORDERS)
_info_axis_number = 1
_info_axis_number: Literal[1] = 1
_info_axis_name: Literal["columns"] = "columns"

index = properties.AxisProperty(
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ def _get_axis(self, axis: Axis) -> Index:

@final
@classmethod
def _get_block_manager_axis(cls, axis: Axis) -> int:
def _get_block_manager_axis(cls, axis: Axis) -> AxisInt:
"""Map the axis to the block_manager axis."""
axis = cls._get_axis_number(axis)
ndim = cls._AXIS_LEN
Expand Down Expand Up @@ -1705,7 +1705,7 @@ def _is_level_reference(self, key: Level, axis: Axis = 0) -> bool_t:
)

@final
def _is_label_reference(self, key: Level, axis: int = 0) -> bool_t:
def _is_label_reference(self, key: Level, axis: Axis = 0) -> bool_t:
"""
Test whether a key is a label reference for a given axis.

Expand All @@ -1725,8 +1725,8 @@ def _is_label_reference(self, key: Level, axis: int = 0) -> bool_t:
-------
is_label: bool
"""
axis = self._get_axis_number(axis)
other_axes = (ax for ax in range(self._AXIS_LEN) if ax != axis)
axis_int = self._get_axis_number(axis)
other_axes = (ax for ax in range(self._AXIS_LEN) if ax != axis_int)

return (
key is not None
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ class _LocIndexer(_LocationIndexer):
# Key Checks

@doc(_LocationIndexer._validate_key)
def _validate_key(self, key, axis: AxisInt):
def _validate_key(self, key, axis: Axis):
# valid for a collection of labels (we check their presence later)
# slice of labels (where start-end in labels)
# slice of integers (only if in the labels)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/reshape/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from pandas._typing import (
Axis,
AxisInt,
HashableT,
)
from pandas.util._decorators import (
Expand Down Expand Up @@ -636,7 +637,7 @@ def _get_new_axes(self) -> list[Index]:
for i in range(ndim)
]

def _get_comb_axis(self, i: int) -> Index:
def _get_comb_axis(self, i: AxisInt) -> Index:
data_axis = self.objs[0]._get_block_manager_axis(i)
return get_objs_combined_axis(
self.objs,
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -6228,7 +6228,7 @@ def mask( # type: ignore[override]
# Add index
_AXIS_ORDERS: list[Literal["index", "columns"]] = ["index"]
_AXIS_LEN = len(_AXIS_ORDERS)
_info_axis_number = 0
_info_axis_number: Literal[0] = 0
_info_axis_name: Literal["index"] = "index"

index = properties.AxisProperty(
Expand Down
33 changes: 10 additions & 23 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def _ensure_decoded(s):
return s


def _ensure_encoding(encoding):
def _ensure_encoding(encoding: str | None) -> str:
# set the encoding if we need
if encoding is None:
encoding = _default_encoding
Expand Down Expand Up @@ -543,8 +543,6 @@ class HDFStore:

_handle: File | None
_mode: str
_complevel: int
_fletcher32: bool

def __init__(
self,
Expand Down Expand Up @@ -1043,7 +1041,7 @@ def select_as_multiple(
_tbls = [x for x in tbls if isinstance(x, Table)]

# axis is the concentration axes
axis = list({t.non_index_axes[0][0] for t in _tbls})[0]
axis = {t.non_index_axes[0][0] for t in _tbls}.pop()

def func(_start, _stop, _where):

Expand Down Expand Up @@ -1975,9 +1973,6 @@ class IndexCol:
is_data_indexable: bool = True
_info_fields = ["freq", "tz", "index_name"]

name: str
cname: str

def __init__(
self,
name: str,
Expand Down Expand Up @@ -2310,7 +2305,7 @@ def __init__(
values=None,
kind=None,
typ=None,
cname=None,
cname: str | None = None,
pos=None,
tz=None,
ordered=None,
Expand Down Expand Up @@ -2622,17 +2617,14 @@ class Fixed:
format_type: str = "fixed" # GH#30962 needed by dask
obj_type: type[DataFrame | Series]
ndim: int
encoding: str
parent: HDFStore
group: Node
errors: str
is_table: bool = False

def __init__(
self,
parent: HDFStore,
group: Node,
encoding: str = "UTF-8",
encoding: str|None = "UTF-8",
errors: str = "strict",
) -> None:
assert isinstance(parent, HDFStore), type(parent)
Expand Down Expand Up @@ -3306,24 +3298,19 @@ class Table(Fixed):
levels: int | list[Hashable] = 1
is_table = True

index_axes: list[IndexCol]
non_index_axes: list[tuple[int, Any]]
values_axes: list[DataCol]
data_columns: list
metadata: list
info: dict

def __init__(
self,
parent: HDFStore,
group: Node,
encoding=None,
encoding: str | None = None,
errors: str = "strict",
index_axes=None,
non_index_axes=None,
values_axes=None,
data_columns=None,
info=None,
index_axes: list[IndexCol] | None = None,
non_index_axes: list[tuple[AxisInt, Any]] | None = None,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My intenion was to only change the type here, but I also moved the class-level annotations into __init__.

values_axes: list[DataCol] | None = None,
data_columns: list | None = None,
info: dict | None = None,
nan_rep=None,
) -> None:
super().__init__(parent, group, encoding=encoding, errors=errors)
Expand Down