markowitz.data.calendars¶
markowitz.data.calendars
¶
Trading-calendar utilities.
Wraps :mod:pandas_market_calendars when available and falls back to
a hand-rolled NYSE approximation (weekdays minus a hard-coded set of
US federal market holidays) when the optional dependency is missing.
The fallback is sufficient for unit testing but production callers
should install pandas_market_calendars.
reindex_to_sessions(df: pd.DataFrame, *, exchange: str = 'XNYS', on_missing: str = 'raise', max_gap_sessions: int = 1) -> pd.DataFrame
¶
Align df to the canonical session calendar.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input frame indexed by tz-naive midnight timestamps. |
required |
exchange
|
str
|
Exchange MIC code understood by |
'XNYS'
|
on_missing
|
str
|
|
'raise'
|
max_gap_sessions
|
int
|
Maximum number of consecutive missing sessions tolerated when
|
1
|
Source code in src/markowitz/data/calendars.py
trading_sessions(start: str | _dt.date | pd.Timestamp, end: str | _dt.date | pd.Timestamp, *, exchange: str = 'XNYS') -> pd.DatetimeIndex
¶
Return tz-naive midnight DatetimeIndex of trading sessions.
Uses :mod:pandas_market_calendars when installed. Falls back to a
NYSE weekday-minus-fixed-holidays approximation otherwise.