Skip to content

Commit 3d44136

Browse files
authored
REF: ensure we have offset objects in plotting functions (#34585)
1 parent 266b9c0 commit 3d44136

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

pandas/plotting/_matplotlib/converter.py

+26-25
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
import numpy as np
1212

1313
from pandas._libs import lib, tslibs
14-
from pandas._libs.tslibs.frequencies import FreqGroup, get_freq_code, get_freq_group
14+
from pandas._libs.tslibs import to_offset
15+
from pandas._libs.tslibs.frequencies import FreqGroup, get_freq_group
16+
from pandas._libs.tslibs.offsets import BaseOffset
1517

1618
from pandas.core.dtypes.common import (
1719
is_datetime64_ns_dtype,
@@ -522,34 +524,36 @@ def has_level_label(label_flags, vmin):
522524
return True
523525

524526

525-
def _daily_finder(vmin, vmax, freq):
527+
def _daily_finder(vmin, vmax, freq: BaseOffset):
528+
dtype_code = freq._period_dtype_code
529+
526530
periodsperday = -1
527531

528-
if freq >= FreqGroup.FR_HR:
529-
if freq == FreqGroup.FR_NS:
532+
if dtype_code >= FreqGroup.FR_HR:
533+
if dtype_code == FreqGroup.FR_NS:
530534
periodsperday = 24 * 60 * 60 * 1000000000
531-
elif freq == FreqGroup.FR_US:
535+
elif dtype_code == FreqGroup.FR_US:
532536
periodsperday = 24 * 60 * 60 * 1000000
533-
elif freq == FreqGroup.FR_MS:
537+
elif dtype_code == FreqGroup.FR_MS:
534538
periodsperday = 24 * 60 * 60 * 1000
535-
elif freq == FreqGroup.FR_SEC:
539+
elif dtype_code == FreqGroup.FR_SEC:
536540
periodsperday = 24 * 60 * 60
537-
elif freq == FreqGroup.FR_MIN:
541+
elif dtype_code == FreqGroup.FR_MIN:
538542
periodsperday = 24 * 60
539-
elif freq == FreqGroup.FR_HR:
543+
elif dtype_code == FreqGroup.FR_HR:
540544
periodsperday = 24
541545
else: # pragma: no cover
542-
raise ValueError(f"unexpected frequency: {freq}")
546+
raise ValueError(f"unexpected frequency: {dtype_code}")
543547
periodsperyear = 365 * periodsperday
544548
periodspermonth = 28 * periodsperday
545549

546-
elif freq == FreqGroup.FR_BUS:
550+
elif dtype_code == FreqGroup.FR_BUS:
547551
periodsperyear = 261
548552
periodspermonth = 19
549-
elif freq == FreqGroup.FR_DAY:
553+
elif dtype_code == FreqGroup.FR_DAY:
550554
periodsperyear = 365
551555
periodspermonth = 28
552-
elif get_freq_group(freq) == FreqGroup.FR_WK:
556+
elif get_freq_group(dtype_code) == FreqGroup.FR_WK:
553557
periodsperyear = 52
554558
periodspermonth = 3
555559
else: # pragma: no cover
@@ -676,7 +680,7 @@ def _second_finder(label_interval):
676680
elif span <= periodsperyear // 4:
677681
month_start = period_break(dates_, "month")
678682
info_maj[month_start] = True
679-
if freq < FreqGroup.FR_HR:
683+
if dtype_code < FreqGroup.FR_HR:
680684
info["min"] = True
681685
else:
682686
day_start = period_break(dates_, "day")
@@ -884,21 +888,20 @@ def _annual_finder(vmin, vmax, freq):
884888
return info
885889

886890

887-
def get_finder(freq):
888-
if isinstance(freq, str):
889-
freq = get_freq_code(freq)[0]
890-
fgroup = get_freq_group(freq)
891+
def get_finder(freq: BaseOffset):
892+
dtype_code = freq._period_dtype_code
893+
fgroup = (dtype_code // 1000) * 1000
891894

892895
if fgroup == FreqGroup.FR_ANN:
893896
return _annual_finder
894897
elif fgroup == FreqGroup.FR_QTR:
895898
return _quarterly_finder
896-
elif freq == FreqGroup.FR_MTH:
899+
elif dtype_code == FreqGroup.FR_MTH:
897900
return _monthly_finder
898-
elif (freq >= FreqGroup.FR_BUS) or fgroup == FreqGroup.FR_WK:
901+
elif (dtype_code >= FreqGroup.FR_BUS) or fgroup == FreqGroup.FR_WK:
899902
return _daily_finder
900903
else: # pragma: no cover
901-
raise NotImplementedError(f"Unsupported frequency: {freq}")
904+
raise NotImplementedError(f"Unsupported frequency: {dtype_code}")
902905

903906

904907
class TimeSeries_DateLocator(Locator):
@@ -930,8 +933,7 @@ def __init__(
930933
day=1,
931934
plot_obj=None,
932935
):
933-
if isinstance(freq, str):
934-
freq = get_freq_code(freq)[0]
936+
freq = to_offset(freq)
935937
self.freq = freq
936938
self.base = base
937939
(self.quarter, self.month, self.day) = (quarter, month, day)
@@ -1009,8 +1011,7 @@ class TimeSeries_DateFormatter(Formatter):
10091011
"""
10101012

10111013
def __init__(self, freq, minor_locator=False, dynamic_mode=True, plot_obj=None):
1012-
if isinstance(freq, str):
1013-
freq = get_freq_code(freq)[0]
1014+
freq = to_offset(freq)
10141015
self.format = None
10151016
self.freq = freq
10161017
self.locs = []

pandas/tests/plotting/test_datetimelike.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import numpy as np
77
import pytest
88

9+
from pandas._libs.tslibs import to_offset
910
import pandas.util._test_decorators as td
1011

1112
from pandas import DataFrame, Index, NaT, Series, isna
@@ -397,12 +398,12 @@ def _test(ax):
397398
def test_get_finder(self):
398399
import pandas.plotting._matplotlib.converter as conv
399400

400-
assert conv.get_finder("B") == conv._daily_finder
401-
assert conv.get_finder("D") == conv._daily_finder
402-
assert conv.get_finder("M") == conv._monthly_finder
403-
assert conv.get_finder("Q") == conv._quarterly_finder
404-
assert conv.get_finder("A") == conv._annual_finder
405-
assert conv.get_finder("W") == conv._daily_finder
401+
assert conv.get_finder(to_offset("B")) == conv._daily_finder
402+
assert conv.get_finder(to_offset("D")) == conv._daily_finder
403+
assert conv.get_finder(to_offset("M")) == conv._monthly_finder
404+
assert conv.get_finder(to_offset("Q")) == conv._quarterly_finder
405+
assert conv.get_finder(to_offset("A")) == conv._annual_finder
406+
assert conv.get_finder(to_offset("W")) == conv._daily_finder
406407

407408
@pytest.mark.slow
408409
def test_finder_daily(self):

0 commit comments

Comments
 (0)