MCPcopy
hub / github.com/pandas-dev/pandas / maybe_convert_index

Function maybe_convert_index

pandas/plotting/_matplotlib/timeseries.py:266–294  ·  view source on GitHub ↗
(ax: Axes, data: NDFrameT)

Source from the content-addressed store, hash-verified

264
265
266def maybe_convert_index(ax: Axes, data: NDFrameT) -> NDFrameT:
267 # tsplot converts automatically, but don't want to convert index
268 # over and over for DataFrames
269 if isinstance(data.index, (ABCDatetimeIndex, ABCPeriodIndex)):
270 freq = _get_index_freq(data.index)
271
272 if freq is None:
273 freq = _get_ax_freq(ax)
274
275 if freq is None:
276 raise ValueError("Could not get frequency alias for plotting")
277
278 freq_str = _get_period_alias(freq)
279
280 with warnings.catch_warnings():
281 # suppress Period[B] deprecation warning
282 # TODO: need to find an alternative to this before the deprecation
283 # is enforced!
284 warnings.filterwarnings(
285 "ignore",
286 r"PeriodDtype\[B\] is deprecated",
287 category=FutureWarning,
288 )
289
290 if isinstance(data.index, ABCDatetimeIndex):
291 data = data.tz_localize(None).to_period(freq=freq_str)
292 elif isinstance(data.index, ABCPeriodIndex):
293 data.index = data.index.asfreq(freq=freq_str, how="start")
294 return data
295
296
297# Patch methods for subplot.

Callers 2

_make_plotMethod · 0.90
_make_plotMethod · 0.90

Calls 6

_get_index_freqFunction · 0.85
_get_ax_freqFunction · 0.85
_get_period_aliasFunction · 0.85
to_periodMethod · 0.45
tz_localizeMethod · 0.45
asfreqMethod · 0.45

Tested by

no test coverage detected