Added .get method with closest parameter
This commit is contained in:
parent
b34c14d778
commit
ee48446c6e
@ -5,7 +5,7 @@ from dataclasses import dataclass
|
||||
from numbers import Number
|
||||
from typing import Iterable, List, Literal, Mapping, Sequence, Union
|
||||
|
||||
from .utils import _parse_date, _preprocess_timeseries
|
||||
from .utils import FincalOptions, _parse_date, _preprocess_timeseries
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@ -376,6 +376,33 @@ class TimeSeriesCore(UserDict):
|
||||
def __contains__(self, key: object) -> bool:
|
||||
return super().__contains__(key)
|
||||
|
||||
@date_parser(1)
|
||||
def get(self, date: Union[str, datetime.datetime], default=None, closest=None):
|
||||
|
||||
if closest is None:
|
||||
closest = FincalOptions.get_closest
|
||||
|
||||
if closest == "exact":
|
||||
try:
|
||||
item = self._get_item_from_date(date)
|
||||
return item
|
||||
except KeyError:
|
||||
return default
|
||||
|
||||
if closest == "previous":
|
||||
delta = datetime.timedelta(-1)
|
||||
elif closest == "next":
|
||||
delta = datetime.timedelta(1)
|
||||
else:
|
||||
raise ValueError(f"Invalid argument from closest {closest!r}")
|
||||
|
||||
while True:
|
||||
try:
|
||||
item = self._get_item_from_date(date)
|
||||
return item
|
||||
except KeyError:
|
||||
date += delta
|
||||
|
||||
@property
|
||||
def iloc(self) -> Mapping:
|
||||
"""Returns an item or a set of items based on index
|
||||
|
Loading…
Reference in New Issue
Block a user