changes to getitem to fetch closest date
This commit is contained in:
parent
65f2e8434c
commit
c99ffe02d0
@ -393,7 +393,7 @@ class TimeSeriesCore:
|
|||||||
def _get_item_from_date(self, date: str | datetime.datetime):
|
def _get_item_from_date(self, date: str | datetime.datetime):
|
||||||
"""Helper function to retrieve item using a date"""
|
"""Helper function to retrieve item using a date"""
|
||||||
|
|
||||||
return date, self.data[date]
|
return self.get(date, raise_error=True)
|
||||||
|
|
||||||
def _get_item_from_key(self, key: str | datetime.datetime):
|
def _get_item_from_key(self, key: str | datetime.datetime):
|
||||||
"""Helper function to implement special keys"""
|
"""Helper function to implement special keys"""
|
||||||
@ -447,9 +447,9 @@ class TimeSeriesCore:
|
|||||||
raise TypeError("Only numerical values can be stored in TimeSeries")
|
raise TypeError("Only numerical values can be stored in TimeSeries")
|
||||||
|
|
||||||
if key in self.data:
|
if key in self.data:
|
||||||
self.data[key] = value
|
self.data[key] = float(value)
|
||||||
else:
|
else:
|
||||||
self.data.update({key: value})
|
self.data.update({key: float(value)})
|
||||||
self.data = dict(sorted(self.data.items()))
|
self.data = dict(sorted(self.data.items()))
|
||||||
|
|
||||||
@date_parser(1)
|
@date_parser(1)
|
||||||
@ -765,19 +765,22 @@ class TimeSeriesCore:
|
|||||||
raise NotImplementedError("This operation is not supported.")
|
raise NotImplementedError("This operation is not supported.")
|
||||||
|
|
||||||
@date_parser(1)
|
@date_parser(1)
|
||||||
def get(self, date: str | datetime.datetime, default=None, closest=None):
|
def get(self, date: str | datetime.datetime, default=None, closest=None, raise_error: bool = False):
|
||||||
|
|
||||||
if closest is None:
|
if closest is None:
|
||||||
closest = FincalOptions.get_closest
|
closest = FincalOptions.get_closest
|
||||||
|
|
||||||
if closest == "exact":
|
if closest == "exact":
|
||||||
try:
|
# try:
|
||||||
item = self._get_item_from_date(date)
|
# item = self.data[date]
|
||||||
return item
|
# return date, item
|
||||||
except KeyError:
|
# except KeyError:
|
||||||
return default
|
# if raise_error:
|
||||||
|
# raise KeyError(date)
|
||||||
|
|
||||||
if closest == "previous":
|
# return default
|
||||||
|
delta = 0
|
||||||
|
elif closest == "previous":
|
||||||
delta = datetime.timedelta(-1)
|
delta = datetime.timedelta(-1)
|
||||||
elif closest == "next":
|
elif closest == "next":
|
||||||
delta = datetime.timedelta(1)
|
delta = datetime.timedelta(1)
|
||||||
@ -786,9 +789,13 @@ class TimeSeriesCore:
|
|||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
item = self._get_item_from_date(date)
|
item = self.data[date]
|
||||||
return item
|
return date, item
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
if delta == 0:
|
||||||
|
if raise_error:
|
||||||
|
raise KeyError(date)
|
||||||
|
return default
|
||||||
date += delta
|
date += delta
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -8,7 +8,7 @@ from .exceptions import DateNotFoundError, DateOutOfRangeError
|
|||||||
@dataclass
|
@dataclass
|
||||||
class FincalOptions:
|
class FincalOptions:
|
||||||
date_format: str = "%Y-%m-%d"
|
date_format: str = "%Y-%m-%d"
|
||||||
closest: str = "before" # after
|
closest: str = "previous" # next
|
||||||
traded_days: int = 365
|
traded_days: int = 365
|
||||||
get_closest: str = "exact"
|
get_closest: str = "exact"
|
||||||
|
|
||||||
|
@ -386,7 +386,7 @@ class TestTimeSeriesArithmatic:
|
|||||||
|
|
||||||
def test_truediv(self):
|
def test_truediv(self):
|
||||||
ts = TimeSeriesCore(self.data, "M")
|
ts = TimeSeriesCore(self.data, "M")
|
||||||
ser = Series([21, 21, 23, 24], "number")
|
ser = Series([22, 23, 24, 25], "number")
|
||||||
|
|
||||||
num_div_ts = ts / 10
|
num_div_ts = ts / 10
|
||||||
assert num_div_ts["2021-01-01"][1] == 22
|
assert num_div_ts["2021-01-01"][1] == 22
|
||||||
|
Loading…
Reference in New Issue
Block a user