Skip to content

TYP: Series.quantile #47304

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 3 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,6 @@ def closed(self) -> bool:
# sort_index
SortKind = Literal["quicksort", "mergesort", "heapsort", "stable"]
NaPosition = Literal["first", "last"]

# quantile interpolation
QuantileInterpolation = Literal["linear", "lower", "higher", "midpoint", "nearest"]
6 changes: 3 additions & 3 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ def is_bool_indexer(key: Any) -> bool:
is_array_like(key) and is_extension_array_dtype(key.dtype)
):
if key.dtype == np.object_:
key = np.asarray(key)
key_array = np.asarray(key)

if not lib.is_bool_array(key):
if not lib.is_bool_array(key_array):
na_msg = "Cannot mask with non-boolean array containing NA / NaN values"
if lib.infer_dtype(key) == "boolean" and isna(key).any():
if lib.infer_dtype(key_array) == "boolean" and isna(key_array).any():
# Don't raise on e.g. ["A", "B", np.nan], see
# test_loc_getitem_list_of_labels_categoricalindex_with_na
raise ValueError(na_msg)
Expand Down
30 changes: 29 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from pandas._libs.lib import no_default
from pandas._typing import (
AggFuncType,
AnyArrayLike,
ArrayLike,
Axis,
Dtype,
Expand All @@ -42,6 +43,7 @@
IndexKeyFunc,
Level,
NaPosition,
QuantileInterpolation,
Renamer,
SingleManager,
SortKind,
Expand Down Expand Up @@ -2478,7 +2480,33 @@ def round(self, decimals=0, *args, **kwargs) -> Series:

return result

def quantile(self, q=0.5, interpolation="linear"):
@overload
def quantile(
self, q: float = ..., interpolation: QuantileInterpolation = ...
) -> float:
...

@overload
def quantile(
self,
q: Sequence[float] | AnyArrayLike,
interpolation: QuantileInterpolation = ...,
) -> Series:
...

@overload
def quantile(
self,
q: float | Sequence[float] | AnyArrayLike = ...,
interpolation: QuantileInterpolation = ...,
) -> float | Series:
...

def quantile(
self,
q: float | Sequence[float] | AnyArrayLike = 0.5,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, I wouldn't need this overload to enable reportGeneralTypingIssues for pandas/core/describe.py - would only need float | Sequence[float]. In the absence of annotations, pyright assumes the default argument's type to be the annotation (in this case float).

interpolation: QuantileInterpolation = "linear",
) -> float | Series:
"""
Return value at the given quantile.

Expand Down
13 changes: 9 additions & 4 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
FilePath,
IndexLabel,
Level,
QuantileInterpolation,
Scalar,
WriteBuffer,
)
Expand Down Expand Up @@ -3467,7 +3468,7 @@ def highlight_quantile(
axis: Axis | None = 0,
q_left: float = 0.0,
q_right: float = 1.0,
interpolation: str = "linear",
interpolation: QuantileInterpolation = "linear",
inclusive: str = "both",
props: str | None = None,
) -> Styler:
Expand Down Expand Up @@ -3539,13 +3540,17 @@ def highlight_quantile(

# after quantile is found along axis, e.g. along rows,
# applying the calculated quantile to alternate axis, e.g. to each column
kwargs = {"q": [q_left, q_right], "interpolation": interpolation}
quantiles = [q_left, q_right]
if axis is None:
q = Series(data.to_numpy().ravel()).quantile(**kwargs)
q = Series(data.to_numpy().ravel()).quantile(
q=quantiles, interpolation=interpolation
)
axis_apply: int | None = None
else:
axis = self.data._get_axis_number(axis)
q = data.quantile(axis=axis, numeric_only=False, **kwargs)
q = data.quantile(
axis=axis, numeric_only=False, q=quantiles, interpolation=interpolation
)
axis_apply = 1 - axis

if props is None:
Expand Down
1 change: 0 additions & 1 deletion pyright_reportGeneralTypeIssues.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"pandas/core/common.py",
"pandas/core/computation/align.py",
"pandas/core/construction.py",
"pandas/core/describe.py",
"pandas/core/dtypes/base.py",
"pandas/core/dtypes/cast.py",
"pandas/core/dtypes/common.py",
Expand Down