Added .get method with closest parameter

This commit is contained in:
Gourav Kumar 2022-03-22 21:29:58 +05:30
parent b34c14d778
commit ee48446c6e

View File

@ -5,7 +5,7 @@ from dataclasses import dataclass
from numbers import Number from numbers import Number
from typing import Iterable, List, Literal, Mapping, Sequence, Union 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) @dataclass(frozen=True)
@ -376,6 +376,33 @@ class TimeSeriesCore(UserDict):
def __contains__(self, key: object) -> bool: def __contains__(self, key: object) -> bool:
return super().__contains__(key) 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 @property
def iloc(self) -> Mapping: def iloc(self) -> Mapping:
"""Returns an item or a set of items based on index """Returns an item or a set of items based on index