Corrected date getting missed in transform

This commit is contained in:
Gourav Kumar 2023-03-26 20:33:01 +05:30
parent 0bab00f455
commit 14afb1400a

View File

@ -809,7 +809,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.
@ -854,18 +858,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)