snat_sim.utils.time_series

The time_series module extends pandas functionality for manipulating time series data. It is intended support tasks particular to dealing with atmospheric / weather data.

Usage Example

As an example, we create a pandas.Series object with missing data and fill in the missing data using the periodic_interpolation method.

>>> import numpy as np
>>> import pandas as pd

>>> # Create a Series with missing data
>>> demo_series = pd.Series(np.arange(10, 21))
>>> demo_series.iloc[[0, -1]] = np.nan
>>> print(demo_series)
0      NaN
1     11.0
2     12.0
3     13.0
4     14.0
5     15.0
6     16.0
7     17.0
8     18.0
9     19.0
10     NaN
dtype: float64


>>> # Interpolate for the missing data using periodic boundary conditions
>>> print(demo_series.tsu.periodic_interpolation())
0     13.666667
1     11.000000
2     12.000000
3     13.000000
4     14.000000
5     15.000000
6     16.000000
7     17.000000
8     18.000000
9     19.000000
10    16.333333
dtype: float64

For information on what other methods are incorporated under the tsu accessor attribut, see the TSUAccessor class.

Module Docs

snat_sim.utils.time_series.datetime_to_sec_in_year(date)[source]

Calculate number of seconds elapsed modulo 1 year.

Accurate to within a microsecond.

Parameters:

date (Union[datetime, Collection[datetime]]) – Date(s) to calculate seconds for

Return type:

Union[float, float16, float32, float64, float128, int, int8, int16, int32, int64, uint8, uint16, uint32, uint64, longlong, ulonglong, ndarray]

Returns:

A single float if the input is a single datetime, or a numpy array if the input is a collection.

class snat_sim.utils.time_series.TSUAccessor(pandas_obj)[source]

Pandas Series accessor for time series utilities

__init__(pandas_obj)[source]

Extends pandas support for time series data

DO NOT USE THIS CLASS DIRECTLY! This class is registered as a pandas accessor. See the module level usage example for more information.

supplemented_data(year, supp_years=())[source]

Return the supplemented subset of the series corresponding to a given year

Data for the given year is supplemented with any available data from supplementary years by asserting that the measured values from supplementary years are exactly the same as they would be if taken during the primary year. Priority is given to supplementary years in the order specified by the supp_years argument.

Parameters:
  • year (int) – Year to supplement data for

  • supp_years (Collection[int]) – Years to supplement data with when missing from year

Return type:

Series

Returns:

A pandas Series object

periodic_interpolation()[source]

Linearly interpolate the series using periodic boundary conditions

Similar to the default linear interpolation used by pandas, but missing values at the beginning and end of the series are interpolated assuming a periodic boundary condition.

Return type:

Series

Returns:

An interpolated copy of the passed series

resample_data_across_year()[source]

Resample the series evenly from the beginning of the earliest year through the end of the latest year.

Return type:

Series

Returns:

A copy of the passed series interpolated for January first through December 31st