.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/times/times_temporal_matrices.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_times_times_temporal_matrices.py: TEMPORAL MATRICES: YEAR × MONTH REPRESENTATIONS =============================================== Convert time series to year × month matrix for pattern analysis and visualization. Part of the diive library: https://github.com/holukas/diive .. GENERATED FROM PYTHON SOURCE LINES 11-13 Load example data ^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 13-23 .. code-block:: Python import diive as dv df = dv.load_exampledata_parquet() series = df['Tair_f'].copy() print("Example data loaded:") print(f" Records: {len(series)}") print(f" Years: {series.index[0].year} to {series.index[-1].year}") .. rst-class:: sphx-glr-script-out .. code-block:: none > Loaded .parquet file /home/docs/checkouts/diive/checkouts/latest/diive/configs/exampledata/exampledat a_PARQUET_CH-DAV_FP2022.5_2013-2022_ID20230206154316_30MIN.parquet (0.050 seconds). Example data loaded: Records: 175296 Years: 2013 to 2022 .. GENERATED FROM PYTHON SOURCE LINES 24-26 Create monthly aggregation matrix ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 26-35 .. code-block:: Python monthly_mean = dv.times.resample_to_monthly_agg_matrix(series=series, agg='mean') print("\nMonthly mean matrix:") print(f" Shape: {monthly_mean.shape} (years × months)") print(f" Years: {monthly_mean.index.min()} to {monthly_mean.index.max()}") print(f"\nFirst 5 years:") print(monthly_mean.head()) .. rst-class:: sphx-glr-script-out .. code-block:: none Monthly mean matrix: Shape: (10, 12) (years × months) Years: 2013 to 2022 First 5 years: MONTH 1 2 3 ... 10 11 12 YEAR ... 2013 -4.127289 -7.613169 -1.802983 ... 6.455432 -1.454567 -0.418102 2014 -1.758389 -1.760185 1.261626 ... 6.744112 4.176977 -1.852366 2015 -3.424022 -4.642639 0.013280 ... 4.600675 3.170463 2.282356 2016 -2.897780 -1.724999 -1.704113 ... 3.805754 0.864235 0.304792 2017 -6.134009 -0.702418 2.126041 ... 5.936708 -1.048244 -4.428223 [5 rows x 12 columns] .. GENERATED FROM PYTHON SOURCE LINES 36-38 Analyze patterns ^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 38-62 .. code-block:: Python print("\n" + "=" * 60) print("Seasonal and yearly patterns") print("=" * 60) # Seasonal cycle monthly_avg = monthly_mean.mean(axis=0) month_names = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] print("\nMonthly averages:") for month, temp in enumerate(monthly_avg, 1): print(f" {month_names[month - 1]}: {temp:.2f}°C") # Extremes flat_matrix = monthly_mean.values.flatten() max_idx = flat_matrix.argmax() min_idx = flat_matrix.argmin() max_year, max_month = divmod(max_idx, 12) min_year, min_month = divmod(min_idx, 12) print(f"\nWarmest: {monthly_mean.index[max_year]} {month_names[max_month]} ({monthly_mean.iloc[max_year, max_month]:.2f}°C)") print(f"Coldest: {monthly_mean.index[min_year]} {month_names[min_month]} ({monthly_mean.iloc[min_year, min_month]:.2f}°C)") .. rst-class:: sphx-glr-script-out .. code-block:: none ============================================================ Seasonal and yearly patterns ============================================================ Monthly averages: Jan: -3.60°C Feb: -2.73°C Mar: -0.26°C Apr: 3.45°C May: 6.52°C Jun: 11.62°C Jul: 13.35°C Aug: 12.76°C Sep: 8.93°C Oct: 6.00°C Nov: 1.36°C Dec: -1.32°C Warmest: 2015 Jul (15.58°C) Coldest: 2013 Feb (-7.61°C) .. GENERATED FROM PYTHON SOURCE LINES 63-67 Matrix with ranking ^^^^^^^^^^^^^^^^^^^ Percentile ranking within each month (100=warmest, 0=coldest). .. GENERATED FROM PYTHON SOURCE LINES 67-76 .. code-block:: Python monthly_rank = dv.times.resample_to_monthly_agg_matrix(series=series, agg='mean', ranks=True) print("\n" + "=" * 60) print("Monthly ranking") print("=" * 60) print(f"\nJanuary rankings (example):") print(monthly_rank.iloc[:, 0]) .. rst-class:: sphx-glr-script-out .. code-block:: none ============================================================ Monthly ranking ============================================================ January rankings (example): YEAR 2013 7.0 2014 3.0 2015 6.0 2016 5.0 2017 9.0 2018 2.0 2019 10.0 2020 1.0 2021 8.0 2022 4.0 Name: 1, dtype: float64 .. GENERATED FROM PYTHON SOURCE LINES 77-79 Alternative aggregations ^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 79-99 .. code-block:: Python print("\n" + "=" * 60) print("Other aggregation methods") print("=" * 60) monthly_sum = dv.times.resample_to_monthly_agg_matrix(series=series, agg='sum') monthly_max = dv.times.resample_to_monthly_agg_matrix(series=series, agg='max') monthly_min = dv.times.resample_to_monthly_agg_matrix(series=series, agg='min') methods = { 'mean': monthly_mean, 'sum': monthly_sum, 'max': monthly_max, 'min': monthly_min, } print("Available aggregations:") for method, matrix in methods.items(): val = matrix.iloc[0, 0] print(f" {method:>6}: {val:7.2f} (Jan, year 1)") .. rst-class:: sphx-glr-script-out .. code-block:: none ============================================================ Other aggregation methods ============================================================ Available aggregations: mean: -4.13 (Jan, year 1) sum: -6141.41 (Jan, year 1) max: 7.80 (Jan, year 1) min: -17.50 (Jan, year 1) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.142 seconds) .. _sphx_glr_download_auto_examples_times_times_temporal_matrices.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: times_temporal_matrices.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: times_temporal_matrices.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: times_temporal_matrices.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_