diff --git a/arpav_ppcv/operations.py b/arpav_ppcv/operations.py index fdcc178d..8ad1ac33 100644 --- a/arpav_ppcv/operations.py +++ b/arpav_ppcv/operations.py @@ -70,13 +70,21 @@ def get_observation_time_series( info = {} if include_decade_data: - decade_df = df.groupby((df.index.year // 10) * 10).mean() - decade_df = decade_df.drop(columns=[base_name]) + # group values by climatological decade, which starts at year 1 and ends at year 10 + decade_grouper = df.groupby(((df.index.year - 1) // 10) * 10) + + mean_column_name = f"{base_name}__DECADE_MEAN" + decade_df = decade_grouper.agg( + num_values=(unsmoothed_col_name, "size"), + **{mean_column_name: (unsmoothed_col_name, "mean")}, + ) + + # discard decades where there are less than 7 years + decade_df = decade_df[decade_df.num_values >= 7] + + decade_df = decade_df.drop(columns=["num_values"]) decade_df["time"] = pd.to_datetime(decade_df.index.astype(str), utc=True) decade_df.set_index("time", inplace=True) - decade_df = decade_df.rename( - columns={unsmoothed_col_name: f"{base_name}__DECADE_MEAN"} - ) else: decade_df = None diff --git a/tests/notebooks/observation-time-series.ipynb b/tests/notebooks/observation-time-series.ipynb index e4f8a6a7..293cbd85 100644 --- a/tests/notebooks/observation-time-series.ipynb +++ b/tests/notebooks/observation-time-series.ipynb @@ -31,12 +31,12 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 2, "id": "233ab640-9ac4-4fe0-b4b3-31e177cb67b1", "metadata": {}, "outputs": [], "source": [ - "df, decade_df, mk_df = operations.get_observation_time_series(\n", + "df, decade_df, mk_df, info = operations.get_observation_time_series(\n", " session,\n", " variable,\n", " station,\n", @@ -50,7 +50,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "id": "65b0cbc2-58be-4001-9653-bd0386ae2283", "metadata": {}, "outputs": [ @@ -322,7 +322,7 @@ "2024-01-01 00:00:00+00:00 2.416 NaN" ] }, - "execution_count": 4, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -333,7 +333,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 4, "id": "574e423c-35f1-4c1f-a1eb-5295c8b187df", "metadata": {}, "outputs": [ @@ -367,40 +367,30 @@ " \n", " \n", " \n", - " 1980\n", - " 0.845667\n", - " \n", - " \n", - " 1990\n", - " 1.246000\n", - " \n", - " \n", - " 2000\n", - " 0.847400\n", + " 1990-01-01 00:00:00+00:00\n", + " 1.0870\n", " \n", " \n", - " 2010\n", - " 1.280200\n", + " 2000-01-01 00:00:00+00:00\n", + " 0.8325\n", " \n", " \n", - " 2020\n", - " 1.941400\n", + " 2010-01-01 00:00:00+00:00\n", + " 1.6243\n", " \n", " \n", "\n", "" ], "text/plain": [ - " TDd__DECADE_MEAN\n", - "time \n", - "1980 0.845667\n", - "1990 1.246000\n", - "2000 0.847400\n", - "2010 1.280200\n", - "2020 1.941400" + " TDd__DECADE_MEAN\n", + "time \n", + "1990-01-01 00:00:00+00:00 1.0870\n", + "2000-01-01 00:00:00+00:00 0.8325\n", + "2010-01-01 00:00:00+00:00 1.6243" ] }, - "execution_count": 24, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -621,7 +611,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.14" } }, "nbformat": 4,