This commit is contained in:
Gourav Kumar 2023-07-16 19:54:23 +05:30
commit c453ff20e5

View File

@ -805,7 +805,11 @@ class TimeSeries(TimeSeriesCore):
return statistics.mean(self.values) return statistics.mean(self.values)
def transform( 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: ) -> TimeSeries:
"""Transform a time series object into a lower frequency object with an aggregation function. """Transform a time series object into a lower frequency object with an aggregation function.
@ -850,18 +854,21 @@ class TimeSeries(TimeSeriesCore):
ensure_coverage=True, ensure_coverage=True,
eomonth=eomonth, eomonth=eomonth,
) )
prev_date = dates[0] # prev_date = dates[0]
new_ts_dict = {} new_ts_dict = {}
for date in dates[1:]: for idx, date in enumerate(dates):
cur_data = self[(self.dates >= prev_date) & (self.dates < date)] if idx == 0:
cur_data = self[self.dates <= date]
else:
cur_data = self[(self.dates <= date) & (self.dates > dates[idx - 1])]
if method == "sum": if method == "sum":
value = sum(cur_data.values) value = sum(cur_data.values)
elif method == "mean": elif method == "mean":
value = cur_data.mean() value = cur_data.mean()
new_ts_dict.update({prev_date: value}) new_ts_dict.update({date: value})
prev_date = date # prev_date = date
return self.__class__(new_ts_dict, to_frequency.symbol) return self.__class__(new_ts_dict, to_frequency.symbol)