Skip to content

Commit

Permalink
Merge pull request #36 from wilsonfreitas/feature/pmc
Browse files Browse the repository at this point in the history
Added support to python_market_calendars
  • Loading branch information
wilsonfreitas authored Oct 29, 2023
2 parents da3fb4c + df7ad02 commit fceae8c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
20 changes: 17 additions & 3 deletions bizdays.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
from datetime import datetime, date, timedelta
from itertools import cycle
from typing import TextIO

PANDAS_INSTALLED = False

Expand Down Expand Up @@ -972,9 +973,22 @@ def load(cls, name=None, filename=None):
"""
if filename:
res = _checkfile(filename)
return cls._load_calendar_from_file(res)
elif name:
res = _checklocalfile(name)
if name.startswith("PMC/"):
try:
import pandas_market_calendars as mcal
except ImportError:
raise Exception("pandas_market_calendars must be installed to use PMC calendars")
cal = mcal.get_calendar(name[4:])
hol = cal.holidays()
return Calendar((d.item() for d in hol.holidays), weekdays=("Saturday", "Sunday"), name=name)
else:
res = _checklocalfile(name)
return cls._load_calendar_from_file(res)

@classmethod
def _load_calendar_from_file(cls, res: dict[str, TextIO]) -> "Calendar":
w = "|".join(w.lower() for w in cls._weekdays)
wre = "^%s$" % w
_holidays = []
Expand Down Expand Up @@ -1008,7 +1022,7 @@ def __str__(self):
__repr__ = __str__


def _checkfile(fname):
def _checkfile(fname: str) -> dict[str, TextIO]:
if not os.path.exists(fname):
raise Exception(f"Invalid calendar: {fname}")
name = os.path.split(fname)[-1]
Expand All @@ -1019,7 +1033,7 @@ def _checkfile(fname):
return {"name": name, "iter": open(fname)}


def _checklocalfile(name):
def _checklocalfile(name: str) -> dict[str, TextIO]:
dir = os.path.dirname(__file__)
fname = f"{dir}/{name}.cal"
if not os.path.exists(fname):
Expand Down
7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ readme = "README.md"
include = ["ANBIMA.cal", "B3.cal"]

[tool.poetry.dependencies]
python = "^3.7"
python = "^3.8"

[tool.poetry.group.dev.dependencies]
pytest = "^7.1.3"
pycodestyle = "^2.11.1"
pandas-market-calendars = "4.3.1"
ipykernel = "^6.26.0"

[tool.poetry.group.docs.dependencies]
Sphinx = "^5.1.1"
nbsphinx = "^0.8.9"
matplotlib = "^3.5.3"
numpy = "^1.23.2"
lxml = "^4.9.1"
pandas = "^1.4.4"
pandas = "^1.5.3"
alabaster = "^0.7.12"
ipywidgets = "^8.0.2"

Expand Down
6 changes: 6 additions & 0 deletions test_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ def test_calendar_load():
def test_calendar_load_invalid():
with pytest.raises(Exception):
cal = Calendar.load("B1")


def test_calendar_load_pmc():
cal = Calendar.load("PMC/B3")
assert cal.name == "PMC/B3"
assert len(cal.holidays) > 4000

0 comments on commit fceae8c

Please sign in to comment.