A NumPy array of time series data provides a fast, memory-efficient way to store and manipulate sequential measurements indexed by time — ideal for financial data, sensor readings, weather records, stock prices, IoT streams, or any ordered temporal series. In 2026, NumPy remains the foundation for time series handling: use numpy.datetime64 or numpy.timedelta64 for time indices, structured arrays for multiple variables, or 2D arrays with separate time column for simple cases. NumPy enables vectorized operations (resampling, rolling windows, FFT, differencing), broadcasting, and seamless integration with pandas (for higher-level time series), Polars (for columnar speed), xarray (for labeled multi-dimensional time series), and Dask (for out-of-core scale) — making it the perfect starting point for analysis, forecasting, anomaly detection, or feature engineering.
Here’s a complete, practical guide to creating and computing with NumPy arrays of time series data: time index creation, structured vs regular arrays, indexing/slicing by time, basic operations (resampling, rolling, differencing), aggregation/statistics, real-world patterns, and modern best practices with type hints, memory optimization, and integration with pandas/Polars/xarray/Dask.
Creating time series arrays — use datetime64 for time index, regular or structured arrays for values.
import numpy as np
# Daily time index (datetime64[D])
dates = np.arange('2022-01-01', '2022-01-11', dtype='datetime64[D]')
print(dates)
# ['2022-01-01' '2022-01-02' ... '2022-01-10']
# 2D array: time × features (rows = time steps, columns = variables)
values = np.array([
[1.2, 10.5],
[2.3, 11.2],
[3.4, 12.8],
[4.5, 13.9],
[5.6, 15.1],
[6.7, 16.4],
[7.8, 17.7],
[8.9, 18.9],
[9.0, 20.1],
[10.1, 21.3]
])
# Structured array: named fields + time index
dtype = [('date', 'datetime64[D]'), ('temp', 'float32'), ('pressure', 'float32')]
ts_struct = np.empty(len(dates), dtype=dtype)
ts_struct['date'] = dates
ts_struct['temp'] = values[:, 0]
ts_struct['pressure'] = values[:, 1]
print(ts_struct[:3])
# [('2022-01-01', 1.2, 10.5) ('2022-01-02', 2.3, 11.2) ('2022-01-03', 3.4, 12.8)]
Indexing & slicing by time — use boolean masks or searchsorted for efficient access.
# Boolean mask: after 2022-01-05
mask = ts_struct['date'] > np.datetime64('2022-01-05')
print(ts_struct[mask]['temp']) # temps after Jan 5
# Find index for specific date
idx = np.searchsorted(ts_struct['date'], np.datetime64('2022-01-07'))
print(ts_struct[idx]) # data on Jan 7
Basic operations — vectorized differencing, rolling means, resampling (via pandas for simplicity).
# First differences (change from previous)
diff_temp = np.diff(ts_struct['temp'])
print(diff_temp[:3]) # [1.1 1.1 1.1]
# Simple moving average (window=3)
ma_temp = np.convolve(ts_struct['temp'], np.ones(3)/3, mode='valid')
print(ma_temp[:3]) # [2.3 3.4 4.5]
# Resample to weekly (use pandas for ease)
import pandas as pd
df = pd.DataFrame(ts_struct)
df.set_index('date', inplace=True)
weekly = df.resample('W').mean()
print(weekly)
Best practices for NumPy time series arrays. Use datetime64 consistently — avoid Python datetime objects in arrays. Modern tip: prefer pandas DatetimeIndex or Polars pl.Datetime — better time zone handling, resampling, and plotting; convert NumPy to pandas with pd.DataFrame(ts_struct). Use structured arrays — named fields improve readability. Vectorize everything — avoid loops; use np.diff, np.convolve, np.cumsum. Add type hints — def process_ts(arr: np.ndarray[('date', 'datetime64[D]'), ('value', 'float64')]) -> np.ndarray. Monitor memory — ts_struct.nbytes for raw size. Use views — slicing returns views (no copy). Use np.ascontiguousarray — ensure C-order for speed. Use np.searchsorted — fast time-based lookup. Resample/aggregate in pandas/Polars — easier than manual NumPy. Use xarray — for labeled multi-dimensional time series. Use dask.array — chunked NumPy for out-of-core scale. Profile with timeit — compare vectorized vs looped ops. Use matplotlib/seaborn — plot directly from NumPy arrays.
Computing with NumPy arrays of time series data uses datetime64 indices, structured/regular arrays, vectorized ops, and aggregation for fast temporal analysis. In 2026, use NumPy for core speed, pandas/Polars for time-aware resampling, xarray for labeled multi-D, Dask for scale, and monitor memory with nbytes. Master NumPy time series, and you’ll handle sequential data efficiently, scalably, and with full vectorized power.
Next time you work with time-indexed measurements — use NumPy arrays. It’s Python’s cleanest way to say: “Store and compute time series — fast and structured.”