Skip to content

Commit 830130a

Browse files
authored
TYP: Series.quantile (#47304)
* TYP: Series.quantile * common.py
1 parent 032c590 commit 830130a

File tree

5 files changed

+49
-20
lines changed

5 files changed

+49
-20
lines changed

pandas/_typing.py

+3
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,6 @@ def closed(self) -> bool:
323323
# sort_index
324324
SortKind = Literal["quicksort", "mergesort", "heapsort", "stable"]
325325
NaPosition = Literal["first", "last"]
326+
327+
# quantile interpolation
328+
QuantileInterpolation = Literal["linear", "lower", "higher", "midpoint", "nearest"]

pandas/core/common.py

+9-13
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,11 @@ def is_bool_indexer(key: Any) -> bool:
125125
is_array_like(key) and is_extension_array_dtype(key.dtype)
126126
):
127127
if key.dtype == np.object_:
128-
key = np.asarray(key)
128+
key_array = np.asarray(key)
129129

130-
if not lib.is_bool_array(key):
130+
if not lib.is_bool_array(key_array):
131131
na_msg = "Cannot mask with non-boolean array containing NA / NaN values"
132-
if lib.infer_dtype(key) == "boolean" and isna(key).any():
132+
if lib.infer_dtype(key_array) == "boolean" and isna(key_array).any():
133133
# Don't raise on e.g. ["A", "B", np.nan], see
134134
# test_loc_getitem_list_of_labels_categoricalindex_with_na
135135
raise ValueError(na_msg)
@@ -508,18 +508,14 @@ def get_rename_function(mapper):
508508
Returns a function that will map names/labels, dependent if mapper
509509
is a dict, Series or just a function.
510510
"""
511-
if isinstance(mapper, (abc.Mapping, ABCSeries)):
512511

513-
def f(x):
514-
if x in mapper:
515-
return mapper[x]
516-
else:
517-
return x
518-
519-
else:
520-
f = mapper
512+
def f(x):
513+
if x in mapper:
514+
return mapper[x]
515+
else:
516+
return x
521517

522-
return f
518+
return f if isinstance(mapper, (abc.Mapping, ABCSeries)) else mapper
523519

524520

525521
def convert_to_list_like(

pandas/core/series.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
IndexKeyFunc,
4444
Level,
4545
NaPosition,
46+
QuantileInterpolation,
4647
Renamer,
4748
SingleManager,
4849
SortKind,
@@ -2479,7 +2480,33 @@ def round(self, decimals=0, *args, **kwargs) -> Series:
24792480

24802481
return result
24812482

2482-
def quantile(self, q=0.5, interpolation="linear"):
2483+
@overload
2484+
def quantile(
2485+
self, q: float = ..., interpolation: QuantileInterpolation = ...
2486+
) -> float:
2487+
...
2488+
2489+
@overload
2490+
def quantile(
2491+
self,
2492+
q: Sequence[float] | AnyArrayLike,
2493+
interpolation: QuantileInterpolation = ...,
2494+
) -> Series:
2495+
...
2496+
2497+
@overload
2498+
def quantile(
2499+
self,
2500+
q: float | Sequence[float] | AnyArrayLike = ...,
2501+
interpolation: QuantileInterpolation = ...,
2502+
) -> float | Series:
2503+
...
2504+
2505+
def quantile(
2506+
self,
2507+
q: float | Sequence[float] | AnyArrayLike = 0.5,
2508+
interpolation: QuantileInterpolation = "linear",
2509+
) -> float | Series:
24832510
"""
24842511
Return value at the given quantile.
24852512

pandas/io/formats/style.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
FilePath,
2626
IndexLabel,
2727
Level,
28+
QuantileInterpolation,
2829
Scalar,
2930
WriteBuffer,
3031
)
@@ -3467,7 +3468,7 @@ def highlight_quantile(
34673468
axis: Axis | None = 0,
34683469
q_left: float = 0.0,
34693470
q_right: float = 1.0,
3470-
interpolation: str = "linear",
3471+
interpolation: QuantileInterpolation = "linear",
34713472
inclusive: str = "both",
34723473
props: str | None = None,
34733474
) -> Styler:
@@ -3539,13 +3540,17 @@ def highlight_quantile(
35393540

35403541
# after quantile is found along axis, e.g. along rows,
35413542
# applying the calculated quantile to alternate axis, e.g. to each column
3542-
kwargs = {"q": [q_left, q_right], "interpolation": interpolation}
3543+
quantiles = [q_left, q_right]
35433544
if axis is None:
3544-
q = Series(data.to_numpy().ravel()).quantile(**kwargs)
3545+
q = Series(data.to_numpy().ravel()).quantile(
3546+
q=quantiles, interpolation=interpolation
3547+
)
35453548
axis_apply: int | None = None
35463549
else:
35473550
axis = self.data._get_axis_number(axis)
3548-
q = data.quantile(axis=axis, numeric_only=False, **kwargs)
3551+
q = data.quantile(
3552+
axis=axis, numeric_only=False, q=quantiles, interpolation=interpolation
3553+
)
35493554
axis_apply = 1 - axis
35503555

35513556
if props is None:

pyright_reportGeneralTypeIssues.json

-2
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@
3535
"pandas/core/arrays/string_.py",
3636
"pandas/core/arrays/string_arrow.py",
3737
"pandas/core/arrays/timedeltas.py",
38-
"pandas/core/common.py",
3938
"pandas/core/computation/align.py",
4039
"pandas/core/construction.py",
41-
"pandas/core/describe.py",
4240
"pandas/core/dtypes/cast.py",
4341
"pandas/core/dtypes/common.py",
4442
"pandas/core/dtypes/concat.py",

0 commit comments

Comments
 (0)