Skip to content

Commit

Permalink
Fix pandas 2.2. deprecation warnings
Browse files Browse the repository at this point in the history
Fixes pandas 2.2 deprecation warnings concerning deprecation of frequency units "T", "H" and "S" in favor of "min", "h" and "s".
  • Loading branch information
maread99 committed Feb 1, 2024
1 parent 6759706 commit ed1ac95
Show file tree
Hide file tree
Showing 30 changed files with 517 additions and 489 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ The above call was made 21 minutes after the NYSE open. Notice that the call ret
Any interval can be evaluated (limited only by the availability of underlying data).
```python
>>> # prices over a specific session at 68 minute intervals
>>> prices.get("68T", start="2022-06-27", end="2022-06-27", force=True)
>>> prices.get("68min", start="2022-06-27", end="2022-06-27", force=True)
```
```
symbol MSFT
Expand Down Expand Up @@ -108,7 +108,7 @@ Although some indices are longer than three calendar days, they all comprise of
)
>>> # lead_symbol determines the exchange against which the period will be evaluated and
>>> # the default output time zone (which for Bitcoin is UTC).
>>> prices_mult.get("90T", hours=9, lead_symbol="BTC-USD")
>>> prices_mult.get("90min", hours=9, lead_symbol="BTC-USD")
```
```
symbol MSFT 9988.HK BTC-USD
Expand All @@ -127,7 +127,7 @@ By default prices are shown as missing when the exchange is closed (the time zon
The `get` method has plenty of options to customize the output, including `fill` to fill in indices when an exchange is closed...
```python
>>> # as before, only now filling in prices when exchanges are closed
>>> prices_mult.get("90T", hours=9, lead_symbol="BTC-USD", fill="both")
>>> prices_mult.get("90min", hours=9, lead_symbol="BTC-USD", fill="both")
```
```
symbol MSFT 9988.HK BTC-USD
Expand Down
2 changes: 1 addition & 1 deletion src/market_prices/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ def _requested_dates_adjust(self, dr: DateRangeReq) -> DateRangeReq | None:
elif self.bi.is_intraday:
assert self._delay is not None
# ten minutes to cover provider delays in publishing data.
rerequest_from = helpers.now() - self._delay - pd.Timedelta(10, "T")
rerequest_from = helpers.now() - self._delay - pd.Timedelta(10, "min")
if end < rerequest_from:
return dr
excess = end - rerequest_from
Expand Down
8 changes: 4 additions & 4 deletions src/market_prices/daterange.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ def end_now(self) -> tuple[pd.Timestamp, pd.Timestamp]:
# which the end is accurate as at the moment the end is requested.
# (without the + one minute would be ignoring those price that have
# come in since the current minute started.)
end_accuracy = min(end_accuracy, now + pd.Timedelta("1T"))
end_accuracy = min(end_accuracy, now + pd.Timedelta("1min"))
return end, end_accuracy

