Skip to content

Commit

Permalink
TYP: type NDFrame.(_get_axis|_get_axis_name|_get_axis_number)
Browse files Browse the repository at this point in the history
  • Loading branch information
topper-123 committed Apr 17, 2020
1 parent 556e1c2 commit dd72723
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def _construct_axes_from_arguments(
return axes, kwargs

@classmethod
def _get_axis_number(cls, axis):
def _get_axis_number(cls, axis) -> int:
axis = cls._AXIS_ALIASES.get(axis, axis)
if is_integer(axis):
if axis in cls._AXIS_NAMES:
Expand All @@ -366,7 +366,7 @@ def _get_axis_number(cls, axis):
raise ValueError(f"No axis named {axis} for object type {cls.__name__}")

@classmethod
def _get_axis_name(cls, axis):
def _get_axis_name(cls, axis) -> str:
axis = cls._AXIS_ALIASES.get(axis, axis)
if isinstance(axis, str):
if axis in cls._AXIS_NUMBERS:
Expand All @@ -378,12 +378,12 @@ def _get_axis_name(cls, axis):
pass
raise ValueError(f"No axis named {axis} for object type {cls.__name__}")

def _get_axis(self, axis):
def _get_axis(self, axis) -> Index:
name = self._get_axis_name(axis)
return getattr(self, name)

@classmethod
def _get_block_manager_axis(cls, axis):
def _get_block_manager_axis(cls, axis) -> int:
"""Map the axis to the block_manager axis."""
axis = cls._get_axis_number(axis)
if cls._AXIS_REVERSED:
Expand Down Expand Up @@ -590,7 +590,7 @@ def swapaxes(self: FrameOrSeries, axis1, axis2, copy=True) -> FrameOrSeries:
if copy:
new_values = new_values.copy()

return self._constructor(new_values, *new_axes).__finalize__(
return self._constructor(new_values, *new_axes).__finalize__( # type: ignore
self, method="swapaxes"
)

Expand Down Expand Up @@ -3490,6 +3490,7 @@ class animal locomotion
axis = self._get_axis_number(axis)
labels = self._get_axis(axis)
if level is not None:
assert isinstance(labels, MultiIndex), type(labels)
loc, new_ax = labels.get_loc_level(key, level=level, drop_level=drop_level)

# create the tuple of the indexer
Expand Down Expand Up @@ -7621,11 +7622,11 @@ def at_time(
axis = self._get_axis_number(axis)

index = self._get_axis(axis)
try:
indexer = index.indexer_at_time(time, asof=asof)
except AttributeError as err:
raise TypeError("Index must be DatetimeIndex") from err

if not isinstance(index, DatetimeIndex):
raise TypeError("Index must be DatetimeIndex")

indexer = index.indexer_at_time(time, asof=asof)
return self._take_with_is_copy(indexer, axis=axis)

def between_time(
Expand Down Expand Up @@ -7704,16 +7705,12 @@ def between_time(
axis = self._get_axis_number(axis)

index = self._get_axis(axis)
try:
indexer = index.indexer_between_time(
start_time,
end_time,
include_start=include_start,
include_end=include_end,
)
except AttributeError as err:
raise TypeError("Index must be DatetimeIndex") from err
if not isinstance(index, DatetimeIndex):
raise TypeError("Index must be DatetimeIndex")

indexer = index.indexer_between_time(
start_time, end_time, include_start=include_start, include_end=include_end,
)
return self._take_with_is_copy(indexer, axis=axis)

def resample(
Expand Down

0 comments on commit dd72723

Please sign in to comment.