|
| 1 | +.. py:currentmodule:: datareservoirio |
| 2 | +
|
| 3 | +Access time series |
| 4 | +================== |
| 5 | + |
| 6 | +Access existing data |
| 7 | +------------------------------------- |
| 8 | + |
| 9 | +You can access any data for which you have the ``TimeSeriesId`` (and authorization). It is possible to |
| 10 | +query aggregated data directly, e.g. you can query 1 minute average values for a specified period. The finest available aggregation period |
| 11 | +is *"tick"* (100 nanoseconds). |
| 12 | + |
| 13 | + |
| 14 | +.. code-block:: python |
| 15 | + |
| 16 | + # set up |
| 17 | + import datareservoirio as drio |
| 18 | + import numpy as np |
| 19 | + import pandas as pd |
| 20 | +
|
| 21 | + auth = drio.Authenticator() |
| 22 | + # Follow instructions to authenticate |
| 23 | +
|
| 24 | + client = drio.Client(auth) |
| 25 | +
|
| 26 | + # Get timeseries data resampled to 15 minutes average for selected period |
| 27 | + timeseries = client.get_samples_aggregate(series_id, |
| 28 | + start='2024-01-01', end='2024-01-02', |
| 29 | + aggregation_period='15m', |
| 30 | + aggregation_function='mean') |
| 31 | +
|
| 32 | + # Get all data for selected time period |
| 33 | + timeseries = client.get_samples_aggregate(series_id, |
| 34 | + start='2024-01-01', end='2024-01-02', |
| 35 | + aggregation_period='tick', |
| 36 | + aggregation_function='mean') |
| 37 | +
|
| 38 | +.. note:: |
| 39 | + |
| 40 | + :py:meth:`Client.get_samples_aggregate` returns a :py:class:`pandas.Series`. The :py:mod:`start`, :py:mod:`end`, :py:mod:`aggregation_period` and :py:mod:`aggregation_function` parameters are required. |
| 41 | + |
| 42 | +.. important:: |
| 43 | + |
| 44 | + Time series data is archived 90 days after the upload. To access archived data directly, you can use |
| 45 | + the :py:meth:`Client.get` method, but the data can also be restored by contacting :ref:`support <support>`. |
| 46 | + |
| 47 | + |
| 48 | +Access archived data |
| 49 | +-------------------- |
| 50 | +You can access time series data using the :py:meth:`Client.get` method, as long as you have |
| 51 | +the ``TimeSeriesId`` (and authorization). Note that this method only returns the raw data, and |
| 52 | +does not support aggregation. Below is an example demonstrating how to access archived time |
| 53 | +series data. We strongly recommended to use the |
| 54 | +:py:meth:`Client.get_samples_aggregate` as long as the data was uploaded within the last 90 days, |
| 55 | +or contact support to restore it. |
| 56 | + |
| 57 | +.. code-block:: python |
| 58 | +
|
| 59 | + # Get entire timeseries |
| 60 | + timeseries = client.get(series_id) |
| 61 | +
|
| 62 | + # Get a slice of time series |
| 63 | + timeseries = client.get(series_id, start='2018-01-01 12:00:00', |
| 64 | + end='2018-01-02 06:00:00') |
| 65 | +
|
| 66 | +
|
| 67 | +.. warning:: |
| 68 | + |
| 69 | + The time resolution of aggregated data is in ticks (1tick = 100 nanoseconds), while the time resolution of non-aggregated data is in nanoseconds. This may lead to discrepancies in data when comparing the two, and some datapoints might get lost when using aggregation to access data, in cases when there are multiple datapoints within the same 100 nanosecond range. |
| 70 | + |
| 71 | + |
| 72 | +.. tip:: |
| 73 | + When handling high-frequency data and/or extended timespans, it is crucial to consider memory usage. |
| 74 | + Accessing an excessive amount of data at once can cause your script to fail. The following is a recommended approach for accessing data in smaller chunks: |
| 75 | + |
| 76 | + .. code-block:: python |
| 77 | +
|
| 78 | + # Make a date iterator |
| 79 | + start_end = pd.date_range(start="2020-01-01 00:00", end="2020-02-01 00:00", freq="1H") |
| 80 | + start_end_iter = zip(start_end[:-1], start_end[1:]) |
| 81 | +
|
| 82 | + series_id = <your time series ID> |
| 83 | +
|
| 84 | +
|
| 85 | + # Get timeseries in chunks |
| 86 | + for start, end in start_end_iter: |
| 87 | + timeseries = client.get(series_id, start=start, end=end) |
| 88 | +
|
| 89 | +
|
| 90 | +.. _DataReservoir.io: https://www.datareservoir.io/ |
| 91 | +.. _Pandas: https://pandas.pydata.org/ |
0 commit comments