Skip to content

Commit 4d7cfc4

Browse files
authored
TYP: pandas/plotting annotations from pandas-stubs (#47827)
* TYP: pandas/plotting annotations from pandas-stubs * xticks + pyright
1 parent cedd9c7 commit 4d7cfc4

File tree

11 files changed

+148
-109
lines changed

11 files changed

+148
-109
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ repos:
9393
types: [python]
9494
stages: [manual]
9595
additional_dependencies: &pyright_dependencies
96-
96+
9797
- id: pyright_reportGeneralTypeIssues
9898
name: pyright reportGeneralTypeIssues
9999
entry: pyright --skipunannotated -p pyright_reportGeneralTypeIssues.json

pandas/core/arrays/arrow/array.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ def dropna(self: ArrowExtensionArrayT) -> ArrowExtensionArrayT:
444444
else:
445445
return type(self)(pc.drop_null(self._data))
446446

447-
def isin(self: ArrowExtensionArrayT, values) -> npt.NDArray[np.bool_]:
447+
def isin(self, values) -> npt.NDArray[np.bool_]:
448448
if pa_version_under2p0:
449449
fallback_performancewarning(version="2")
450450
return super().isin(values)

pandas/plotting/_core.py

+31-27
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
from pandas.core.base import PandasObject
2828

2929
if TYPE_CHECKING:
30+
from matplotlib.axes import Axes
31+
3032
from pandas import DataFrame
3133

3234

@@ -463,16 +465,16 @@ def hist_frame(
463465
@Substitution(backend="")
464466
@Appender(_boxplot_doc)
465467
def boxplot(
466-
data,
467-
column=None,
468-
by=None,
469-
ax=None,
470-
fontsize=None,
471-
rot=0,
472-
grid=True,
473-
figsize=None,
474-
layout=None,
475-
return_type=None,
468+
data: DataFrame,
469+
column: str | list[str] | None = None,
470+
by: str | list[str] | None = None,
471+
ax: Axes | None = None,
472+
fontsize: float | str | None = None,
473+
rot: int = 0,
474+
grid: bool = True,
475+
figsize: tuple[float, float] | None = None,
476+
layout: tuple[int, int] | None = None,
477+
return_type: str | None = None,
476478
**kwargs,
477479
):
478480
plot_backend = _get_plot_backend("matplotlib")
@@ -499,8 +501,8 @@ def boxplot_frame(
499501
by=None,
500502
ax=None,
501503
fontsize=None,
502-
rot=0,
503-
grid=True,
504+
rot: int = 0,
505+
grid: bool = True,
504506
figsize=None,
505507
layout=None,
506508
return_type=None,
@@ -525,16 +527,16 @@ def boxplot_frame(
525527

526528
def boxplot_frame_groupby(
527529
grouped,
528-
subplots=True,
530+
subplots: bool = True,
529531
column=None,
530532
fontsize=None,
531-
rot=0,
532-
grid=True,
533+
rot: int = 0,
534+
grid: bool = True,
533535
ax=None,
534536
figsize=None,
535537
layout=None,
536-
sharex=False,
537-
sharey=True,
538+
sharex: bool = False,
539+
sharey: bool = True,
538540
backend=None,
539541
**kwargs,
540542
):
@@ -1041,7 +1043,7 @@ def __call__(self, *args, **kwargs):
10411043
)
10421044
@Substitution(kind="line")
10431045
@Appender(_bar_or_line_doc)
1044-
def line(self, x=None, y=None, **kwargs):
1046+
def line(self, x=None, y=None, **kwargs) -> PlotAccessor:
10451047
"""
10461048
Plot Series or DataFrame as lines.
10471049
@@ -1128,7 +1130,7 @@ def line(self, x=None, y=None, **kwargs):
11281130
)
11291131
@Substitution(kind="bar")
11301132
@Appender(_bar_or_line_doc)
1131-
def bar(self, x=None, y=None, **kwargs):
1133+
def bar(self, x=None, y=None, **kwargs) -> PlotAccessor:
11321134
"""
11331135
Vertical bar plot.
11341136
@@ -1214,7 +1216,7 @@ def bar(self, x=None, y=None, **kwargs):
12141216
)
12151217
@Substitution(kind="bar")
12161218
@Appender(_bar_or_line_doc)
1217-
def barh(self, x=None, y=None, **kwargs):
1219+
def barh(self, x=None, y=None, **kwargs) -> PlotAccessor:
12181220
"""
12191221
Make a horizontal bar plot.
12201222
@@ -1226,7 +1228,7 @@ def barh(self, x=None, y=None, **kwargs):
12261228
"""
12271229
return self(kind="barh", x=x, y=y, **kwargs)
12281230

1229-
def box(self, by=None, **kwargs):
1231+
def box(self, by=None, **kwargs) -> PlotAccessor:
12301232
r"""
12311233
Make a box plot of the DataFrame columns.
12321234
@@ -1293,7 +1295,7 @@ def box(self, by=None, **kwargs):
12931295
"""
12941296
return self(kind="box", by=by, **kwargs)
12951297

1296-
def hist(self, by=None, bins=10, **kwargs):
1298+
def hist(self, by=None, bins: int = 10, **kwargs) -> PlotAccessor:
12971299
"""
12981300
Draw one histogram of the DataFrame's columns.
12991301
@@ -1355,7 +1357,7 @@ def hist(self, by=None, bins=10, **kwargs):
13551357
"""
13561358
return self(kind="hist", by=by, bins=bins, **kwargs)
13571359

1358-
def kde(self, bw_method=None, ind=None, **kwargs):
1360+
def kde(self, bw_method=None, ind=None, **kwargs) -> PlotAccessor:
13591361
"""
13601362
Generate Kernel Density Estimate plot using Gaussian kernels.
13611363
@@ -1465,7 +1467,7 @@ def kde(self, bw_method=None, ind=None, **kwargs):
14651467

14661468
density = kde
14671469

1468-
def area(self, x=None, y=None, **kwargs):
1470+
def area(self, x=None, y=None, **kwargs) -> PlotAccessor:
14691471
"""
14701472
Draw a stacked area plot.
14711473
@@ -1538,7 +1540,7 @@ def area(self, x=None, y=None, **kwargs):
15381540
"""
15391541
return self(kind="area", x=x, y=y, **kwargs)
15401542

1541-
def pie(self, **kwargs):
1543+
def pie(self, **kwargs) -> PlotAccessor:
15421544
"""
15431545
Generate a pie plot.
15441546
@@ -1593,7 +1595,7 @@ def pie(self, **kwargs):
15931595
raise ValueError("pie requires either y column or 'subplots=True'")
15941596
return self(kind="pie", **kwargs)
15951597

1596-
def scatter(self, x, y, s=None, c=None, **kwargs):
1598+
def scatter(self, x, y, s=None, c=None, **kwargs) -> PlotAccessor:
15971599
"""
15981600
Create a scatter plot with varying marker point size and color.
15991601
@@ -1699,7 +1701,9 @@ def scatter(self, x, y, s=None, c=None, **kwargs):
16991701

17001702
return self(kind="scatter", x=x, y=y, **kwargs)
17011703

1702-
def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
1704+
def hexbin(
1705+
self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs
1706+
) -> PlotAccessor:
17031707
"""
17041708
Generate a hexagonal binning plot.
17051709

pandas/plotting/_matplotlib/boxplot.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class BP(NamedTuple):
4848
ax: Axes
4949
lines: dict[str, list[Line2D]]
5050

51-
def __init__(self, data, return_type="axes", **kwargs) -> None:
51+
def __init__(self, data, return_type: str = "axes", **kwargs) -> None:
5252
# Do not call LinePlot.__init__ which may fill nan
5353
if return_type not in self._valid_return_types:
5454
raise ValueError("return_type must be {None, 'axes', 'dict', 'both'}")
@@ -117,7 +117,7 @@ def _validate_color_args(self):
117117
def _get_colors(self, num_colors=None, color_kwds="color"):
118118
pass
119119

120-
def maybe_color_bp(self, bp):
120+
def maybe_color_bp(self, bp) -> None:
121121
if isinstance(self.color, dict):
122122
boxes = self.color.get("boxes", self._boxes_c)
123123
whiskers = self.color.get("whiskers", self._whiskers_c)
@@ -292,8 +292,8 @@ def boxplot(
292292
by=None,
293293
ax=None,
294294
fontsize=None,
295-
rot=0,
296-
grid=True,
295+
rot: int = 0,
296+
grid: bool = True,
297297
figsize=None,
298298
layout=None,
299299
return_type=None,
@@ -443,8 +443,8 @@ def boxplot_frame(
443443
by=None,
444444
ax=None,
445445
fontsize=None,
446-
rot=0,
447-
grid=True,
446+
rot: int = 0,
447+
grid: bool = True,
448448
figsize=None,
449449
layout=None,
450450
return_type=None,
@@ -471,16 +471,16 @@ def boxplot_frame(
471471

472472
def boxplot_frame_groupby(
473473
grouped,
474-
subplots=True,
474+
subplots: bool = True,
475475
column=None,
476476
fontsize=None,
477-
rot=0,
478-
grid=True,
477+
rot: int = 0,
478+
grid: bool = True,
479479
ax=None,
480480
figsize=None,
481481
layout=None,
482-
sharex=False,
483-
sharey=True,
482+
sharex: bool = False,
483+
sharey: bool = True,
484484
**kwds,
485485
):
486486
if subplots is True:

pandas/plotting/_matplotlib/converter.py

+22-20
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import functools
1111
from typing import (
1212
Any,
13+
Final,
14+
Iterator,
1315
cast,
1416
)
1517

@@ -56,14 +58,14 @@
5658
import pandas.core.tools.datetimes as tools
5759

5860
# constants
59-
HOURS_PER_DAY = 24.0
60-
MIN_PER_HOUR = 60.0
61-
SEC_PER_MIN = 60.0
61+
HOURS_PER_DAY: Final = 24.0
62+
MIN_PER_HOUR: Final = 60.0
63+
SEC_PER_MIN: Final = 60.0
6264

63-
SEC_PER_HOUR = SEC_PER_MIN * MIN_PER_HOUR
64-
SEC_PER_DAY = SEC_PER_HOUR * HOURS_PER_DAY
65+
SEC_PER_HOUR: Final = SEC_PER_MIN * MIN_PER_HOUR
66+
SEC_PER_DAY: Final = SEC_PER_HOUR * HOURS_PER_DAY
6567

66-
MUSEC_PER_DAY = 10**6 * SEC_PER_DAY
68+
MUSEC_PER_DAY: Final = 10**6 * SEC_PER_DAY
6769

6870
_mpl_units = {} # Cache for units overwritten by us
6971

@@ -94,7 +96,7 @@ def wrapper(*args, **kwargs):
9496

9597

9698
@contextlib.contextmanager
97-
def pandas_converters():
99+
def pandas_converters() -> Iterator[None]:
98100
"""
99101
Context manager registering pandas' converters for a plot.
100102
@@ -115,7 +117,7 @@ def pandas_converters():
115117
deregister()
116118

117119

118-
def register():
120+
def register() -> None:
119121
pairs = get_pairs()
120122
for type_, cls in pairs:
121123
# Cache previous converter if present
@@ -126,7 +128,7 @@ def register():
126128
units.registry[type_] = cls()
127129

128130

129-
def deregister():
131+
def deregister() -> None:
130132
# Renamed in pandas.plotting.__init__
131133
for type_, cls in get_pairs():
132134
# We use type to catch our classes directly, no inheritance
@@ -187,7 +189,7 @@ class TimeFormatter(Formatter):
187189
def __init__(self, locs) -> None:
188190
self.locs = locs
189191

190-
def __call__(self, x, pos=0) -> str:
192+
def __call__(self, x, pos: int = 0) -> str:
191193
"""
192194
Return the time of day as a formatted string.
193195
@@ -339,7 +341,7 @@ def axisinfo(unit: tzinfo | None, axis) -> units.AxisInfo:
339341

340342

341343
class PandasAutoDateFormatter(dates.AutoDateFormatter):
342-
def __init__(self, locator, tz=None, defaultfmt="%Y-%m-%d") -> None:
344+
def __init__(self, locator, tz=None, defaultfmt: str = "%Y-%m-%d") -> None:
343345
dates.AutoDateFormatter.__init__(self, locator, tz, defaultfmt)
344346

345347

@@ -937,12 +939,12 @@ class TimeSeries_DateLocator(Locator):
937939
def __init__(
938940
self,
939941
freq: BaseOffset,
940-
minor_locator=False,
941-
dynamic_mode=True,
942-
base=1,
943-
quarter=1,
944-
month=1,
945-
day=1,
942+
minor_locator: bool = False,
943+
dynamic_mode: bool = True,
944+
base: int = 1,
945+
quarter: int = 1,
946+
month: int = 1,
947+
day: int = 1,
946948
plot_obj=None,
947949
) -> None:
948950
freq = to_offset(freq)
@@ -1053,7 +1055,7 @@ def _set_default_format(self, vmin, vmax):
10531055
self.formatdict = {x: f for (x, _, _, f) in format}
10541056
return self.formatdict
10551057

1056-
def set_locs(self, locs):
1058+
def set_locs(self, locs) -> None:
10571059
"""Sets the locations of the ticks"""
10581060
# don't actually use the locs. This is just needed to work with
10591061
# matplotlib. Force to use vmin, vmax
@@ -1068,7 +1070,7 @@ def set_locs(self, locs):
10681070
(vmin, vmax) = (vmax, vmin)
10691071
self._set_default_format(vmin, vmax)
10701072

1071-
def __call__(self, x, pos=0) -> str:
1073+
def __call__(self, x, pos: int = 0) -> str:
10721074

10731075
if self.formatdict is None:
10741076
return ""
@@ -1103,7 +1105,7 @@ def format_timedelta_ticks(x, pos, n_decimals: int) -> str:
11031105
s = f"{int(d):d} days {s}"
11041106
return s
11051107

1106-
def __call__(self, x, pos=0) -> str:
1108+
def __call__(self, x, pos: int = 0) -> str:
11071109
(vmin, vmax) = tuple(self.axis.get_view_interval())
11081110
n_decimals = int(np.ceil(np.log10(100 * 10**9 / abs(vmax - vmin))))
11091111
if n_decimals > 9:

0 commit comments

Comments
 (0)