Skip to content

Commit 0a5c089

Browse files
arw2019luckyvs1
authored andcommitted
TYP: pandas/core/frame.py (easy: Axis/Level) (pandas-dev#38441)
* typing (Axis/Level) * review comment * replace List[Axis]->Sequence[Axis] in reorder_levels
1 parent bbe6bec commit 0a5c089

File tree

1 file changed

+45
-32
lines changed

1 file changed

+45
-32
lines changed

pandas/core/frame.py

+45-32
Original file line numberDiff line numberDiff line change
@@ -4050,7 +4050,7 @@ def _reindex_index(
40504050
new_index,
40514051
method,
40524052
copy: bool,
4053-
level,
4053+
level: Level,
40544054
fill_value=np.nan,
40554055
limit=None,
40564056
tolerance=None,
@@ -4070,7 +4070,7 @@ def _reindex_columns(
40704070
new_columns,
40714071
method,
40724072
copy: bool,
4073-
level,
4073+
level: Level,
40744074
fill_value=None,
40754075
limit=None,
40764076
tolerance=None,
@@ -4110,14 +4110,14 @@ def align(
41104110
self,
41114111
other,
41124112
join: str = "outer",
4113-
axis=None,
4114-
level=None,
4113+
axis: Optional[Axis] = None,
4114+
level: Optional[Level] = None,
41154115
copy: bool = True,
41164116
fill_value=None,
41174117
method: Optional[str] = None,
41184118
limit=None,
4119-
fill_axis=0,
4120-
broadcast_axis=None,
4119+
fill_axis: Axis = 0,
4120+
broadcast_axis: Optional[Axis] = None,
41214121
) -> DataFrame:
41224122
return super().align(
41234123
other,
@@ -4198,10 +4198,10 @@ def reindex(self, *args, **kwargs) -> DataFrame:
41984198
def drop(
41994199
self,
42004200
labels=None,
4201-
axis=0,
4201+
axis: Axis = 0,
42024202
index=None,
42034203
columns=None,
4204-
level=None,
4204+
level: Optional[Level] = None,
42054205
inplace: bool = False,
42064206
errors: str = "raise",
42074207
):
@@ -4474,7 +4474,7 @@ def fillna(
44744474
self,
44754475
value=None,
44764476
method: Optional[str] = None,
4477-
axis=None,
4477+
axis: Optional[Axis] = None,
44784478
inplace: bool = False,
44794479
limit=None,
44804480
downcast=None,
@@ -4587,7 +4587,7 @@ def _replace_columnwise(
45874587

45884588
@doc(NDFrame.shift, klass=_shared_doc_kwargs["klass"])
45894589
def shift(
4590-
self, periods=1, freq=None, axis=0, fill_value=lib.no_default
4590+
self, periods=1, freq=None, axis: Axis = 0, fill_value=lib.no_default
45914591
) -> DataFrame:
45924592
axis = self._get_axis_number(axis)
45934593

@@ -5074,7 +5074,12 @@ def notnull(self) -> DataFrame:
50745074
return ~self.isna()
50755075

50765076
def dropna(
5077-
self, axis=0, how: str = "any", thresh=None, subset=None, inplace: bool = False
5077+
self,
5078+
axis: Axis = 0,
5079+
how: str = "any",
5080+
thresh=None,
5081+
subset=None,
5082+
inplace: bool = False,
50785083
):
50795084
"""
50805085
Remove missing values.
@@ -5454,7 +5459,7 @@ def f(vals):
54545459
def sort_values( # type: ignore[override]
54555460
self,
54565461
by,
5457-
axis=0,
5462+
axis: Axis = 0,
54585463
ascending=True,
54595464
inplace: bool = False,
54605465
kind: str = "quicksort",
@@ -5514,8 +5519,8 @@ def sort_values( # type: ignore[override]
55145519

55155520
def sort_index(
55165521
self,
5517-
axis=0,
5518-
level=None,
5522+
axis: Axis = 0,
5523+
level: Optional[Level] = None,
55195524
ascending: bool = True,
55205525
inplace: bool = False,
55215526
kind: str = "quicksort",
@@ -5932,7 +5937,7 @@ def nsmallest(self, n, columns, keep: str = "first") -> DataFrame:
59325937
self, n=n, keep=keep, columns=columns
59335938
).nsmallest()
59345939

5935-
def swaplevel(self, i=-2, j=-1, axis=0) -> DataFrame:
5940+
def swaplevel(self, i: Axis = -2, j: Axis = -1, axis: Axis = 0) -> DataFrame:
59365941
"""
59375942
Swap levels i and j in a MultiIndex on a particular axis.
59385943
@@ -5963,7 +5968,7 @@ def swaplevel(self, i=-2, j=-1, axis=0) -> DataFrame:
59635968
result.columns = result.columns.swaplevel(i, j)
59645969
return result
59655970

5966-
def reorder_levels(self, order, axis=0) -> DataFrame:
5971+
def reorder_levels(self, order: Sequence[Axis], axis: Axis = 0) -> DataFrame:
59675972
"""
59685973
Rearrange index levels using input order. May not drop or duplicate levels.
59695974
@@ -6726,8 +6731,8 @@ def update(
67266731
def groupby(
67276732
self,
67286733
by=None,
6729-
axis=0,
6730-
level=None,
6734+
axis: Axis = 0,
6735+
level: Optional[Level] = None,
67316736
as_index: bool = True,
67326737
sort: bool = True,
67336738
group_keys: bool = True,
@@ -7080,7 +7085,7 @@ def pivot_table(
70807085
observed=observed,
70817086
)
70827087

7083-
def stack(self, level=-1, dropna: bool = True):
7088+
def stack(self, level: Level = -1, dropna: bool = True):
70847089
"""
70857090
Stack the prescribed level(s) from columns to index.
70867091
@@ -7399,7 +7404,7 @@ def melt(
73997404
value_vars=None,
74007405
var_name=None,
74017406
value_name="value",
7402-
col_level=None,
7407+
col_level: Optional[Level] = None,
74037408
ignore_index=True,
74047409
) -> DataFrame:
74057410

@@ -7607,7 +7612,7 @@ def _gotitem(
76077612
see_also=_agg_summary_and_see_also_doc,
76087613
examples=_agg_examples_doc,
76097614
)
7610-
def aggregate(self, func=None, axis=0, *args, **kwargs):
7615+
def aggregate(self, func=None, axis: Axis = 0, *args, **kwargs):
76117616
axis = self._get_axis_number(axis)
76127617

76137618
relabeling, func, columns, order = reconstruct_func(func, **kwargs)
@@ -7638,7 +7643,7 @@ def aggregate(self, func=None, axis=0, *args, **kwargs):
76387643

76397644
return result
76407645

7641-
def _aggregate(self, arg, axis=0, *args, **kwargs):
7646+
def _aggregate(self, arg, axis: Axis = 0, *args, **kwargs):
76427647
if axis == 1:
76437648
# NDFrame.aggregate returns a tuple, and we need to transpose
76447649
# only result
@@ -7661,7 +7666,9 @@ def transform(
76617666
assert isinstance(result, DataFrame)
76627667
return result
76637668

7664-
def apply(self, func, axis=0, raw: bool = False, result_type=None, args=(), **kwds):
7669+
def apply(
7670+
self, func, axis: Axis = 0, raw: bool = False, result_type=None, args=(), **kwds
7671+
):
76657672
"""
76667673
Apply a function along an axis of the DataFrame.
76677674
@@ -8578,7 +8585,7 @@ def cov(
85788585

85798586
return self._constructor(base_cov, index=idx, columns=cols)
85808587

8581-
def corrwith(self, other, axis=0, drop=False, method="pearson") -> Series:
8588+
def corrwith(self, other, axis: Axis = 0, drop=False, method="pearson") -> Series:
85828589
"""
85838590
Compute pairwise correlation.
85848591
@@ -8674,7 +8681,9 @@ def c(x):
86748681
# ----------------------------------------------------------------------
86758682
# ndarray-like stats methods
86768683

8677-
def count(self, axis=0, level=None, numeric_only: bool = False):
8684+
def count(
8685+
self, axis: Axis = 0, level: Optional[Level] = None, numeric_only: bool = False
8686+
):
86788687
"""
86798688
Count non-NA cells for each column or row.
86808689
@@ -8778,7 +8787,7 @@ def count(self, axis=0, level=None, numeric_only: bool = False):
87788787

87798788
return result.astype("int64")
87808789

8781-
def _count_level(self, level, axis=0, numeric_only=False):
8790+
def _count_level(self, level: Level, axis: Axis = 0, numeric_only=False):
87828791
if numeric_only:
87838792
frame = self._get_numeric_data()
87848793
else:
@@ -8828,7 +8837,7 @@ def _reduce(
88288837
op,
88298838
name: str,
88308839
*,
8831-
axis=0,
8840+
axis: Axis = 0,
88328841
skipna: bool = True,
88338842
numeric_only: Optional[bool] = None,
88348843
filter_type=None,
@@ -8936,7 +8945,7 @@ def _get_data() -> DataFrame:
89368945
result = self._constructor_sliced(result, index=labels)
89378946
return result
89388947

8939-
def nunique(self, axis=0, dropna: bool = True) -> Series:
8948+
def nunique(self, axis: Axis = 0, dropna: bool = True) -> Series:
89408949
"""
89418950
Count distinct observations over requested axis.
89428951
@@ -8976,7 +8985,7 @@ def nunique(self, axis=0, dropna: bool = True) -> Series:
89768985
"""
89778986
return self.apply(Series.nunique, axis=axis, dropna=dropna)
89788987

8979-
def idxmin(self, axis=0, skipna: bool = True) -> Series:
8988+
def idxmin(self, axis: Axis = 0, skipna: bool = True) -> Series:
89808989
"""
89818990
Return index of first occurrence of minimum over requested axis.
89828991
@@ -9053,7 +9062,7 @@ def idxmin(self, axis=0, skipna: bool = True) -> Series:
90539062
result = [index[i] if i >= 0 else np.nan for i in indices]
90549063
return self._constructor_sliced(result, index=self._get_agg_axis(axis))
90559064

9056-
def idxmax(self, axis=0, skipna: bool = True) -> Series:
9065+
def idxmax(self, axis: Axis = 0, skipna: bool = True) -> Series:
90579066
"""
90589067
Return index of first occurrence of maximum over requested axis.
90599068
@@ -9142,7 +9151,7 @@ def _get_agg_axis(self, axis_num: int) -> Index:
91429151
raise ValueError(f"Axis must be 0 or 1 (got {repr(axis_num)})")
91439152

91449153
def mode(
9145-
self, axis=0, numeric_only: bool = False, dropna: bool = True
9154+
self, axis: Axis = 0, numeric_only: bool = False, dropna: bool = True
91469155
) -> DataFrame:
91479156
"""
91489157
Get the mode(s) of each element along the selected axis.
@@ -9231,7 +9240,11 @@ def f(s):
92319240
return data.apply(f, axis=axis)
92329241

92339242
def quantile(
9234-
self, q=0.5, axis=0, numeric_only: bool = True, interpolation: str = "linear"
9243+
self,
9244+
q=0.5,
9245+
axis: Axis = 0,
9246+
numeric_only: bool = True,
9247+
interpolation: str = "linear",
92359248
):
92369249
"""
92379250
Return values at the given quantile over requested axis.

0 commit comments

Comments
 (0)