---
jupytext:
  text_representation:
    format_name: myst
kernelspec:
  display_name: Python 3 (ipykernel)
  language: python
  name: python3
---

# Time-series data

{.lead}
Time-series data is a list of values, each with the time it was measured. This page covers how channels with different timing line up, and how to draw a long recording without losing its spikes.

{download}`Download this page as a notebook <time-series-data.md>` to run its code in JupyterLab. [Running the notebooks](notebooks/running.md) explains how to set it up.

## Channels, timestamps and values

A recording is made of {gloss}`channels <channel>`, each a series of timestamped values. A Nominal dataset stores time-series data, and each channel in it has a data type, and can have a unit and a description.[^nominal-glossary] Channels don't share timestamps: a pressure may update 1,000 times a second and a flow 2 times a second, and a value that arrives late carries a later timestamp. To compare two channels, you align them first.

## Aligning channels

This code builds two made-up channels with different timing: a temperature at irregular times, and a flow at a steady 2 Hz. It puts both on one list of timestamps, the times of either channel, and fills each gap with that channel's last value:

```{code-cell} ipython3
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from learning.style import set_matplotlib_style

set_matplotlib_style()
```

```{code-cell} ipython3
rng = np.random.default_rng(5)
temp_s = np.sort(rng.uniform(0, 10, 25))  # s: irregular times
temperature = pd.Series(
    20 + 0.3 * temp_s + rng.normal(0, 0.05, temp_s.size),
    index=pd.to_timedelta(temp_s, unit="s"),
    name="temperature",
)
flow_s = np.arange(0, 10, 0.5)  # s: a steady 2 Hz
flow = pd.Series(
    4 + np.sin(flow_s), index=pd.to_timedelta(flow_s, unit="s"), name="flow"
)

aligned = pd.concat([temperature, flow], axis=1).sort_index().ffill()
print(f"{len(temperature)} + {len(flow)} readings give {len(aligned)} aligned rows")

fig, ax = plt.subplots()
ax.plot(
    aligned.index.total_seconds(), aligned["temperature"], drawstyle="steps-post"
)
ax.set_xlabel("Time (s)")
ax.set_ylabel("Temperature (°C)")
plt.show()
aligned.head(6).round(2)
```

Filling a gap with the last value assumes the channel didn't change in between, which holds for a setpoint and may not hold for a measurement. Whether it's right depends on how fast the signal moves compared with how often it's read.

## Drawing a long recording

A chart shows only as much detail as it has pixels. In an example from high-tech manufacturing, one chart of the last 12 hours of a 100 Hz sensor needs over 4 million rows. The M4 method keeps four values for each pixel column of the chart: the minimum, the maximum, the first and the last. From at most four values per pixel column, it draws the same line chart as the raw data, where averaging each column "significantly changes the actual shape of the time series".[^m4]

This code reduces an hour of a 100 Hz signal with one short spike to 600 buckets, one for each pixel column of a chart. It keeps each bucket's mean, and each bucket's minimum and maximum:

```{code-cell} ipython3
rng = np.random.default_rng(7)
raw = rng.normal(0, 0.2, 360_000)  # an hour at 100 Hz
raw[123_456] += 5.0  # one 10 ms spike
buckets = raw.reshape(600, -1)  # 600 pixel columns of 600 samples each

fig, (top, bottom) = plt.subplots(2, 1, sharex=True)
x = np.arange(600)
top.plot(x, buckets.mean(axis=1))
top.set_ylabel("Mean")
bottom.fill_between(x, buckets.min(axis=1), buckets.max(axis=1))
bottom.set_ylabel("Min to max")
bottom.set_xlabel("Chart column")
plt.show()
print(
    f"raw maximum: {raw.max():.1f}, largest bucket mean: {buckets.mean(axis=1).max():.1f}"
)
```

The means flatten the spike into the noise, and the minimum and maximum keep it. Use the means to see a trend, and the extremes to see events.

## In Nominal

Upload the aligned or raw data with its timestamp column, as in [Introduction to measurement](introduction-to-measurement.md#in-nominal). Don't align or reduce the data before you upload it, so the dataset keeps every reading. To chart a long recording, Nominal's `channel_to_dataframe_decimated()` reads a fixed number of time buckets, each with the minimum, maximum, mean, count and variance of its values, so spikes stay visible. Zooming in requests new buckets for the visible range, until the chart shows raw values.[^nominal-decimate]

```python
from nominal.core import NominalClient

client = NominalClient.from_profile("default")
dataset = client.create_dataset("Heater recording")

# readings.csv has a "timestamp" column in ISO 8601, then one column for each channel
dataset.add_tabular_data("readings.csv", timestamp_column="timestamp", timestamp_type="iso_8601")
```

[Decimate data](https://dev.nominal.io/python/guides/datasets/decimating-data/) shows how to read the buckets with the Python client.

## Further reading

- [Engineering data and warehousing](engineering-data.md), on file formats and where time-series data lives.
- U. Jugel et al., [M4: A Visualization-Oriented Time Series Data Aggregation](https://www.vldb.org/pvldb/vol7/p797-jugel.pdf).
- Nominal, [Data model core concepts](https://dev.nominal.io/platform/data-model/).

## References

[^nominal-glossary]: Nominal, ["Glossary"](https://dev.nominal.io/platform/reference/glossary/), Dataset entry. Retrieved 2026-11-24.
[^m4]: U. Jugel, Z. Jerzak, G. Hackenbroich and V. Markl, ["M4: A Visualization-Oriented Time Series Data Aggregation"](https://www.vldb.org/pvldb/vol7/p797-jugel.pdf), *Proceedings of the VLDB Endowment* 7(10), 2014, sections 1, 2 and 4. Retrieved 2026-11-24.
[^nominal-decimate]: Nominal, ["Decimate data"](https://dev.nominal.io/python/guides/datasets/decimating-data/), Python client guide. Retrieved 2026-11-24.
