From 130f4e58e91ce326b3badda347e2fe1f2a76feef Mon Sep 17 00:00:00 2001 From: Gourav Kumar Date: Wed, 18 May 2022 21:39:57 -0700 Subject: [PATCH] started working on transform with aggregation --- fincal/fincal.py | 39 +++++++++++++++++++++++++++++++++++++++ requirements.txt | Bin 1002 -> 942 bytes 2 files changed, 39 insertions(+) diff --git a/fincal/fincal.py b/fincal/fincal.py index c0b2c94..3cc7bfb 100644 --- a/fincal/fincal.py +++ b/fincal/fincal.py @@ -783,6 +783,45 @@ 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 + ) -> TimeSeries: + """Transform a time series object into a lower frequency object with an aggregation function. + + Parameters + ---------- + to_frequency: + Frequency to which the time series needs to be transformed + + method: + Aggregation method to be used. Can be either mean or sum + + eomonth: + User end of month dates. Only applicable for frequencies monthly and lower. + + Returns + ------- + Returns a TimeSeries object + + Raises + ------- + ValueError: + * If invalid input is passed for frequency + * if invalid input is passed for method + * If to_frequency is higher than the current frequency + """ + + try: + to_frequency: Frequency = getattr(AllFrequencies, to_frequency) + except AttributeError: + raise ValueError(f"Invalid argument for to_frequency {to_frequency}") + + if to_frequency.days <= self.frequency.days: + raise ValueError("TimeSeries can be only shrunk to a lower frequency") + + if method not in ["sum", "mean"]: + raise ValueError(f"Method not recognised: {method}") + def _preprocess_csv(file_path: str | pathlib.Path, delimiter: str = ",", encoding: str = "utf-8") -> List[list]: """Preprocess csv data""" diff --git a/requirements.txt b/requirements.txt index af4b474ae3c722c8ac5094cab6e7c45f32a3d33f..0f071b335a47c3e0b4a7e76d4d64df0f8df7edf2 100644 GIT binary patch delta 16 YcmaFGzK(svG{(tW7}YlaVEn`g06eh<`v3p{ delta 64 zcmZ3-{)&CWG)BohhEj%Hh609423sIBWYA+U0zx1;c_O2>XdXifLn1>lT+Re2W4L)U H<8MX))$R?q