From 2c1d5087342077f3a7ae12ae2f23249b9b066c44 Mon Sep 17 00:00:00 2001 From: Gourav Kumar Date: Sun, 20 Feb 2022 21:36:44 +0530 Subject: [PATCH] implemented iterator --- fincal/core.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/fincal/core.py b/fincal/core.py index d4acef0..6893faa 100644 --- a/fincal/core.py +++ b/fincal/core.py @@ -148,6 +148,7 @@ class TimeSeriesCore: self.start_date = self.dates[0] self.end_date = self.dates[-1] self.frequency = getattr(AllFrequencies, frequency) + self.iter_num = -1 def _get_printable_slice(self, n: int): """Returns a slice of the dataframe from beginning and end""" @@ -215,6 +216,18 @@ class TimeSeriesCore: def __len__(self): return len(self.time_series) + def __iter__(self): + self.n = 0 + return self + + def __next__(self): + if self.n >= len(self.dates): + raise StopIteration + else: + key = self.dates[self.n] + self.n += 1 + return key, self.time_series[key] + def head(self, n: int = 6): """Returns the first n items of the TimeSeries object"""