Skip to content

Commit a072875

Browse files
authored
CLN: plotting code (#62431)
1 parent 0f9647e commit a072875

File tree

3 files changed

+34
-39
lines changed

3 files changed

+34
-39
lines changed

pandas/plotting/_matplotlib/converter.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -224,22 +224,29 @@ def __call__(self, x, pos: int | None = 0) -> str:
224224

225225
class PeriodConverter(mdates.DateConverter):
226226
@staticmethod
227-
def convert(values, units, axis):
227+
def convert(values, unit, axis: Axis):
228+
# Reached via e.g. `ax.set_xlim`
229+
230+
# In tests as of 2025-09-24, unit is always None except for 3 tests
231+
# that directly call this with unit="";
232+
# axis is always specifically a matplotlib.axis.XAxis
233+
228234
if not hasattr(axis, "freq"):
229235
raise TypeError("Axis must have `freq` set to convert to Periods")
230-
return PeriodConverter.convert_from_freq(values, axis.freq)
236+
freq = to_offset(axis.freq, is_period=True)
237+
return PeriodConverter.convert_from_freq(values, freq)
231238

232239
@staticmethod
233-
def convert_from_freq(values, freq):
240+
def convert_from_freq(values, freq: BaseOffset):
234241
if is_nested_list_like(values):
235242
values = [PeriodConverter._convert_1d(v, freq) for v in values]
236243
else:
237244
values = PeriodConverter._convert_1d(values, freq)
238245
return values
239246

240247
@staticmethod
241-
def _convert_1d(values, freq):
242-
valid_types = (str, datetime, Period, pydt.date, pydt.time, np.datetime64)
248+
def _convert_1d(values, freq: BaseOffset):
249+
valid_types = (str, datetime, Period, pydt.date, np.datetime64)
243250
with warnings.catch_warnings():
244251
warnings.filterwarnings(
245252
"ignore", "Period with BDay freq is deprecated", category=FutureWarning
@@ -252,30 +259,26 @@ def _convert_1d(values, freq):
252259
or is_integer(values)
253260
or is_float(values)
254261
):
255-
return get_datevalue(values, freq)
262+
return _get_datevalue(values, freq)
256263
elif isinstance(values, PeriodIndex):
257264
return values.asfreq(freq).asi8
258265
elif isinstance(values, Index):
259-
return values.map(lambda x: get_datevalue(x, freq))
266+
return values.map(lambda x: _get_datevalue(x, freq))
260267
elif lib.infer_dtype(values, skipna=False) == "period":
261268
# https://github.com/pandas-dev/pandas/issues/24304
262269
# convert ndarray[period] -> PeriodIndex
263270
return PeriodIndex(values, freq=freq).asi8
264-
elif isinstance(values, (list, tuple, np.ndarray, Index)):
265-
return [get_datevalue(x, freq) for x in values]
271+
elif isinstance(values, (list, tuple, np.ndarray)):
272+
return [_get_datevalue(x, freq) for x in values]
266273
return values
267274

268275

269-
def get_datevalue(date, freq):
276+
def _get_datevalue(date, freq: BaseOffset):
270277
if isinstance(date, Period):
271278
return date.asfreq(freq).ordinal
272-
elif isinstance(date, (str, datetime, pydt.date, pydt.time, np.datetime64)):
279+
elif isinstance(date, (str, datetime, pydt.date, np.datetime64)):
273280
return Period(date, freq).ordinal
274-
elif (
275-
is_integer(date)
276-
or is_float(date)
277-
or (isinstance(date, (np.ndarray, Index)) and (date.size == 1))
278-
):
281+
elif is_integer(date) or is_float(date):
279282
return date
280283
elif date is None:
281284
return None
@@ -285,7 +288,13 @@ def get_datevalue(date, freq):
285288
# Datetime Conversion
286289
class DatetimeConverter(mdates.DateConverter):
287290
@staticmethod
288-
def convert(values, unit, axis):
291+
def convert(values, unit, axis: Axis):
292+
# Reached via e.g. `ax.set_xlim`
293+
294+
# In tests as of 2025-09-24, unit is always None except for 3 tests
295+
# that directly call this with unit="";
296+
# axis is always specifically a matplotlib.axis.XAxis
297+
289298
# values might be a 1-d array, or a list-like of arrays.
290299
if is_nested_list_like(values):
291300
values = [DatetimeConverter._convert_1d(v, unit, axis) for v in values]

pandas/plotting/_matplotlib/timeseries.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
from typing import (
77
TYPE_CHECKING,
88
Any,
9-
cast,
109
)
1110
import warnings
1211

13-
import numpy as np
14-
1512
from pandas._libs.tslibs import (
1613
BaseOffset,
1714
Period,
@@ -265,27 +262,15 @@ def _get_index_freq(index: Index) -> BaseOffset | None:
265262
freq = getattr(index, "freq", None)
266263
if freq is None:
267264
freq = getattr(index, "inferred_freq", None)
268-
if freq == "B":
269-
# error: "Index" has no attribute "dayofweek"
270-
weekdays = np.unique(index.dayofweek) # type: ignore[attr-defined]
271-
if (5 in weekdays) or (6 in weekdays):
272-
freq = None
273-
274-
freq = to_offset(freq)
265+
freq = to_offset(freq)
275266
return freq
276267

277268

278269
def maybe_convert_index(ax: Axes, data: NDFrameT) -> NDFrameT:
279270
# tsplot converts automatically, but don't want to convert index
280271
# over and over for DataFrames
281272
if isinstance(data.index, (ABCDatetimeIndex, ABCPeriodIndex)):
282-
freq: str | BaseOffset | None = data.index.freq
283-
284-
if freq is None:
285-
# We only get here for DatetimeIndex
286-
data.index = cast("DatetimeIndex", data.index)
287-
freq = data.index.inferred_freq
288-
freq = to_offset(freq)
273+
freq = _get_index_freq(data.index)
289274

290275
if freq is None:
291276
freq = _get_ax_freq(ax)
@@ -315,7 +300,7 @@ def maybe_convert_index(ax: Axes, data: NDFrameT) -> NDFrameT:
315300
# Patch methods for subplot.
316301

317302

318-
def _format_coord(freq, t, y) -> str:
303+
def _format_coord(freq: BaseOffset, t, y) -> str:
319304
time_period = Period(ordinal=int(t), freq=freq)
320305
return f"t = {time_period} y = {y:8f}"
321306

pandas/tests/plotting/test_datetimelike.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,13 @@ def test_high_freq(self, freq):
165165
_check_plot_works(ser.plot, ax=ax)
166166

167167
def test_get_datevalue(self):
168-
assert conv.get_datevalue(None, "D") is None
169-
assert conv.get_datevalue(1987, "Y") == 1987
168+
assert conv._get_datevalue(None, "D") is None
169+
assert conv._get_datevalue(1987, "Y") == 1987
170170
assert (
171-
conv.get_datevalue(Period(1987, "Y"), "M") == Period("1987-12", "M").ordinal
171+
conv._get_datevalue(Period(1987, "Y"), "M")
172+
== Period("1987-12", "M").ordinal
172173
)
173-
assert conv.get_datevalue("1/1/1987", "D") == Period("1987-1-1", "D").ordinal
174+
assert conv._get_datevalue("1/1/1987", "D") == Period("1987-1-1", "D").ordinal
174175

175176
@pytest.mark.parametrize(
176177
"freq, expected_string",

0 commit comments

Comments
 (0)