README.md 5.82 KB
Newer Older
1
2
TSHISTORY
===========
Aurélien Campéas's avatar
Aurélien Campéas committed
3
4
5
6

This is a library to store/retrieve pandas timeseries to/from a
postgres database, tracking their successive versions.

7
8
[TOC]

Aurélien Campéas's avatar
Aurélien Campéas committed
9
10
11
12
13
14
15
16

# Principles

There are many ways to represent timeseries in a relational database,
and `tshistory` provides two things:

* a base python API which abstracts away the underlying storage

17
* a postgres model, which uses BYTEA fields to store chunks of the
Aurélien Campéas's avatar
Aurélien Campéas committed
18
19
20
21
22
23
24
25
  series data.

The core idea of tshistory is to handle successive versions of
timeseries as they grow in time, allowing to get older states of any
series.

Series state can be indexed by either a timestamp (which typically
matches the moment a new insertion took place) or a `changeset id`
Aurélien Campéas's avatar
Aurélien Campéas committed
26
which is a numeric index denoting the exact change leading to a given
27
version.
Aurélien Campéas's avatar
Aurélien Campéas committed
28
29


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Starting with a fresh database

You need a postgresql database. You can create one like this:

```shell
 createdb mydb
```

Then, initialize the `tshistory` tables, like this:

```python
 tsh init-db postgresql://me:password@localhost/mydb
```

From this you're ready to go !


Aurélien Campéas's avatar
Aurélien Campéas committed
47
48
49
# Basic usage


50
However here's a simple example:
Aurélien Campéas's avatar
Aurélien Campéas committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64

```python
 from datetime import datetime
 from sqlalchemy import create_engine
 import pandas as pd
 from tshistory.tsio import TimeSerie

 engine = create_engine('postgres://me:password@localhost/mydb')
 tsh = TimeSerie()

 serie = pd.Series([1, 2, 3],
                  pd.date_range(start=datetime(2017, 1, 1),
                                freq='D', periods=3))
 # db insertion
Aurélien Campéas's avatar
Aurélien Campéas committed
65
 tsh.insert(engine, serie, 'my_serie', 'babar@pythonian.fr')
Aurélien Campéas's avatar
Aurélien Campéas committed
66

67
68
69
70
71
72
73
74
 # it looks like this:
 assert """
2017-01-01    1.0
2017-01-02    2.0
2017-01-03    3.0
Freq: D, dtype: float64
""".strip() == serie.to_string().strip()

Aurélien Campéas's avatar
Aurélien Campéas committed
75
76
77
78
79
80
81
82
83
84
85
 # db retrieval
 assert tsh.get(engine, 'my_serie') == serie
```

This is good. Now, let's add more:

```python
 serie = pd.Series([7, 8, 9],
                  pd.date_range(start=datetime(2017, 1, 3),
                                freq='D', periods=3))
 # db insertion
Aurélien Campéas's avatar
Aurélien Campéas committed
86
 tsh.insert(engine, serie, 'my_serie', 'babar@pythonian.fr')
Aurélien Campéas's avatar
Aurélien Campéas committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

 # db retrieval
 stored = tsh.get(engine, 'my_serie')

 assert """
2017-01-01    1
2017-01-02    2
2017-01-03    7
2017-01-04    8
2017-01-04    9
Freq: D
""".strip() == stored.to_string().strip()
```

It is important to note that the third value was replaced, and the two
last values were just appended.

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
We can access the whole history (or parts of it) in one call:

```python
 history = tsh.get_history(engine, 'my_serie')

 assert """
insertion_date              value_date
2017-11-20 15:29:35.210535  2017-01-01    1.0
                            2017-01-02    2.0
                            2017-01-03    3.0
2017-11-20 15:32:25.160935  2017-01-01    1.0
                            2017-01-02    2.0
                            2017-01-03    7.0
                            2017-01-04    8.0
                            2017-01-05    9.0
""".strip() == history.to_string().strip()
```

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
Note how this shows the full serie state for each insertion date. It
is possible to show the differences only:

```python
 diffs = tsh.get_history(engine, 'my_serie', diffmode=True)

 assert """
insertion_date              value_date
2017-11-20 15:29:35.210535  2017-01-01    1.0
                            2017-01-02    2.0
                            2017-01-03    3.0
2017-11-20 15:32:25.160935  2017-01-03    7.0
                            2017-01-04    8.0
                            2017-01-05    9.0
""".strip() == diffs.to_string().strip()
```
138

139
# Command line
140
141


142
143
## Basic operations

144
145
146
147
A command line tool is provided, called `tsh`. It provides its usage
guidelines:

```shell
148
 $ tsh
149
150
151
152
153
 Usage: tsh [OPTIONS] COMMAND [ARGS]...

 Options:
   --help  Show this message and exit.

154
155
156
157
158
159
160
161
162
Commands:
  dump     dump all time series revisions in a zip file
  get      show a serie in its current state
  history  show a serie full history
  info     show global statistics of the repository
  init-db  initialize an new db.
  log      show revision history of entire repository or...
  restore  restore zip file in a freshly initialized...
  view     visualize time series through the web
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
```

`Info` provides an overview of the time series repository (number of
committed changes, number and series and their names).

```shell
 $ tsh info postgres://babar:babarpassword@dataserver:5432/banana_studies
 changeset count: 209
 series count:    144
 series names:    banana_spot_price, banana_trades, banana_turnover
```

`Log` provides the full history of editions to time series in the
repository.

```shell
 $ tsh log postgres://babar:babar@dataserver:5432/banana_studies --limit 3
 revision: 206
 author:   BABAR
 date:     2017-06-06 15:32:51.502507
 series:   banana_spot_price

 revision: 207
 author:   BABAR
 date:     2017-06-06 15:32:51.676507
 series:   banana_trades

 revision: 209
 author:   CELESTE
 date:     2017-06-06 15:32:51.977507
 series:   banana_turnover
```

All options of all commands can be obtained by using the `--help`
switch:

```shell
 $ tsh log --help
 Usage: tsh log [OPTIONS] DB_URI

 Options:
   -l, --limit TEXT
   --show-diff
   -s, --serie TEXT
   --from-rev TEXT
   --to-rev TEXT
   --help            Show this message and exit.
```
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231


## Extensions

It is possible to augment the `tsh` command with new subcommands (or
augment, modify existing commands).

Any program doing so must define a new command and declare a setup
tools entry point named `tshistory:subcommand` as in e.g.:

```python

    entry_points={'tshistory.subcommands': [
        'view=tsview.command:view'
    ]}
```

For instance, the [tsview][tsview] python package provides such a
`view` subcommand for generic time series visualisation.

[tsview]: https://bitbucket.org/pythonian/tsview