Skip to content

Commit 1003668

Browse files
committed
DOC: add further type hints to public API methods in pandas/core/generic.py (pandas-dev#26792)
1 parent 1effb56 commit 1003668

File tree

1 file changed

+62
-26
lines changed

1 file changed

+62
-26
lines changed

pandas/core/generic.py

+62-26
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
Optional,
2020
Sequence,
2121
Set,
22+
Tuple,
2223
Union,
2324
)
2425
import warnings
@@ -90,6 +91,7 @@
9091
from pandas.io.formats.printing import pprint_thing
9192
from pandas.tseries.frequencies import to_offset
9293

94+
9395
# goal is to be able to define the docs close to function, while still being
9496
# able to share
9597
_shared_docs = dict() # type: Dict[str, str]
@@ -552,7 +554,7 @@ def _stat_axis(self):
552554
return getattr(self, self._stat_axis_name)
553555

554556
@property
555-
def shape(self):
557+
def shape(self) -> Tuple[int]:
556558
"""
557559
Return a tuple of axis dimensions
558560
"""
@@ -568,7 +570,7 @@ def axes(self):
568570
return [self._get_axis(a) for a in self._AXIS_ORDERS]
569571

570572
@property
571-
def ndim(self):
573+
def ndim(self) -> int:
572574
"""
573575
Return an int representing the number of axes / array dimensions.
574576
@@ -591,7 +593,7 @@ def ndim(self):
591593
return self._data.ndim
592594

593595
@property
594-
def size(self):
596+
def size(self) -> int:
595597
"""
596598
Return an int representing the number of elements in this object.
597599
@@ -624,7 +626,10 @@ def _obj_with_exclusions(self):
624626
""" internal compat with SelectionMixin """
625627
return self
626628

627-
def set_axis(self, labels, axis=0, inplace=False):
629+
def set_axis(self,
630+
labels: Sequence[Any],
631+
axis=0,
632+
inplace=False) -> "NDFrame":
628633
"""
629634
Assign desired index to given axis.
630635
@@ -727,7 +732,7 @@ def _set_axis(self, axis, labels):
727732
self._data.set_axis(axis, labels)
728733
self._clear_item_cache()
729734

730-
def transpose(self, *args, **kwargs):
735+
def transpose(self, *args, **kwargs) -> "NDFrame":
731736
"""
732737
Permute the dimensions of the %(klass)s
733738
@@ -771,7 +776,10 @@ def transpose(self, *args, **kwargs):
771776
nv.validate_transpose(tuple(), kwargs)
772777
return self._constructor(new_values, **new_axes).__finalize__(self)
773778

774-
def swapaxes(self, axis1, axis2, copy=True):
779+
def swapaxes(self,
780+
axis1: int,
781+
axis2: int,
782+
copy=True) -> "NDFrame":
775783
"""
776784
Interchange axes and swap values axes appropriately.
777785
@@ -796,7 +804,9 @@ def swapaxes(self, axis1, axis2, copy=True):
796804

797805
return self._constructor(new_values, *new_axes).__finalize__(self)
798806

799-
def droplevel(self, level, axis=0):
807+
def droplevel(self,
808+
level: Union[int, str, Sequence[Union[int, str]]],
809+
axis=0) -> "NDFrame":
800810
"""
801811
Return DataFrame with requested index / column level(s) removed.
802812
@@ -855,7 +865,8 @@ def droplevel(self, level, axis=0):
855865
result = self.set_axis(new_labels, axis=axis, inplace=False)
856866
return result
857867

858-
def pop(self, item):
868+
def pop(self,
869+
item: str) -> "pd.Series":
859870
"""
860871
Return item and drop from frame. Raise KeyError if not found.
861872
@@ -905,7 +916,7 @@ def pop(self, item):
905916

906917
return result
907918

908-
def squeeze(self, axis=None):
919+
def squeeze(self, axis=None) -> "NDFrame":
909920
"""
910921
Squeeze 1 dimensional axis objects into scalars.
911922
@@ -1016,7 +1027,7 @@ def squeeze(self, axis=None):
10161027
)
10171028
]
10181029

1019-
def swaplevel(self, i=-2, j=-1, axis=0):
1030+
def swaplevel(self, i=-2, j=-1, axis=0) -> "NDFrame":
10201031
"""
10211032
Swap levels i and j in a MultiIndex on a particular axis
10221033
@@ -1038,7 +1049,7 @@ def swaplevel(self, i=-2, j=-1, axis=0):
10381049
# ----------------------------------------------------------------------
10391050
# Rename
10401051

1041-
def rename(self, *args, **kwargs):
1052+
def rename(self, *args, **kwargs) -> "NDFrame":
10421053
"""
10431054
Alter axes input function or functions. Function / dict values must be
10441055
unique (1-to-1). Labels not contained in a dict / Series will be left
@@ -1203,7 +1214,7 @@ def rename(self, *args, **kwargs):
12031214
return result.__finalize__(self)
12041215

12051216
@rewrite_axis_style_signature("mapper", [("copy", True), ("inplace", False)])
1206-
def rename_axis(self, mapper=sentinel, **kwargs):
1217+
def rename_axis(self, mapper=sentinel, **kwargs) -> "NDFrame":
12071218
"""
12081219
Set the name of the axis for the index or columns.
12091220
@@ -1373,7 +1384,10 @@ class name
13731384
if not inplace:
13741385
return result
13751386

1376-
def _set_axis_name(self, name, axis=0, inplace=False):
1387+
def _set_axis_name(self,
1388+
name: Union[str, List[str]],
1389+
axis=0,
1390+
inplace=False) -> "NDFrame":
13771391
"""
13781392
Set the name(s) of the axis.
13791393
@@ -1442,7 +1456,8 @@ def _indexed_same(self, other):
14421456
self._get_axis(a).equals(other._get_axis(a)) for a in self._AXIS_ORDERS
14431457
)
14441458