def _trading_index(
Expand Down Expand Up @@ -1055,7 +1055,7 @@ def _offset_days(self, ts: pd.Timestamp, days: int) -> pd.Timestamp:

# "previous" to cover ts as close (not a trading minute).
session = self.cal.minute_to_session(ts, "previous")
schedule_vals = self.cal.schedule.loc[session].dropna().view(np.int64).values
schedule_vals = self.cal.schedule.loc[session].dropna().astype(np.int64).values
if ts.value in schedule_vals:
target_i = self.cal.sessions.get_loc(session) + days

Expand Down Expand Up @@ -1199,7 +1199,7 @@ def daterange(self) -> tuple[mptypes.DateRange, pd.Timestamp]:
if intraday_duration:
if intraday_duration < self.final_interval.as_minutes:
raise errors.PricesUnavailableIntervalDurationError(
pd.Timedelta(intraday_duration, "T"), self
pd.Timedelta(intraday_duration, "min"), self
)
if start is None:
end_ = end
Expand Down Expand Up @@ -1269,7 +1269,7 @@ def daterange(self) -> tuple[mptypes.DateRange, pd.Timestamp]:
else:
minutes = calutils.minutes_in_period(self.cal, start, end_)
if self.final_interval.as_minutes > minutes:
period_duration = pd.Timedelta(minutes, "T")
period_duration = pd.Timedelta(minutes, "min")
raise errors.PricesUnavailableIntervalPeriodError(
self, start, end_, period_duration
)
Expand Down
4 changes: 2 additions & 2 deletions src/market_prices/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -872,10 +872,10 @@ class PricesMissingWarning(PricesWarning):

def __init__(self, symbol: str, bi: BI, sessions: pd.DatetimeIndex, source: str):
date_format = "%Y-%m-%d"
sessions = sessions.format(date_format=date_format)
sessions_ = sessions.strftime(date_format).tolist()
self._msg = (
f"Prices from {source} are missing for '{symbol}' at the"
f" base interval '{bi}' for the following sessions: {sessions}."
f" base interval '{bi}' for the following sessions: {sessions_}."
)


Expand Down
12 changes: 6 additions & 6 deletions src/market_prices/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
UTC = zoneinfo.ZoneInfo("UTC")

ONE_DAY: pd.Timedelta = pd.Timedelta(1, "D")
ONE_MIN: pd.Timedelta = pd.Timedelta(1, "T")
ONE_SEC: pd.Timedelta = pd.Timedelta(1, "S")
ONE_MIN: pd.Timedelta = pd.Timedelta(1, "min")
ONE_SEC: pd.Timedelta = pd.Timedelta(1, "s")


def symbols_to_list(symbols: mptypes.Symbols) -> list[str]:
Expand Down Expand Up @@ -155,7 +155,7 @@ def now(
now_ = now_.tz_convert(None)
res = "D"
else:
res = "T"
res = "min"
return now_.ceil(res) if side == "right" else now_.floor(res)


Expand All @@ -172,7 +172,7 @@ def extract_freq_parts(freq: str) -> tuple[int, str]:
Parameters
----------
freq
Pandas frequency, for example "5D", "30T", "4H", "33MS".
Pandas frequency, for example "5D", "30min", "4h", "33MS".
Raises
------
Expand All @@ -192,8 +192,8 @@ def extract_freq_parts(freq: str) -> tuple[int, str]:
Examples
--------
>>> extract_freq_parts("22T")
(22, 'T')
>>> extract_freq_parts("22min")
(22, 'min')
>>> extract_freq_parts("1m")
(1, 'm')
>>> extract_freq_parts("D")
Expand Down
29 changes: 16 additions & 13 deletions src/market_prices/intervals.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ def as_pdtd(self) -> pd.Timedelta:
return pd.Timedelta(self)

@property
def freq_unit(self) -> typing.Literal["T", "H", "D"]:
def freq_unit(self) -> typing.Literal["min", "h", "D"]:
"""Return unit of pandas frequency represented by the member.
Returns either "T", "H" or "D".
Returns either "min", "h" or "D".
"""
return self.as_pdtd.resolution_string

Expand All @@ -64,7 +64,7 @@ def freq_value(self) -> int:
components = self.as_pdtd.components
if self.freq_unit == "D":
return components.days
elif self.freq_unit == "H":
elif self.freq_unit == "h":
return components.hours
else:
return components.minutes + (components.hours * 60)
Expand All @@ -74,7 +74,7 @@ def is_intraday(self) -> bool:
"""Query if a member represents an intraday interval.
An interval is considered to be intraday if it is shorter than one
day. The unit of intraday intervals will be either "T" or "H".
day. The unit of intraday intervals will be either "min" or "h".
"""
return self < helpers.ONE_DAY

Expand Down Expand Up @@ -126,7 +126,7 @@ def as_offset(
calendar
Calendar against which to evaluate custom business day for
intervals with `self.freq_unit` as "D". Not required for
intervals with `self.freq_unit` as "T" or "H".
intervals with `self.freq_unit` as "min" or "h".
one_less
If `self.freq_unit` is "D" then:
Expand Down Expand Up @@ -260,7 +260,7 @@ def is_intraday(self) -> bool:
"""Query if a member represents an intraday interval.
An interval is considered to be intraday if it is shorter than one
day. The unit of intraday intervals will be either "T" or "H".
day. The unit of intraday intervals will be either "min" or "h".
"""
return False

Expand Down Expand Up @@ -376,9 +376,9 @@ def create_base_intervals_enum(intervals: list[TDInterval]) -> _BaseInterval:
for intrvl in intervals:
unit, value = intrvl.freq_unit, intrvl.freq_value
td_args: tuple
if unit == "T":
if unit == "min":
td_args = (0, 0, 0, 0, value)
elif unit == "H":
elif unit == "h":
td_args = (0, 0, 0, 0, 0, value)
else:
if intrvl is not TDInterval.D1:
Expand Down Expand Up @@ -432,6 +432,8 @@ def to_ptinterval(interval: str | timedelta | pd.Timedelta) -> PTInterval:
f"`interval` unit must by one of {valid_units} (or lower-"
f"case) although evaluated to '{unit}'."
)
if unit == "MIN":
unit = "T"

else:
if interval <= timedelta(0):
Expand All @@ -448,7 +450,7 @@ def to_ptinterval(interval: str | timedelta | pd.Timedelta) -> PTInterval:
' example "1m" for one month.'
)

valid_resolutions = ["T", "H", "D"]
valid_resolutions = ["min", "h", "D"]
if interval.resolution_string not in valid_resolutions:
raise ValueError(error_msg)

Expand Down Expand Up @@ -476,8 +478,7 @@ def raise_value_oob_error(component: str, limit: int):
}

