diff --git a/README.md b/README.md index 7f175fd..b6b5333 100644 --- a/README.md +++ b/README.md @@ -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 \ No newline at end of file diff --git a/fincal/fincal.py b/fincal/fincal.py index 2e2c922..f4d1bfa 100644 --- a/fincal/fincal.py +++ b/fincal/fincal.py @@ -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