-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
TYP: type NDFrame.(_get_axis|_get_axis_name|_get_axis_number) #33610
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
@@ -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: | ||
|
@@ -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: | ||
|
@@ -590,7 +590,9 @@ def swapaxes(self: FrameOrSeries, axis1, axis2, copy=True) -> FrameOrSeries: | |
if copy: | ||
new_values = new_values.copy() | ||
|
||
return self._constructor(new_values, *new_axes).__finalize__( | ||
# ignore needed because of NDFrame constructor is different than | ||
# DataFrame/Series constructors. | ||
return self._constructor(new_values, *new_axes).__finalize__( # type: ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, can you add the mypy error as a comment. IIUC there were issues with deprecating this method, xref #26654. For series, it's a no-op. maybe now that Panel is deprecated, we could have logic in DataFrame and Series instead of NDFrame and simplify. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I remember that. The problem was that Maybe add |
||
self, method="swapaxes" | ||
) | ||
|
||
|
@@ -3490,6 +3492,8 @@ class animal locomotion | |
axis = self._get_axis_number(axis) | ||
labels = self._get_axis(axis) | ||
if level is not None: | ||
if not isinstance(labels, MultiIndex): | ||
raise TypeError("Index must be a MultiIndex") | ||
loc, new_ax = labels.get_loc_level(key, level=level, drop_level=drop_level) | ||
|
||
# create the tuple of the indexer | ||
|
@@ -7621,11 +7625,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( | ||
|
@@ -7704,16 +7708,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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
|
||
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( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can "axis" be typed as Axis?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, but in some cases
axis
is returned, so that that would require some refactoring inside the function, which I'd like to do seperately. For example I'm pretty sure_AXIS_ALIASES
can be removed from the code base.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense, thanks