Implemented limit parameter in ffill and bfill

This commit is contained in:
Gourav Kumar 2022-04-26 10:32:42 +05:30
parent 978566e0a8
commit 79cd44d41f
2 changed files with 18 additions and 7 deletions

View File

@ -34,6 +34,6 @@ Fincal aims to simplify things by allowing you to:
- [x] Max drawdown - [x] Max drawdown
### Pending implementation ### Pending implementation
- [ ] Use limit parameter in ffill and bfill - [x] Use limit parameter in ffill and bfill
- [x] Implementation of ffill and bfill may be incorrect inside expand, check and correct - [x] Implementation of ffill and bfill may be incorrect inside expand, check and correct
- [ ] Implement interpolation in expand - [ ] Implement interpolation in expand

View File

@ -170,11 +170,16 @@ class TimeSeries(TimeSeriesCore):
) )
new_ts = dict() new_ts = dict()
counter = 0
for cur_date in dates_to_fill: for cur_date in dates_to_fill:
try: try:
cur_val = self.get(cur_date, closest="previous") new_val = self[cur_date]
cur_val = new_val
counter = 0
except KeyError: except KeyError:
pass if counter >= limit:
continue
counter += 1
new_ts.update({cur_date: cur_val[1]}) new_ts.update({cur_date: cur_val[1]})
if inplace: if inplace:
@ -209,13 +214,19 @@ class TimeSeries(TimeSeriesCore):
dates_to_fill.append(self.end_date) dates_to_fill.append(self.end_date)
bfill_ts = dict() bfill_ts = dict()
counter = 0
for cur_date in reversed(dates_to_fill): for cur_date in reversed(dates_to_fill):
try: try:
cur_val = self.data[cur_date] new_val = self[cur_date]
cur_val = new_val
counter = 0
except KeyError: except KeyError:
pass if counter >= limit:
bfill_ts.update({cur_date: cur_val}) continue
new_ts = {k: bfill_ts[k] for k in reversed(bfill_ts)} counter += 1
bfill_ts.update({cur_date: cur_val[1]})
# new_ts = {k: bfill_ts[k] for k in reversed(bfill_ts)}
new_ts = dict(list(reversed(bfill_ts.items())))
if inplace: if inplace:
self.data = new_ts self.data = new_ts
return None return None