if isinstance(interval, str):
if unit == "MIN":
unit = "T"

if value > limits[unit]:
raise_value_oob_error(components[unit], limits[unit])
if unit == "T" and not value % 60:
Expand All @@ -487,10 +488,12 @@ def raise_value_oob_error(component: str, limit: int):

else:
unit = interval.resolution_string
if unit == "T":
if unit in ["min", "T"]: # "T" for compatibility pandas < 2.2
value = int(interval.total_seconds() // 60)
elif unit == "H":
unit = "T"
elif unit in ["h", "H"]: # "H" for compatibility pandas < 2.2
value = int(interval.total_seconds() // 3600)
unit = "H"
else:
value = interval.days

Expand Down
4 changes: 2 additions & 2 deletions src/market_prices/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ def _parse_start_end(
start_is_date = helpers.is_date(start)
if not start_is_date:
# do not include any incomplete minute
start = start.ceil("T")
start = start.ceil("min")
if end is not None:
end_is_date = helpers.is_date(end)
if not end_is_date:
# do not include incomplete minute
end = end.floor("T")
end = end.floor("min")
# if end > now, set to None.
now_interval = intervals.ONE_DAY if end_is_date else intervals.ONE_MIN
now = helpers.now(now_interval, "left")
Expand Down
16 changes: 8 additions & 8 deletions src/market_prices/prices/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,7 @@ def lead_symbol_default(self) -> str:

def _set_delays(self, delays: int | list[int] | dict[str, int]):
d = self._dict_for_all_symbols("delays", delays)
self._delays = {k: pd.Timedelta(v, "T") for k, v in d.items()}
self._delays = {k: pd.Timedelta(v, "min") for k, v in d.items()}

@property
def delays(self) -> dict[str, pd.Timedelta]:
Expand Down Expand Up @@ -1640,8 +1640,8 @@ def _set_indices_aligned(self):

index = self._trading_indexes[bi]
nano_index = index.left.asi8
opens = self.cc.opens[slc].view("int64")
closes = self.cc.closes[slc].view("int64")
opens = self.cc.opens[slc].astype("int64")
closes = self.cc.closes[slc].astype("int64")

sessions = opens.index
srs = pd.Series(True, index=sessions)
Expand Down Expand Up @@ -3444,20 +3444,20 @@ def get(
value:
one or more digits.
unit:
"min", "t", "MIN" or "T" for minutes
"min", "MIN", "T" or "t" for minutes
"h" or "H" for hours
"d" or "D' for days
'm' or "M" for months
Examples:
thirty minutes:
"30min", "30T"
pd.Timedelta(30, "T"), pd.Timedelta(minutes=30)
pd.Timedelta(30, "min"), pd.Timedelta(minutes=30)
timedelta(minutes=30)
three hours:
"3h", "3H"
pd.Timedelta(3, "H"), pd.Timedelta(hours=3)
pd.Timedelta(3, "h"), pd.Timedelta(hours=3)
timedelta(hours=3)
one day:
Expand Down Expand Up @@ -3854,8 +3854,8 @@ def get(
greater than the interval although the indice bounds
they evaluate to do not. For example, if a session
opens at 09.00, `start` is 09.01 and `end` is 09.09
then a call for data at a "5T" `interval` will evalute
the period bounds as from 09.05 through 09.05.
then a call for data at a "5min" `interval` will
evalute the period bounds as from 09.05 through 09.05.
errors.PricesUnavailableDOIntervalPeriodError:
Monthly `interval` is longer than the evaluated period.
Expand Down
4 changes: 2 additions & 2 deletions src/market_prices/prices/yahoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ def _set_daily_bi_limit(self):
@staticmethod
def _bi_to_source_key(interval: intervals.BI) -> str:
"""Map interval to value for source's interval parameter."""
if interval.freq_unit == "T":
if interval.freq_unit == "min":
return str(interval.freq_value) + "m"
else:
return interval.as_pdfreq.lower() # as yahooquery value
Expand Down Expand Up @@ -868,6 +868,6 @@ def _request_data(
# 22 hours ensures markets opening in Americas included
# whilst avoiding including the following session of
# Australasian markets
end_ += pd.Timedelta(22, "H")
end_ += pd.Timedelta(22, "h")
prices = self._request_yahoo(interval=interval, start=start, end=end_)
return self._tidy_yahoo(prices, interval, start, end)
Loading

0 comments on commit ed1ac95

Please sign in to comment.