diff --git a/pyfacts/pyfacts.py b/pyfacts/pyfacts.py index fdeff56..fd41153 100644 --- a/pyfacts/pyfacts.py +++ b/pyfacts/pyfacts.py @@ -805,7 +805,11 @@ class TimeSeries(TimeSeriesCore): return statistics.mean(self.values) def transform( - self, to_frequency: Literal["W", "M", "Q", "H", "Y"], method: Literal["sum", "mean"], eomonth: bool = False + self, + to_frequency: Literal["W", "M", "Q", "H", "Y"], + method: Literal["sum", "mean"], + eomonth: bool = False, + anchor_date=Literal["start", "end"], ) -> TimeSeries: """Transform a time series object into a lower frequency object with an aggregation function. @@ -850,18 +854,21 @@ class TimeSeries(TimeSeriesCore): ensure_coverage=True, eomonth=eomonth, ) - prev_date = dates[0] + # prev_date = dates[0] new_ts_dict = {} - for date in dates[1:]: - cur_data = self[(self.dates >= prev_date) & (self.dates < date)] + for idx, date in enumerate(dates): + if idx == 0: + cur_data = self[self.dates <= date] + else: + cur_data = self[(self.dates <= date) & (self.dates > dates[idx - 1])] if method == "sum": value = sum(cur_data.values) elif method == "mean": value = cur_data.mean() - new_ts_dict.update({prev_date: value}) - prev_date = date + new_ts_dict.update({date: value}) + # prev_date = date return self.__class__(new_ts_dict, to_frequency.symbol)