Registry / ai-ml / dtw-python

dtw-python

JSON →
library1.7.5pypypi✓ verified 87d ago

dtw-python is a comprehensive Python library that provides a faithful, functionally equivalent implementation of Dynamic Time Warping (DTW) algorithms, mirroring the capabilities of the popular R 'dtw' package. It enables optimal alignment between two time series, even if they have different lengths or time axes, and supports various local (slope) and global (window) constraints. The library is currently at version 1.7.4 and maintains an active, though irregular, release cadence, with several updates per year. It is widely used for classification and clustering tasks across domains like bioinformatics, econometrics, and general time series analysis.

pip install dtw-python
INSTALL
IMPORT
SIG · DTW-PYTHON
D
dtw-python
ai-mlpythonv1.7.5
Install
9.3s avg
Import
1503ms
Disk
318MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.7.5 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.940 runs
build_error
glibc
py 3.103.940 runs
installs and imports cleanly · install 9.3s · import 1.503s · 310MB
318MB installed
● package 318MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

dtw
import dtw
from dtw-python import dtw
The package name for pip is 'dtw-python', but the import statement is simply 'import dtw'.
*
from dtw import *
Common for interactive use, imports core functions like `dtw` and `rabinerJuangStepPattern` directly.

This quickstart demonstrates how to compute Dynamic Time Warping between two NumPy arrays. It defines a query and a template time series, then uses the `dtw()` function to find their optimal alignment. The resulting `alignment` object contains the DTW distance and can be used for plotting the warping path (if `matplotlib` is installed).

import numpy as np from dtw import * # Create two noisy sine waves idx = np.linspace(0, 6.28, num=100) query = np.sin(idx) + np.random.uniform(size=100) / 10.0 template = np.cos(idx) # Compute DTW alignment alignment = dtw(query, template, keep_internals=True) # Print the distance print(f"DTW Distance: {alignment.distance:.2f}") # Optionally, visualize the alignment (requires matplotlib) try: import matplotlib.pyplot as plt alignment.plot(type="threeway") plt.title("DTW Alignment") plt.show() except ImportError: print("Matplotlib not installed. Install with `pip install dtw-python[plots]` to enable plotting.")
Debug
Known issues
gotchaIndexing in `dtw-python` is 0-based, consistent with Python conventions. This differs from the 1-based indexing used in the R 'dtw' package, which can be a source of error when porting code or comparing results directly.
fix
Adjust all indices (e.g., `index1`, `index2`, `index1s`, `index2s` attributes of alignment objects) to be 0-based when working in Python.
affects: All versions
gotchaFunction argument names in Python use underscores (e.g., `keep_internals`) while the R version commonly uses dots (e.g., `keep.int`). Python does not accept abbreviated argument names.
fix
Use Pythonic underscore-separated full argument names in function calls (e.g., `dtw(..., keep_internals=True)`).
affects: All versions
gotchaPlotting functionality, such as `alignment.plot()`, relies on `matplotlib`. If `matplotlib` is not installed, these methods will raise an `ImportError` or fail silently.
fix
Install `matplotlib` separately (`pip install matplotlib`) or install `dtw-python` with the `plots` extra: `pip install dtw-python[plots]`.
affects: All versions
gotchaThe `dtw` function may return `NaN` values in the cost matrix, particularly when using certain `dist_method` arguments like 'cosine' with inappropriate input data. This can lead to `ParameterError: DTW cost matrix C has NaN values.`
fix
Review your `dist_method` choice and input data. Consider using a more robust distance metric like 'euclidean' if `NaN`s appear unexpectedly, or preprocess data to handle potential division by zero or undefined values if using cosine distance.
affects: All versions
gotchaUncommon build issues related to `undefined symbol: alloca` or `C99 mode` can arise when compiling from source on older systems or with outdated compilers.
fix
Ensure your system's C compiler is up-to-date. If using Conda, `conda install gcc_linux-64` before `pip install dtw-python` may resolve the issue.
affects: All versions (when building from source)
Errors
Common errors & fixes
ImportError: cannot import name 'dtw' from 'dtw-python'
Attempting to import `dtw` directly from a module named `dtw-python`, which is the PyPI package name but not the internal module name.
fix
The correct import statement is `import dtw` or `from dtw import dtw` (or `from dtw import *`). The package name `dtw-python` is for `pip install`.
librosa.util.exceptions.ParameterError: DTW cost matrix C has NaN values.
The local distance function (specified by `dist_method`) produced `NaN` values, often due to inputs incompatible with the chosen metric (e.g., zero vectors with cosine distance).
fix
Inspect the input time series for `NaN`s, `inf`s, or problematic values (e.g., all zeros if using cosine distance). Consider changing the `dist_method` parameter to a more numerically stable option like 'euclidean' if the current metric is not essential.
Unexpected DTW distance or path results compared to R's 'dtw' package or other Python DTW libraries.
Differences often stem from varying default step patterns, local distance metrics, or windowing functions used across implementations. `dtw-python` defaults to `symmetric2` step pattern.
fix
Explicitly set the `step_pattern`, `dist_method`, and `window_type` arguments in `dtw-python` to match the configuration used in the other library for a fair comparison. Pay attention to 0-based vs 1-based indexing conventions.
Upgrade
Version history
1.7.5latest on PyPI · released Jun 12, 2026
Audit
Dependencies
numpyrequiredCore dependency for numerical operations and array handling.
scipyrequiredUsed for distance calculations (e.g., `scipy.spatial.distance.cdist`) and other scientific computing tasks. Pre-installing can speed up `dtw-python` installation.
matplotliboptionalRequired for plotting functions like `alignment.plot()`. Included with `dtw-python[plots]` installation.
Agent activity
27 hits · last 30 days
node
26
OpenAI (training)
1
Resources
dtw-python — pip install dtw-python · libregistry