Skip to content

Commit 4679714

Browse files
authored
TYP: annotations in plotting code (#34469)
1 parent fde7ec7 commit 4679714

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

pandas/plotting/_matplotlib/converter.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def deregister():
113113
units.registry[unit] = formatter
114114

115115

116-
def _to_ordinalf(tm):
116+
def _to_ordinalf(tm: pydt.time) -> float:
117117
tot_sec = tm.hour * 3600 + tm.minute * 60 + tm.second + float(tm.microsecond / 1e6)
118118
return tot_sec
119119

@@ -160,7 +160,7 @@ class TimeFormatter(Formatter):
160160
def __init__(self, locs):
161161
self.locs = locs
162162

163-
def __call__(self, x, pos=0):
163+
def __call__(self, x, pos=0) -> str:
164164
"""
165165
Return the time of day as a formatted string.
166166
@@ -1049,7 +1049,7 @@ def set_locs(self, locs):
10491049
(vmin, vmax) = (vmax, vmin)
10501050
self._set_default_format(vmin, vmax)
10511051

1052-
def __call__(self, x, pos=0):
1052+
def __call__(self, x, pos=0) -> str:
10531053

10541054
if self.formatdict is None:
10551055
return ""
@@ -1066,7 +1066,7 @@ class TimeSeries_TimedeltaFormatter(Formatter):
10661066
"""
10671067

10681068
@staticmethod
1069-
def format_timedelta_ticks(x, pos, n_decimals):
1069+
def format_timedelta_ticks(x, pos, n_decimals: int) -> str:
10701070
"""
10711071
Convert seconds to 'D days HH:MM:SS.F'
10721072
"""
@@ -1082,7 +1082,7 @@ def format_timedelta_ticks(x, pos, n_decimals):
10821082
s = f"{int(d):d} days {s}"
10831083
return s
10841084

1085-
def __call__(self, x, pos=0):
1085+
def __call__(self, x, pos=0) -> str:
10861086
(vmin, vmax) = tuple(self.axis.get_view_interval())
10871087
n_decimals = int(np.ceil(np.log10(100 * 1e9 / (vmax - vmin))))
10881088
if n_decimals > 9:

pandas/plotting/_matplotlib/timeseries.py

+31-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# TODO: Use the fact that axis can have units to simplify the process
22

33
import functools
4-
from typing import Optional
4+
from typing import TYPE_CHECKING, Optional
55

66
import numpy as np
77

@@ -20,15 +20,23 @@
2020
TimeSeries_DateLocator,
2121
TimeSeries_TimedeltaFormatter,
2222
)
23-
import pandas.tseries.frequencies as frequencies
24-
from pandas.tseries.frequencies import is_subperiod, is_superperiod
23+
from pandas.tseries.frequencies import (
24+
get_period_alias,
25+
is_subperiod,
26+
is_superperiod,
27+
to_offset,
28+
)
2529
from pandas.tseries.offsets import DateOffset
2630

31+
if TYPE_CHECKING:
32+
from pandas import Series, Index # noqa:F401
33+
34+
2735
# ---------------------------------------------------------------------
2836
# Plotting functions and monkey patches
2937

3038

31-
def _maybe_resample(series, ax, kwargs):
39+
def _maybe_resample(series: "Series", ax, kwargs):
3240
# resample against axes freq if necessary
3341
freq, ax_freq = _get_freq(ax, series)
3442

@@ -42,7 +50,7 @@ def _maybe_resample(series, ax, kwargs):
4250
if ax_freq is not None and freq != ax_freq:
4351
if is_superperiod(freq, ax_freq): # upsample input
4452
series = series.copy()
45-
series.index = series.index.asfreq(ax_freq, how="s")
53+
series.index = series.index.asfreq(ax_freq, how="s") # type: ignore
4654
freq = ax_freq
4755
elif _is_sup(freq, ax_freq): # one is weekly
4856
how = kwargs.pop("how", "last")
@@ -161,21 +169,22 @@ def _get_ax_freq(ax):
161169
return ax_freq
162170

163171

164-
def get_period_alias(freq) -> Optional[str]:
172+
def _get_period_alias(freq) -> Optional[str]:
165173
if isinstance(freq, DateOffset):
166174
freq = freq.rule_code
167175
else:
168176
freq = base_and_stride(freq)[0]
169177

170-
freq = frequencies.get_period_alias(freq)
178+
freq = get_period_alias(freq)
171179
return freq
172180

173181

174-
def _get_freq(ax, series):
182+
def _get_freq(ax, series: "Series"):
175183
# get frequency from data
176184
freq = getattr(series.index, "freq", None)
177185
if freq is None:
178186
freq = getattr(series.index, "inferred_freq", None)
187+
freq = to_offset(freq)
179188

180189
ax_freq = _get_ax_freq(ax)
181190

@@ -184,12 +193,12 @@ def _get_freq(ax, series):
184193
freq = ax_freq
185194

186195
# get the period frequency
187-
freq = get_period_alias(freq)
196+
freq = _get_period_alias(freq)
188197
return freq, ax_freq
189198

190199

191200
def _use_dynamic_x(ax, data):
192-
freq = _get_index_freq(data)
201+
freq = _get_index_freq(data.index)
193202
ax_freq = _get_ax_freq(ax)
194203

195204
if freq is None: # convert irregular if axes has freq info
@@ -201,7 +210,7 @@ def _use_dynamic_x(ax, data):
201210
if freq is None:
202211
return False
203212

204-
freq = get_period_alias(freq)
213+
freq = _get_period_alias(freq)
205214

206215
if freq is None:
207216
return False
@@ -216,33 +225,37 @@ def _use_dynamic_x(ax, data):
216225
return True
217226

218227

219-
def _get_index_freq(data):
220-
freq = getattr(data.index, "freq", None)
228+
def _get_index_freq(index: "Index") -> Optional[DateOffset]:
229+
freq = getattr(index, "freq", None)
221230
if freq is None:
222-
freq = getattr(data.index, "inferred_freq", None)
231+
freq = getattr(index, "inferred_freq", None)
223232
if freq == "B":
224-
weekdays = np.unique(data.index.dayofweek)
233+
weekdays = np.unique(index.dayofweek) # type: ignore
225234
if (5 in weekdays) or (6 in weekdays):
226235
freq = None
236+
237+
freq = to_offset(freq)
227238
return freq
228239

229240

230241
def _maybe_convert_index(ax, data):
231242
# tsplot converts automatically, but don't want to convert index
232243
# over and over for DataFrames
233244
if isinstance(data.index, (ABCDatetimeIndex, ABCPeriodIndex)):
234-
freq = getattr(data.index, "freq", None)
245+
freq = data.index.freq
235246

236247
if freq is None:
237-
freq = getattr(data.index, "inferred_freq", None)
248+
# We only get here for DatetimeIndex
249+
freq = data.index.inferred_freq
250+
freq = to_offset(freq)
238251

239252
if freq is None:
240253
freq = _get_ax_freq(ax)
241254

242255
if freq is None:
243256
raise ValueError("Could not get frequency alias for plotting")
244257

245-
freq = get_period_alias(freq)
258+
freq = _get_period_alias(freq)
246259

247260
if isinstance(data.index, ABCDatetimeIndex):
248261
data = data.tz_localize(None).to_period(freq=freq)

0 commit comments

Comments
 (0)