Reshaping time series data in NumPy rearranges sequential measurements (e.g., time × features) into new shapes for analysis, visualization, or modeling — without changing the underlying data order. Reshaping is metadata-only (views when possible), enabling efficient pivoting, stacking, transposing, or splitting multi-variate series into separate arrays. In 2026, reshaping remains core for time series preprocessing — converting wide to long format, preparing inputs for LSTM/Transformer models, aligning multi-sensor data, or creating rolling windows — while integrating with pandas (for time-aware reshaping), Polars (for columnar speed), xarray (for labeled multi-D), and Dask (for out-of-core scale).
Here’s a complete, practical guide to reshaping time series data with NumPy: basic reshape, transposing, stacking/unstacking, real-world patterns (wide to long, multi-variate to channels, rolling windows), and modern best practices with type hints, memory views, pandas/Polars/xarray equivalents, and performance tips.
Basic reshaping — change shape while preserving order; use -1 to infer dimension.
import numpy as np
# Sample time series: 10 days × 2 variables
dates = np.arange('2022-01-01', '2022-01-11', dtype='datetime64[D]')
data = 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]
])
# Reshape to 2 × 5 × 2 (e.g., two groups of 5 days)
reshaped = data.reshape(2, 5, 2)
print(reshaped.shape) # (2, 5, 2)
# Reshape to 5 × 2 × 2 (infer -1)
reshaped_inf = data.reshape(-1, 2, 2) # (5, 2, 2)
print(reshaped_inf)
Transposing — swap axes for different views (lazy, no copy).
# Transpose to variables × time (2 × 10)
transposed = data.T
print(transposed.shape) # (2, 10)
# Transpose 3D: (time, features, batch) ? (batch, time, features)
three_d = data.reshape(5, 2, 2) # e.g., 5 samples × 2 time steps × 2 features
trans_3d = three_d.transpose(2, 0, 1) # batch, time, features
print(trans_3d.shape) # (2, 5, 2)
Real-world pattern: wide to long format — reshape multi-variate time series for modeling or plotting.
# Wide: time × variables
wide = data # 10 × 2
# Long: time × variable × value (stacked)
long = wide.reshape(-1, 1) # flatten to (20, 1), then add labels
# Better: use pandas melt for labeled long format
import pandas as pd
df_wide = pd.DataFrame(wide, index=dates, columns=['temp', 'pressure'])
df_long = df_wide.melt(ignore_index=False, var_name='variable', value_name='value')
print(df_long.head())
Best practices for reshaping time series in NumPy. Use views — reshaping/transposing/slicing return views (no copy, save memory). Modern tip: prefer pandas .stack()/.unstack() or Polars .melt()/.pivot() — time-aware, labeled, easier for wide ? long. Preserve time index — keep datetime64 separate or use structured arrays. Use -1 — infer dimensions safely. Avoid copies — use .reshape() instead of .copy() unless needed. Add type hints — def reshape_ts(arr: np.ndarray[np.float64, (None, None)]) -> np.ndarray[np.float64, (None, None, None)]. Monitor memory — arr.nbytes before/after reshape. Use np.ascontiguousarray — ensure C-order for speed after reshape. Use np.rollaxis or np.swapaxes — alternative axis manipulation. Use xarray — da.stack()/.transpose() with labels. Use Dask arrays — da.reshape()/.transpose() for out-of-core. Test reshaping — assert reshaped.ravel().equals(original.ravel()). Use np.newaxis — add dimensions for broadcasting. Profile with timeit — compare reshape vs manual loops.
Reshaping time series data with NumPy rearranges temporal measurements — reshape, transpose, stack/unstack — for modeling, visualization, or analysis. In 2026, use NumPy for core speed, pandas/Polars for labeled reshaping, xarray for multi-D time series, Dask for scale, and preserve views. Master reshaping, and you’ll transform sequential data efficiently and intuitively for any downstream task.
Next time your time series needs a new shape — reshape it with NumPy. It’s Python’s cleanest way to say: “Rearrange my data — keep the order, change the view.”