Skip to content

TYP: return values in core/*.py #47587

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

Merged
merged 14 commits into from
Jul 6, 2022
2 changes: 1 addition & 1 deletion pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ def value_counts(
# Called once from SparseArray, otherwise could be private
def value_counts_arraylike(
values: np.ndarray, dropna: bool, mask: npt.NDArray[np.bool_] | None = None
):
) -> tuple[ArrayLike, npt.NDArray[np.int64]]:
"""
Parameters
----------
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,9 +945,9 @@ def value_counts(self, dropna: bool = True) -> Series:
index = index.astype(self.dtype)

mask = np.zeros(len(counts), dtype="bool")
counts = IntegerArray(counts, mask)
counts_array = IntegerArray(counts, mask)

return Series(counts, index=index)
return Series(counts_array, index=index)

@doc(ExtensionArray.equals)
def equals(self, other) -> bool:
Expand Down
14 changes: 11 additions & 3 deletions pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,12 +889,20 @@ def value_counts(self, dropna: bool = True) -> Series:
if mask.any():
counts[mask] += fcounts
else:
keys = np.insert(keys, 0, self.fill_value)
# error: Argument 1 to "insert" has incompatible type "Union[
# ExtensionArray,ndarray[Any, Any]]"; expected "Union[
# _SupportsArray[dtype[Any]], Sequence[_SupportsArray[dtype
# [Any]]], Sequence[Sequence[_SupportsArray[dtype[Any]]]],
# Sequence[Sequence[Sequence[_SupportsArray[dtype[Any]]]]], Sequence
# [Sequence[Sequence[Sequence[_SupportsArray[dtype[Any]]]]]]]"
keys = np.insert(keys, 0, self.fill_value) # type: ignore[arg-type]
counts = np.insert(counts, 0, fcounts)

if not isinstance(keys, ABCIndex):
keys = Index(keys)
return Series(counts, index=keys)
index = Index(keys)
else:
index = keys
return Series(counts, index=index)

def _quantile(self, qs: npt.NDArray[np.float64], interpolation: str):

Expand Down
11 changes: 7 additions & 4 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@
NumpyValueArrayLike,
)

from pandas import Categorical
from pandas import (
Categorical,
Series,
)


_shared_docs: dict[str, str] = {}
Expand Down Expand Up @@ -161,7 +164,7 @@ def _freeze(self):
object.__setattr__(self, "__frozen", True)

# prevent adding any attribute via s.xxx.new_attribute = ...
def __setattr__(self, key: str, value):
def __setattr__(self, key: str, value) -> None:
# _cache is used by a decorator
# We need to check both 1.) cls.__dict__ and 2.) getattr(self, key)
# because
Expand Down Expand Up @@ -765,7 +768,7 @@ def hasnans(self) -> bool:
# has no attribute "any"
return bool(isna(self).any()) # type: ignore[union-attr]

def isna(self):
def isna(self) -> npt.NDArray[np.bool_]:
return isna(self._values)

def _reduce(
Expand Down Expand Up @@ -890,7 +893,7 @@ def value_counts(
ascending: bool = False,
bins=None,
dropna: bool = True,
):
) -> Series:
"""
Return a Series containing counts of unique values.

Expand Down
6 changes: 4 additions & 2 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ def temp_setattr(obj, attr: str, value) -> Iterator[None]:
setattr(obj, attr, old_value)


def require_length_match(data, index: Index):
def require_length_match(data, index: Index) -> None:
"""
Check the length of data matches the length of the index.
"""
Expand Down Expand Up @@ -665,7 +665,9 @@ def resolve_numeric_only(numeric_only: bool | None | lib.NoDefault) -> bool:
return result


def deprecate_numeric_only_default(cls: type, name: str, deprecate_none: bool = False):
def deprecate_numeric_only_default(
cls: type, name: str, deprecate_none: bool = False
) -> None:
"""Emit FutureWarning message for deprecation of numeric_only.

See GH#46560 for details on the deprecation.
Expand Down
14 changes: 7 additions & 7 deletions pandas/core/config_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"""


def use_bottleneck_cb(key):
def use_bottleneck_cb(key) -> None:
from pandas.core import nanops

nanops.set_use_bottleneck(cf.get_option(key))
Expand All @@ -51,7 +51,7 @@ def use_bottleneck_cb(key):
"""


def use_numexpr_cb(key):
def use_numexpr_cb(key) -> None:
from pandas.core.computation import expressions

expressions.set_use_numexpr(cf.get_option(key))
Expand All @@ -65,7 +65,7 @@ def use_numexpr_cb(key):
"""


def use_numba_cb(key):
def use_numba_cb(key) -> None:
from pandas.core.util import numba_

numba_.set_use_numba(cf.get_option(key))
Expand Down Expand Up @@ -329,7 +329,7 @@ def use_numba_cb(key):
"""


def table_schema_cb(key):
def table_schema_cb(key) -> None:
from pandas.io.formats.printing import enable_data_resource_formatter

enable_data_resource_formatter(cf.get_option(key))
Expand Down Expand Up @@ -500,7 +500,7 @@ def _deprecate_negative_int_max_colwidth(key):
# or we'll hit circular deps.


def use_inf_as_na_cb(key):
def use_inf_as_na_cb(key) -> None:
from pandas.core.dtypes.missing import _use_inf_as_na

_use_inf_as_na(key)
Expand Down Expand Up @@ -720,7 +720,7 @@ def use_inf_as_na_cb(key):
"""


def register_plotting_backend_cb(key):
def register_plotting_backend_cb(key) -> None:
if key == "matplotlib":
# We defer matplotlib validation, since it's the default
return
Expand All @@ -746,7 +746,7 @@ def register_plotting_backend_cb(key):
"""


def register_converter_cb(key):
def register_converter_cb(key) -> None:
from pandas.plotting import (
deregister_matplotlib_converters,
register_matplotlib_converters,
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def allows_duplicate_labels(self) -> bool:
return self._allows_duplicate_labels

@allows_duplicate_labels.setter
def allows_duplicate_labels(self, value: bool):
def allows_duplicate_labels(self, value: bool) -> None:
value = bool(value)
obj = self._obj()
if obj is None:
Expand All @@ -99,12 +99,12 @@ def __getitem__(self, key):

return getattr(self, key)

def __setitem__(self, key, value):
def __setitem__(self, key, value) -> None:
if key not in self._keys:
raise ValueError(f"Unknown flag {key}. Must be one of {self._keys}")
setattr(self, key, value)

def __repr__(self):
def __repr__(self) -> str:
return f"<Flags(allows_duplicate_labels={self.allows_duplicate_labels})>"

def __eq__(self, other):
Expand Down
Loading