pytsdb
An educational time-series database built from scratch in Python. Part of the PyLearn platform for kids aged 12+.
A time-series database stores data points that each have a timestamp, a value, and optional tags -- like a weather station logbook that records temperature readings throughout the day.
Features
- DataPoint -- timestamped values with optional tags (e.g. sensor="kitchen")
- TimeSeries -- ordered collection of data points for one metric
- TimeSeriesDB -- container managing multiple named series with full CRUD
- Time Range Queries -- efficient binary-search lookups between two timestamps
- Tag Filtering -- filter data points by tag key-value pairs
- Aggregation -- compute avg, min, max, sum, count over configurable time windows
- Downsampling -- reduce resolution (e.g. minute data to hourly averages)
- Retention Policies -- auto-delete data older than N days
- Persistence -- save/load the entire database to JSON files
- Rate of Change -- compute how fast a value is changing over time
Quick Start
from datetime import datetime, timedelta, timezone
from pytsdb import AggFunc, TimeSeriesDB
# Create a database and a series
db = TimeSeriesDB()
db.create_series("temperature")
# Add some readings
db.add_point("temperature", value=68.0,
timestamp=datetime(2024, 7, 4, 8, 0, tzinfo=timezone.utc),
tags={"sensor": "kitchen"})
db.add_point("temperature", value=72.5,
timestamp=datetime(2024, 7, 4, 10, 0, tzinfo=timezone.utc),
tags={"sensor": "kitchen"})
db.add_point("temperature", value=75.0,
timestamp=datetime(2024, 7, 4, 12, 0, tzinfo=timezone.utc),
tags={"sensor": "bedroom"})
# Query by time range and tags
kitchen = db.query("temperature", tags={"sensor": "kitchen"})
print(f"Kitchen readings: {len(kitchen)}") # 2
# Aggregate: hourly averages
hourly = db.aggregate("temperature", window=timedelta(hours=1), func=AggFunc.AVG)
# Downsample to 2-hour buckets
coarse = db.downsample("temperature", target_interval=timedelta(hours=2))
# Auto-expire old data
db.set_retention("temperature", max_age=timedelta(days=30))
db.apply_retention()
# Save to disk
from pytsdb import save, load
save(db, "my_data.json")
db2 = load("my_data.json")
Concepts
See the docs/ folder for kid-friendly explanations with real-world analogies:
- What is a Time-Series DB? -- weather station logbook analogy
- Data Points -- thermometer readings
- Time Series -- a week of readings
- Queries -- checking yesterday's weather
- Aggregation -- weekly average temperature
- Downsampling -- monthly summaries
- Retention -- throwing away old newspapers
Documentation
Full docs (with kid-friendly explanations of every idea): https://robcox2910.github.io/pytsdb/
Development
uv sync --all-extras
uv run ruff check src/ tests/
uv run ruff format src/ tests/
uv run pyright src tests
uv run pytest
Releasing
Other projects in the series (PyStack) use a pinned release of this one,
not whatever is on main. To publish a change for them:
- Merge the change to
main. - Tag it:
git tag -a vX.Y.Z -m vX.Y.Z && git push origin vX.Y.Z. A change to the public API bumps the minor version (v0.1.0→v0.2.0); anything else bumps the patch (v0.1.0→v0.1.1). - In each project that uses it, edit the tag in
[tool.uv.sources], runuv lock --upgrade-package <name>, and open a pull request so CI checks the pair together.
Related Projects
This project is one of a series that builds real systems "from scratch in Python" to teach how they work. See the series home for the full story.
| Project | What it teaches | Repository | Docs |
|---|---|---|---|
| PyOS | Operating systems | py-os | Docs |
| Pebble | Compilers and programming languages | pebble-lang | Docs |
| PyDB | Relational databases | pydb | Docs |
| PyKV | Key-value stores | pykv | Docs |
| PyDocDB | Document databases | pydoc-db | Docs |
| PyGraphDB | Graph databases | pygraphdb | Docs |
| PyTSDB | Time-series databases | pytsdb | Docs |
| PyVecDB | Vector databases | pyvecdb | Docs |
| PyColDB | Column-family databases | pycoldb | Docs |
| PyCrypt | Cryptography | pycrypt | Docs |
| PyNet | Computer networking | pynet | Docs |
| PyWeb | HTTP web servers | pyweb | Docs |
| PyGit | Version control | pygit | Docs |
| PySearch | Search engines | pysearch | Docs |
| PyMQ | Message queues | pymq | Docs |
| PyLLM | Language models (the brain) | pyllm | Docs |
| PyScheduler | Job scheduling | pyscheduler | Docs |
| PyStack | Full-stack integration | pystack | Docs |
Metadata
Release files for fromscratch-pytsdb 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| fromscratch_pytsdb-0.4.0.tar.gz | 89.8 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fromscratch_pytsdb-0.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 112.1 kB
Release files / fromscratch_pytsdb-0.4.0.tar.gz
| Download URL | fromscratch_pytsdb-0.4.0.tar.gz |
|---|---|
| Size | 89.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c04f6fe1bbdedc1d07261769048b9f6970e3cd309eb4b2c2940dbad3ea250caf
|
|
BLAKE2b-256 checksum How to use checksums |
b56b864315ce2a87c2231d146c6ad2d0fdeb4ff22bb5c87765f5ef88325c0040
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.
Transparency logRelease files / fromscratch_pytsdb-0.4.0-py3-none-any.whl
| Download URL | fromscratch_pytsdb-0.4.0-py3-none-any.whl |
|---|---|
| Size | 22.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ae16e56ceaee1a5d473c04473e72b131b99216a3aefb3106a5944980ff5354cc
|
|
BLAKE2b-256 checksum How to use checksums |
b81e664b0a1c906f97472574f0a1edef576dae588d10f7f347ddf800b293f832
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.
Transparency logSecurity for fromscratch-pytsdb 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for fromscratch_pytsdb-0.4.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.