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
### 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
- [ ] Implement interpolation in expand

View File

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