1445-
def equals(self, other):
1459+
def equals(self,
1460+
other: "NDFrame") -> bool:
14461461
"""
14471462
Test whether two objects contain the same elements.
14481463
@@ -1581,7 +1596,7 @@ def __nonzero__(self):
15811596

15821597
__bool__ = __nonzero__
15831598

1584-
def bool(self):
1599+
def bool(self) -> bool:
15851600
"""
15861601
Return the bool of a single element PandasObject.
15871602
@@ -1619,7 +1634,9 @@ def __round__(self, decimals=0):
16191634
# operations should utilize/extend these methods when possible so that we
16201635
# have consistent precedence and validation logic throughout the library.
16211636

1622-
def _is_level_reference(self, key, axis=0):
1637+
def _is_level_reference(self,
1638+
key: str,
1639+
axis=0) -> bool:
16231640
"""
16241641
Test whether a key is a level reference for a given axis.
16251642
@@ -1649,7 +1666,9 @@ def _is_level_reference(self, key, axis=0):
16491666
and not self._is_label_reference(key, axis=axis)
16501667
)
16511668

1652-
def _is_label_reference(self, key, axis=0):
1669+
def _is_label_reference(self,
1670+
key: str,
1671+
axis=0) -> bool:
16531672
"""
16541673
Test whether a key is a label reference for a given axis.
16551674
@@ -1678,7 +1697,9 @@ def _is_label_reference(self, key, axis=0):
16781697
and any(key in self.axes[ax] for ax in other_axes)
16791698
)
16801699

1681-
def _is_label_or_level_reference(self, key, axis=0):
1700+
def _is_label_or_level_reference(self,
1701+
key: str,
1702+
axis=0) -> bool:
16821703
"""
16831704
Test whether a key is a label or level reference for a given axis.
16841705
@@ -1702,7 +1723,9 @@ def _is_label_or_level_reference(self, key, axis=0):
17021723
key, axis=axis
17031724
)
17041725

1705-
def _check_label_or_level_ambiguity(self, key, axis=0):
1726+
def _check_label_or_level_ambiguity(self,
1727+
key: str,
1728+
axis=0) -> None:
17061729
"""
17071730
Check whether `key` is ambiguous.
17081731
@@ -1751,7 +1774,9 @@ def _check_label_or_level_ambiguity(self, key, axis=0):
17511774
)
17521775
raise ValueError(msg)
17531776

1754-
def _get_label_or_level_values(self, key, axis=0):
1777+
def _get_label_or_level_values(self,
1778+
key: str,
1779+
axis=0) -> np.ndarray:
17551780
"""
17561781
Return a 1-D array of values associated with `key`, a label or level
17571782
from the given `axis`.
@@ -1823,7 +1848,9 @@ def _get_label_or_level_values(self, key, axis=0):
18231848

18241849
return values
18251850

1826-
def _drop_labels_or_levels(self, keys, axis=0):
1851+
def _drop_labels_or_levels(self,
1852+
keys: Union[str, List[str]],
1853+
axis=0) -> "NDFrame":
18271854
"""
18281855
Drop labels and/or levels for the given `axis`.
18291856
@@ -1959,7 +1986,7 @@ def __contains__(self, key):
19591986
return key in self._info_axis
19601987

19611988
@property
1962-
def empty(self):
1989+
def empty(self) -> bool:
19631990
"""
19641991
Indicator whether DataFrame is empty.
19651992
@@ -2474,7 +2501,10 @@ def to_json(
24742501
indent=indent,
24752502
)
24762503

2477-
def to_hdf(self, path_or_buf, key, **kwargs):
2504+
def to_hdf(self,
2505+
path_or_buf: Union[str, FilePathOrBuffer],
2506+
key: str,
2507+
**kwargs) -> None:
24782508
"""
24792509
Write the contained data to an HDF5 file using HDFStore.
24802510
@@ -2579,7 +2609,10 @@ def to_hdf(self, path_or_buf, key, **kwargs):
25792609

25802610
pytables.to_hdf(path_or_buf, key, self, **kwargs)
25812611

2582-
def to_msgpack(self, path_or_buf=None, encoding="utf-8", **kwargs):
2612+
def to_msgpack(self,
2613+
path_or_buf: Optional[FilePathOrBuffer] =None,
2614+
encoding="utf-8",
2615+
**kwargs) -> None:
25832616
"""
25842617
Serialize object to input file path using msgpack format.
25852618
@@ -2775,7 +2808,10 @@ def to_sql(
27752808
method=method,
27762809
)
27772810

2778-
def to_pickle(self, path, compression="infer", protocol=pickle.HIGHEST_PROTOCOL):
2811+
def to_pickle(self,
2812+
path: str,
2813+
compression="infer",
2814+
protocol=pickle.HIGHEST_PROTOCOL) -> None:
27792815
"""
27802816
Pickle (serialize) object to file.
27812817
@@ -2831,7 +2867,7 @@ def to_pickle(self, path, compression="infer", protocol=pickle.HIGHEST_PROTOCOL)
28312867

28322868
to_pickle(self, path, compression=compression, protocol=protocol)
28332869

2834-
def to_clipboard(self, excel=True, sep=None, **kwargs):
2870+
def to_clipboard(self, excel=True, sep=None, **kwargs) -> None:
28352871
r"""
28362872
Copy object to the system clipboard.
28372873

0 commit comments

Comments
 (0)