Data types
Introduction
Project Haystack defines a fixed set of data types called kinds, which are mapped to Python objects in Phable.
Map for singleton data types
| Project Haystack | Phable |
|---|---|
Marker |
phable.Marker |
NA |
phable.NA |
Remove |
phable.Remove |
Map for scalar atomic data types
| Project Haystack | Phable |
|---|---|
Bool |
bool |
Number |
phable.Number |
Str |
str |
Uri |
phable.Uri |
Ref |
phable.Ref |
Symbol |
phable.Symbol |
Date |
datetime.date |
Time |
datetime.time |
DateTime |
datetime.datetime |
Coord |
phable.Coord |
XStr |
phable.XStr |
Note: Phable's datetime.datetime must be timezone aware to represent Project
Haystack's DateTime.
Map for collection data types
| Project Haystack | Phable |
|---|---|
List |
list |
Dict |
dict |
Grid |
phable.Grid |
Data Types in Phable Only
As a convenience, Phable defines these data types, which are not defined in Project Haystack:
phable.DateRangephable.DateTimeRange
Marker
Marker data type defined by Project Haystack
here. Marker is a
singleton used to create "label" tags.
Example:
from phable.kinds import Marker
meter_equip = {"meter": Marker(), "equip": Marker()}
Source code in phable/kinds.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
NA
NA data type defined by Project Haystack
here. NA is a
singleton to indicate a data value that is not available. In Project Haystack it is
most often used in historized data to indicate a timestamp sample is in error.
Source code in phable/kinds.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
Remove
Remove data type defined by Project Haystack
here. Remove is a
singleton used in a dict to indicate removal of a tag.
Source code in phable/kinds.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
Number
dataclass
Number data type defined by Project Haystack
here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
float
|
Floating point value. |
required |
unit
|
str | None
|
Optional unit of measurement defined in Project Haystack's standard unit database here. Note: Phable does not validate a defined unit at this time. |
None
|
Source code in phable/kinds.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
Uri
dataclass
Uri data type defined by Project Haystack
here.
Example:
from phable.kinds import Uri
uri = Uri("http://project-haystack.org/")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
str
|
Universal Resource Identifier according to RFC 3986. |
required |
Source code in phable/kinds.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
Ref
dataclass
Ref data type defined by Project Haystack
here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
str
|
Unique identifier for an entity. |
required |
dis
|
str | None
|
Optional human display name. |
None
|
Source code in phable/kinds.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
Symbol
dataclass
Symbol data type defined by Project Haystack
here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
str
|
def identifier. Consists of only ASCII letters, digits, underbar, colon, dash, period, or tilde. |
required |
Source code in phable/kinds.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
Coord
dataclass
Coord data type defined by Project Haystack
here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lat
|
decimal.Decimal
|
Latitude represented in decimal degrees. |
required |
lng
|
decimal.Decimal
|
Longitude represented in decimal degrees. |
required |
Source code in phable/kinds.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
XStr
dataclass
XStr data type defined by Project Haystack
here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type
|
str
|
Type name that follows Project Haystack's tag naming rules, except it must start with an ASCII uppercase letter (A-Z). |
required |
val
|
str
|
String encoded value. |
required |
Source code in phable/kinds.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
Grid
dataclass
Grid data type defined by Project Haystack
here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
meta
|
dict[str, typing.Any]
|
Metadata for the entire |
required |
cols
|
list[dict[str, typing.Any]]
|
Metadata for columns within the |
required |
rows
|
list[dict[str, typing.Any]]
|
Row data for |
required |
Source code in phable/kinds.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | |
get_df_meta
get_df_meta()
Gets metadata for a DataFrame describing data from a Grid.
In the returned dictionary:
- Value for
metakey is data used in Grid'smetaattribute. - Value for
colskey is data used in Grid'scolsattribute.
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, typing.Any] | list[dict[str, typing.Any]]]
|
Dictionary with keys |
Source code in phable/kinds.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
to_grid
staticmethod
to_grid(rows, meta=None)
Creates a Grid using row data and optional metadata.
If parameters include history data, assumes the history rows are in
chronological order to establish hisStart and hisEnd in meta.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
dict[str, typing.Any] | list[dict[str, typing.Any]]
|
Row data for |
required |
meta
|
dict[str, typing.Any] | None
|
Optional metadata for the entire |
None
|
Source code in phable/kinds.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
to_pandas
to_pandas()
Converts rows in the Grid to a Pandas DataFrame.
Requires Phable's optional Pandas dependency to be installed.
For Grids with rows that do not have history data, Phable defined data types
are passed as the data input to the DataFrame.
For Grids with rows that have history data, an opinionated mashing process is
applied to data passed to the DataFrame's data input:
- Phable's
NAobjects are converted toNone - Missing column values are converted to
None Numberobjects are converted to unitlessfloatvalues
The resultant Pandas DataFrame's data types are converted to the pyarrow data
format.
Notes:
- This method is experimental and subject to change.
- This method assumes all
Numberobjects in a given column has the same unit.
Example:
from datetime import datetime, timedelta
from phable import Grid, Number
ts_now = datetime.now()
data = [
{"ts": ts_now - timedelta(minutes=30), "val": Number(30, "kW")},
{"ts": ts_now, "val": Number(38, "kW")},
]
data = Grid.to_grid(data)
df = data.to_pandas()
Source code in phable/kinds.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
to_pandas_all
to_pandas_all()
Returns a tuple: (Grid.get_df_meta(), Grid.to_pandas())
Example:
from datetime import datetime, timedelta
from phable import Grid, Number
ts_now = datetime.now()
data = [
{"ts": ts_now - timedelta(minutes=30), "val": Number(30, "kW")},
{"ts": ts_now, "val": Number(38, "kW")},
]
data = Grid.to_grid(data)
df_meta, df = data.to_pandas_all()
Source code in phable/kinds.py
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | |
to_polars
to_polars()
Converts rows in the Grid to a Polars DataFrame.
Requires Phable's optional Polars dependency to be installed.
For Grids with rows that do not have history data, Phable defined data types
are passed as the data input to the DataFrame.
For Grids with rows that have history data, an opinionated mashing process is
applied to data passed to the DataFrame's data input:
- Phable's
NAobjects are converted toNone - Missing column values are converted to
None Numberobjects are converted to unitlessfloatvalues
Notes:
- This method is experimental and subject to change.
- This method assumes all
Numberobjects in a given column has the same unit.
Example:
from datetime import datetime, timedelta
from phable import Grid, Number
ts_now = datetime.now()
data = [
{"ts": ts_now - timedelta(minutes=30), "val": Number(30, "kW")},
{"ts": ts_now, "val": Number(38, "kW")},
]
data = Grid.to_grid(data)
df = data.to_polars()
Source code in phable/kinds.py
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | |
to_polars_all
to_polars_all()
Returns a tuple: (Grid.get_df_meta(), Grid.to_polars())
Example:
from datetime import datetime, timedelta
from phable import Grid, Number
ts_now = datetime.now()
data = [
{"ts": ts_now - timedelta(minutes=30), "val": Number(30, "kW")},
{"ts": ts_now, "val": Number(38, "kW")},
]
data = Grid.to_grid(data)
df_meta, df = data.to_polars_all()
Source code in phable/kinds.py
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | |
DateRange
dataclass
DateRange data type, defined by Phable, describes a time range using dates.
Note: Project Haystack does not define a kind for DateRange.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
datetime.date
|
Midnight of the start date (inclusive) for the range. |
required |
end
|
datetime.date
|
Midnight of the end date (exclusive) for the range. |
required |
Source code in phable/kinds.py
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | |
DateTimeRange
dataclass
DateTimeRange data type, defined by Phable, describes a time range using
date, time, and timezone information.
datetime objects used for start and end must be timezone aware using
ZoneInfo as a concrete implementation of the datetime.tzinfo abstract base
class.
Example:
from datetime import datetime
from zoneinfo import ZoneInfo
from phable.kinds import DateTimeRange
tzinfo = ZoneInfo("America/New_York")
start = datetime(2024, 11, 22, 8, 19, 0, tzinfo=tzinfo)
end = datetime(2024, 11, 22, 9, 19, 0, tzinfo=tzinfo)
range_with_end = DateTimeRange(start, end)
range_without_end = DateTimeRange(start)
Note: Project Haystack does not define a kind for DateTimeRange.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
datetime.datetime
|
Start timestamp (inclusive) which is timezone aware using |
required |
end
|
datetime.datetime | None
|
Optional end timestamp (exclusive) which is timezone aware using
|
None
|
Source code in phable/kinds.py
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 | |