diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 0af5ffc767a1e..99e91e01845ee 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -22,6 +22,7 @@ TYPE_CHECKING, Any, AnyStr, + Callable, Dict, FrozenSet, Hashable, @@ -71,6 +72,7 @@ NpDtype, PythonFuncType, Renamer, + Scalar, StorageOptions, Suffixes, ValueKeyFunc, @@ -1215,7 +1217,9 @@ def iterrows(self) -> Iterable[Tuple[Hashable, Series]]: s = klass(v, index=columns, name=k) yield k, s - def itertuples(self, index: bool = True, name: Optional[str] = "Pandas"): + def itertuples( + self, index: bool = True, name: Optional[str] = "Pandas" + ) -> Iterable[Tuple[Any, ...]]: """ Iterate over DataFrame rows as namedtuples. @@ -1459,7 +1463,11 @@ def __rmatmul__(self, other): @classmethod def from_dict( - cls, data, orient="columns", dtype: Optional[Dtype] = None, columns=None + cls, + data, + orient: str = "columns", + dtype: Optional[Dtype] = None, + columns=None, ) -> DataFrame: """ Construct DataFrame from dict of array-like or dicts. @@ -1897,7 +1905,7 @@ def from_records( exclude=None, columns=None, coerce_float: bool = False, - nrows=None, + nrows: Optional[int] = None, ) -> DataFrame: """ Convert structured or record ndarray to DataFrame. @@ -3087,7 +3095,7 @@ def info( show_counts=show_counts, ) - def memory_usage(self, index=True, deep=False) -> Series: + def memory_usage(self, index: bool = True, deep: bool = False) -> Series: """ Return the memory usage of each column in bytes. @@ -3489,7 +3497,7 @@ def _getitem_multilevel(self, key): # loc is neither a slice nor ndarray, so must be an int return self._ixs(loc, axis=1) - def _get_value(self, index, col, takeable: bool = False): + def _get_value(self, index, col, takeable: bool = False) -> Scalar: """ Quickly retrieve single value at passed column and index. @@ -3657,7 +3665,7 @@ def _iset_item(self, loc: int, value): if len(self): self._check_setitem_copy() - def _set_item(self, key, value): + def _set_item(self, key, value) -> None: """ Add series to DataFrame in specified column. @@ -3682,16 +3690,21 @@ def _set_item(self, key, value): self._set_item_mgr(key, value) - def _set_value(self, index, col, value, takeable: bool = False): + def _set_value( + self, index: IndexLabel, col, value: Scalar, takeable: bool = False + ) -> None: """ Put single value at passed column and index. Parameters ---------- - index : row label - col : column label + index : Label + row label + col : Label + column label value : scalar - takeable : interpret the index/col as indexers, default False + takeable : bool, default False + Sets whether or not index/col interpreted as indexers """ try: if takeable: @@ -3715,7 +3728,7 @@ def _set_value(self, index, col, value, takeable: bool = False): self.loc[index, col] = value self._item_cache.pop(col, None) - def _ensure_valid_index(self, value): + def _ensure_valid_index(self, value) -> None: """ Ensure that if we don't have an index, that we can create one from the passed value. @@ -3912,6 +3925,7 @@ def query(self, expr: str, inplace: bool = False, **kwargs): if inplace: self._update_inplace(result) + return None else: return result @@ -4379,7 +4393,9 @@ def _series(self): for idx, item in enumerate(self.columns) } - def lookup(self, row_labels, col_labels) -> np.ndarray: + def lookup( + self, row_labels: Sequence[IndexLabel], col_labels: Sequence[IndexLabel] + ) -> np.ndarray: """ Label-based "fancy indexing" function for DataFrame. Given equal-length arrays of row and column labels, return an @@ -6574,7 +6590,7 @@ def _arith_method(self, other, op): _logical_method = _arith_method - def _dispatch_frame_op(self, right, func, axis: Optional[int] = None): + def _dispatch_frame_op(self, right, func: Callable, axis: Optional[int] = None): """ Evaluate the frame operation func(left, right) by evaluating column-by-column, dispatching to the Series implementation. @@ -7910,7 +7926,7 @@ def explode( return result - def unstack(self, level=-1, fill_value=None): + def unstack(self, level: Level = -1, fill_value=None): """ Pivot a level of the (necessarily hierarchical) index labels. @@ -7981,7 +7997,7 @@ def melt( var_name=None, value_name="value", col_level: Optional[Level] = None, - ignore_index=True, + ignore_index: bool = True, ) -> DataFrame: return melt( @@ -8846,7 +8862,9 @@ def merge( validate=validate, ) - def round(self, decimals=0, *args, **kwargs) -> DataFrame: + def round( + self, decimals: Union[int, Dict[IndexLabel, int], Series] = 0, *args, **kwargs + ) -> DataFrame: """ Round a DataFrame to a variable number of decimal places. @@ -8960,7 +8978,11 @@ def _series_round(s, decimals): # ---------------------------------------------------------------------- # Statistical methods, etc. - def corr(self, method="pearson", min_periods=1) -> DataFrame: + def corr( + self, + method: Union[str, Callable[[np.ndarray, np.ndarray], float]] = "pearson", + min_periods: int = 1, + ) -> DataFrame: """ Compute pairwise correlation of columns, excluding NA/null values.