Skip to content

TYP: annotations in pandas.plotting #35935

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 18 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
4c5eddd
REF: remove unnecesary try/except
jbrockmendel Aug 21, 2020
c632c9f
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Aug 21, 2020
9e64be3
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Aug 21, 2020
42649fb
TST: add test for agg on ordered categorical cols (#35630)
mathurk1 Aug 21, 2020
47121dd
TST: resample does not yield empty groups (#10603) (#35799)
tkmz-n Aug 21, 2020
1decb3e
revert accidental rebase
jbrockmendel Aug 22, 2020
57c5dd3
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 22, 2020
a358463
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 23, 2020
ffa7ad7
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 23, 2020
e5e98d4
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 24, 2020
408db5a
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 24, 2020
d3493cf
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 25, 2020
75a805a
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 25, 2020
9f61070
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 25, 2020
2d10f6e
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 26, 2020
3e20187
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 26, 2020
e27d07f
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 27, 2020
6d5ef6f
TYP: typing for pandas.plotting
jbrockmendel Aug 27, 2020
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
9 changes: 5 additions & 4 deletions pandas/plotting/_matplotlib/converter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import contextlib
import datetime as pydt
from datetime import datetime, timedelta
from datetime import datetime, timedelta, tzinfo
import functools
from typing import Optional, Tuple

from dateutil.relativedelta import relativedelta
import matplotlib.dates as dates
Expand Down Expand Up @@ -152,7 +153,7 @@ def axisinfo(unit, axis):
return units.AxisInfo(majloc=majloc, majfmt=majfmt, label="time")

@staticmethod
def default_units(x, axis):
def default_units(x, axis) -> str:
return "time"


Expand Down Expand Up @@ -421,7 +422,7 @@ def autoscale(self):
return self.nonsingular(vmin, vmax)


def _from_ordinal(x, tz=None):
def _from_ordinal(x, tz: Optional[tzinfo] = None) -> datetime:
ix = int(x)
dt = datetime.fromordinal(ix)
remainder = float(x) - ix
Expand Down Expand Up @@ -450,7 +451,7 @@ def _from_ordinal(x, tz=None):
# -------------------------------------------------------------------------


def _get_default_annual_spacing(nyears):
def _get_default_annual_spacing(nyears) -> Tuple[int, int]:
"""
Returns a default spacing between consecutive ticks for annual data.
"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/plotting/_matplotlib/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ def _maybe_resample(series: "Series", ax, kwargs):
return freq, series


def _is_sub(f1, f2):
def _is_sub(f1: str, f2: str) -> bool:
return (f1.startswith("W") and is_subperiod("D", f2)) or (
f2.startswith("W") and is_subperiod(f1, "D")
)


def _is_sup(f1, f2):
def _is_sup(f1: str, f2: str) -> bool:
return (f1.startswith("W") and is_superperiod("D", f2)) or (
f2.startswith("W") and is_superperiod(f1, "D")
)
Expand Down
22 changes: 14 additions & 8 deletions pandas/plotting/_matplotlib/tools.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
# being a bit too dynamic
from math import ceil
from typing import TYPE_CHECKING, Tuple
import warnings

import matplotlib.table
import matplotlib.ticker as ticker
import numpy as np

from pandas._typing import FrameOrSeries

from pandas.core.dtypes.common import is_list_like
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries

from pandas.plotting._matplotlib import compat

if TYPE_CHECKING:
from matplotlib.table import Table
Copy link
Member

Choose a reason for hiding this comment

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

Revealed type is 'Any'

not sure what's best here. I imagine that once we get np.ndarray imports sorted, there'll be a lot of mypy errors to fix. I wonder whether we want to continue adding types that currently resolve to Any or leave untyped.

Copy link
Member Author

Choose a reason for hiding this comment

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

ill follow your lead on this.

there are a lot of places in pd.plotting where we have a ax arg that would be nice to disambiguate pd.Index vs matplotlib.axes.Axes vs matplotlib.axis.Axis (i think most are Axes)

Copy link
Member Author

Choose a reason for hiding this comment

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

once we get np.ndarray imports sorted

is there cause for optimism this may be soon-ish?

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure that we have got to any technical blocks yet since not much effort in this direction. Personally, I wanted to get the check_untyped_defs sorted first. (was a phase 0 in a plan) that imo comes before all other work. but in open source can't control the effort and therefore some have a preference for typing the public api. My interest in typing has always been for the internal consistency and quality of the pandas codebase itself.

But with hindsight, getting typing PRs through was so painfull that any typing additions is a bonus.



def format_date_labels(ax, rot):
# mini version of autofmt_xdate
Expand All @@ -21,7 +27,7 @@ def format_date_labels(ax, rot):
fig.subplots_adjust(bottom=0.2)


def table(ax, data, rowLabels=None, colLabels=None, **kwargs):
def table(ax, data: FrameOrSeries, rowLabels=None, colLabels=None, **kwargs) -> "Table":
if isinstance(data, ABCSeries):
data = data.to_frame()
elif isinstance(data, ABCDataFrame):
Expand All @@ -43,7 +49,7 @@ def table(ax, data, rowLabels=None, colLabels=None, **kwargs):
return table


def _get_layout(nplots, layout=None, layout_type="box"):
def _get_layout(nplots: int, layout=None, layout_type: str = "box") -> Tuple[int, int]:
if layout is not None:
if not isinstance(layout, (tuple, list)) or len(layout) != 2:
raise ValueError("Layout must be a tuple of (rows, columns)")
Expand Down Expand Up @@ -92,14 +98,14 @@ def _get_layout(nplots, layout=None, layout_type="box"):


def _subplots(
naxes=None,
sharex=False,
sharey=False,
squeeze=True,
naxes: int,
sharex: bool = False,
sharey: bool = False,
squeeze: bool = True,
subplot_kw=None,
ax=None,
layout=None,
layout_type="box",
layout_type: str = "box",
**fig_kw,
):
"""
Expand Down Expand Up @@ -369,7 +375,7 @@ def _get_all_lines(ax):
return lines


def _get_xlim(lines):
def _get_xlim(lines) -> Tuple[float, float]:
left, right = np.inf, -np.inf
for l in lines:
x = l.get_xdata(orig=False)
Expand Down