-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
85 lines (72 loc) · 1.66 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""pxwebpy pulling some data to Pandas and Polars respectively"""
import pandas as pd
import polars as pl
from pxwebpy import PxTable
URL = "https://api.scb.se/OV0104/v1/doris/sv/ssd/START/HE/HE0110/HE0110A/SamForvInk1"
QUERY = """
{
"query": [
{
"code": "Region",
"selection": {
"filter": "vs:RegionKommun07EjAggr",
"values": [
"0180",
"1280",
"1480"
]
}
},
{
"code": "Alder",
"selection": {
"filter": "item",
"values": [
"tot20+"
]
}
},
{
"code": "ContentsCode",
"selection": {
"filter": "item",
"values": [
"HE0110J7"
]
}
},
{
"code": "Tid",
"selection": {
"filter": "item",
"values": [
"2021"
]
}
}
],
"response": {
"format": "json-stat2"
}
}
"""
# From a string
data1 = PxTable(URL, QUERY)
# Same query, from a file
data2 = PxTable(URL, "example_query.json")
# The object is instantiated without running the query immediately
data3 = PxTable(URL, QUERY)
# We can fetch the data later on
data3.get_data()
# The get_data() function can be used to refresh data as well.
# Though data is cached for 10 minutes.
# Everytime get_data() is called, the field "fetched"
# is updated with a timestamp (datetime).
data3.fetched
# The objects also contain the metadata of the PxTable table
# such as "label", "source" and "updated".
data3.metadata
# The dataset itself can easily be turned into a pandas dataframe...
pandas_df = pd.DataFrame(data3.dataset)
# ...or a polars dataframe
polars_df = pl.DataFrame(data3.dataset)