Skip to content

Commit

Permalink
REF: move offset_to_period_map from liboffsets (#34447)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored May 29, 2020
1 parent 484e0b4 commit 83d3c4c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 50 deletions.
49 changes: 1 addition & 48 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ from pandas._libs.tslibs.util cimport is_integer_object, is_datetime64_object
from pandas._libs.tslibs.base cimport ABCTimestamp

from pandas._libs.tslibs.ccalendar import (
MONTHS, DAYS, MONTH_ALIASES, MONTH_TO_CAL_NUM, weekday_to_int, int_to_weekday,
MONTH_ALIASES, MONTH_TO_CAL_NUM, weekday_to_int, int_to_weekday,
)
from pandas._libs.tslibs.ccalendar cimport get_days_in_month, dayofweek
from pandas._libs.tslibs.conversion cimport (
Expand All @@ -45,53 +45,6 @@ from pandas._libs.tslibs.tzconversion cimport tz_convert_single

from .timedeltas cimport delta_to_nanoseconds

# ---------------------------------------------------------------------
# Constants

_offset_to_period_map = {
'WEEKDAY': 'D',
'EOM': 'M',
'BM': 'M',
'BQS': 'Q',
'QS': 'Q',
'BQ': 'Q',
'BA': 'A',
'AS': 'A',
'BAS': 'A',
'MS': 'M',
'D': 'D',
'C': 'C',
'B': 'B',
'T': 'T',
'S': 'S',
'L': 'L',
'U': 'U',
'N': 'N',
'H': 'H',
'Q': 'Q',
'A': 'A',
'W': 'W',
'M': 'M',
'Y': 'A',
'BY': 'A',
'YS': 'A',
'BYS': 'A'}

need_suffix = ['QS', 'BQ', 'BQS', 'YS', 'AS', 'BY', 'BA', 'BYS', 'BAS']

for __prefix in need_suffix:
for _m in MONTHS:
key = f'{__prefix}-{_m}'
_offset_to_period_map[key] = _offset_to_period_map[__prefix]

for __prefix in ['A', 'Q']:
for _m in MONTHS:
_alias = f'{__prefix}-{_m}'
_offset_to_period_map[_alias] = _alias

for _d in DAYS:
_offset_to_period_map[f'W-{_d}'] = f'W-{_d}'


# ---------------------------------------------------------------------
# Misc Helpers
Expand Down
54 changes: 52 additions & 2 deletions pandas/tseries/frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@

from pandas._libs.algos import unique_deltas
from pandas._libs.tslibs import Timestamp
from pandas._libs.tslibs.ccalendar import MONTH_ALIASES, MONTH_NUMBERS, int_to_weekday
from pandas._libs.tslibs.ccalendar import (
DAYS,
MONTH_ALIASES,
MONTH_NUMBERS,
MONTHS,
int_to_weekday,
)
from pandas._libs.tslibs.fields import build_field_sarray
from pandas._libs.tslibs.offsets import ( # noqa:F401
DateOffset,
Day,
_get_offset,
_offset_to_period_map,
to_offset,
)
from pandas._libs.tslibs.parsing import get_rule_month
Expand Down Expand Up @@ -39,6 +44,51 @@
# ---------------------------------------------------------------------
# Offset names ("time rules") and related functions

_offset_to_period_map = {
"WEEKDAY": "D",
"EOM": "M",
"BM": "M",
"BQS": "Q",
"QS": "Q",
"BQ": "Q",
"BA": "A",
"AS": "A",
"BAS": "A",
"MS": "M",
"D": "D",
"C": "C",
"B": "B",
"T": "T",
"S": "S",
"L": "L",
"U": "U",
"N": "N",
"H": "H",
"Q": "Q",
"A": "A",
"W": "W",
"M": "M",
"Y": "A",
"BY": "A",
"YS": "A",
"BYS": "A",
}

_need_suffix = ["QS", "BQ", "BQS", "YS", "AS", "BY", "BA", "BYS", "BAS"]

for _prefix in _need_suffix:
for _m in MONTHS:
key = f"{_prefix}-{_m}"
_offset_to_period_map[key] = _offset_to_period_map[_prefix]

for _prefix in ["A", "Q"]:
for _m in MONTHS:
_alias = f"{_prefix}-{_m}"
_offset_to_period_map[_alias] = _alias

for _d in DAYS:
_offset_to_period_map[f"W-{_d}"] = f"W-{_d}"


def get_period_alias(offset_str: str) -> Optional[str]:
"""
Expand Down

0 comments on commit 83d3c4c

Please sign in to comment.