@@ -224,22 +224,29 @@ def __call__(self, x, pos: int | None = 0) -> str:
224
224
225
225
class PeriodConverter (mdates .DateConverter ):
226
226
@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
+
228
234
if not hasattr (axis , "freq" ):
229
235
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 )
231
238
232
239
@staticmethod
233
- def convert_from_freq (values , freq ):
240
+ def convert_from_freq (values , freq : BaseOffset ):
234
241
if is_nested_list_like (values ):
235
242
values = [PeriodConverter ._convert_1d (v , freq ) for v in values ]
236
243
else :
237
244
values = PeriodConverter ._convert_1d (values , freq )
238
245
return values
239
246
240
247
@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 )
243
250
with warnings .catch_warnings ():
244
251
warnings .filterwarnings (
245
252
"ignore" , "Period with BDay freq is deprecated" , category = FutureWarning
@@ -252,30 +259,26 @@ def _convert_1d(values, freq):
252
259
or is_integer (values )
253
260
or is_float (values )
254
261
):
255
- return get_datevalue (values , freq )
262
+ return _get_datevalue (values , freq )
256
263
elif isinstance (values , PeriodIndex ):
257
264
return values .asfreq (freq ).asi8
258
265
elif isinstance (values , Index ):
259
- return values .map (lambda x : get_datevalue (x , freq ))
266
+ return values .map (lambda x : _get_datevalue (x , freq ))
260
267
elif lib .infer_dtype (values , skipna = False ) == "period" :
261
268
# https://github.com/pandas-dev/pandas/issues/24304
262
269
# convert ndarray[period] -> PeriodIndex
263
270
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 ]
266
273
return values
267
274
268
275
269
- def get_datevalue (date , freq ):
276
+ def _get_datevalue (date , freq : BaseOffset ):
270
277
if isinstance (date , Period ):
271
278
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 )):
273
280
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 ):
279
282
return date
280
283
elif date is None :
281
284
return None
@@ -285,7 +288,13 @@ def get_datevalue(date, freq):
285
288
# Datetime Conversion
286
289
class DatetimeConverter (mdates .DateConverter ):
287
290
@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
+
289
298
# values might be a 1-d array, or a list-like of arrays.
290
299
if is_nested_list_like (values ):
291
300
values = [DatetimeConverter ._convert_1d (v , unit , axis ) for v in values ]
0 commit comments