|
| 1 | +# How to Map Time |
| 2 | +This recipe demonstrates how to map a table's time configuration from one type to another. |
| 3 | + |
| 4 | +**Source table**: data is stored in representative time where there is one week of data per month by |
| 5 | +hour for one year. |
| 6 | + |
| 7 | +**Destination table**: data is stored with `datetime` timestamps for each hour of the year. |
| 8 | + |
| 9 | +**Workflow**: |
| 10 | +- Add the source table to the database. |
| 11 | +- Call `Store.map_table_time_config()` |
| 12 | +- Chronify adds the destination table to the database. |
| 13 | + |
| 14 | +This example creates a representative time table used in chronify's tests. |
| 15 | + |
| 16 | +1. Ingest the source data. |
| 17 | + |
| 18 | +```python |
| 19 | +from datetime import datetime, timedelta |
| 20 | + |
| 21 | +import numpy as np |
| 22 | +import pandas as pd |
| 23 | + |
| 24 | +from chronify import ( |
| 25 | + DatetimeRange, |
| 26 | + RepresentativePeriodFormat, |
| 27 | + RepresentativePeriodTimeNTZ, |
| 28 | + Store, |
| 29 | + CsvTableSchema, |
| 30 | + TableSchema, |
| 31 | +) |
| 32 | + |
| 33 | +src_table_name = "ev_charging" |
| 34 | +dst_table_name = "ev_charging_datetime" |
| 35 | +hours_per_year = 12 * 7 * 24 |
| 36 | +num_time_arrays = 3 |
| 37 | +df = pd.DataFrame({ |
| 38 | + "id": np.concatenate([np.repeat(i, hours_per_year) for i in range(1, 1 + num_time_arrays)]), |
| 39 | + "month": np.tile(np.repeat(range(1, 13), 7 * 24), num_time_arrays), |
| 40 | + "day_of_week": np.tile(np.tile(np.repeat(range(7), 24), 12), num_time_arrays), |
| 41 | + "hour": np.tile(np.tile(range(24), 12 * 7), num_time_arrays), |
| 42 | + "value": np.random.random(hours_per_year * num_time_arrays), |
| 43 | +}) |
| 44 | +schema = TableSchema( |
| 45 | + name=src_table_name, |
| 46 | + value_column="value", |
| 47 | + time_config=RepresentativePeriodTimeNTZ( |
| 48 | + time_format=RepresentativePeriodFormat.ONE_WEEK_PER_MONTH_BY_HOUR, |
| 49 | + ), |
| 50 | + time_array_id_columns=["id"], |
| 51 | +) |
| 52 | +store = Store.create_in_memory_db() |
| 53 | +store.ingest_table(df, schema) |
| 54 | +store.read_query(src_table_name, f"SELECT * FROM {src_table_name} LIMIT 5").head() |
| 55 | +``` |
| 56 | + |
| 57 | +``` |
| 58 | + id month day_of_week hour value |
| 59 | +0 1 1 0 0 0.578496 |
| 60 | +1 1 1 0 1 0.092271 |
| 61 | +2 1 1 0 2 0.111521 |
| 62 | +3 1 1 0 3 0.671668 |
| 63 | +4 1 1 0 4 0.782365 |
| 64 | +``` |
| 65 | + |
| 66 | +2. Map the table's time to datetime. |
| 67 | +```python |
| 68 | +dst_schema = TableSchema( |
| 69 | + name=dst_table_name, |
| 70 | + value_column="value", |
| 71 | + time_array_id_columns=["id"], |
| 72 | + time_config=DatetimeRange( |
| 73 | + time_column="timestamp", |
| 74 | + start=datetime(2020, 1, 1, 0), |
| 75 | + length=8784, |
| 76 | + resolution=timedelta(hours=1), |
| 77 | + ) |
| 78 | +) |
| 79 | +store.map_table_time_config(src_table_name, dst_schema) |
| 80 | +store.read_query(dst_table_name, f"SELECT * FROM {dst_table_name} LIMIT 5").head() |
| 81 | +``` |
| 82 | + |
| 83 | +``` |
| 84 | + id value timestamp |
| 85 | +0 3 0.006213 2020-01-01 00:00:00 |
| 86 | +1 3 0.865765 2020-01-01 01:00:00 |
| 87 | +2 3 0.187256 2020-01-01 02:00:00 |
| 88 | +3 3 0.336157 2020-01-01 03:00:00 |
| 89 | +4 3 0.582281 2020-01-01 04:00:00 |
| 90 | +``` |
0 commit comments