Skip to content

Commit

Permalink
Merge pull request #124 from ricardogsilva/109-define-decades-as-1-10…
Browse files Browse the repository at this point in the history
…-and-not-0-9

Modify decade grouping
  • Loading branch information
francbartoli authored Jun 6, 2024
2 parents d25665a + b226189 commit 89bfbf4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
18 changes: 13 additions & 5 deletions arpav_ppcv/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
46 changes: 18 additions & 28 deletions tests/notebooks/observation-time-series.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -50,7 +50,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"id": "65b0cbc2-58be-4001-9653-bd0386ae2283",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -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"
}
Expand All @@ -333,7 +333,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 4,
"id": "574e423c-35f1-4c1f-a1eb-5295c8b187df",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -367,40 +367,30 @@
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>1980</th>\n",
" <td>0.845667</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1990</th>\n",
" <td>1.246000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2000</th>\n",
" <td>0.847400</td>\n",
" <th>1990-01-01 00:00:00+00:00</th>\n",
" <td>1.0870</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2010</th>\n",
" <td>1.280200</td>\n",
" <th>2000-01-01 00:00:00+00:00</th>\n",
" <td>0.8325</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020</th>\n",
" <td>1.941400</td>\n",
" <th>2010-01-01 00:00:00+00:00</th>\n",
" <td>1.6243</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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"
}
Expand Down Expand Up @@ -621,7 +611,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.8"
"version": "3.10.14"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 89bfbf4

Please sign in to comment.