JsonStatDataSet

class jsonstat.JsonStatDataSet(name=None)[source]

Represents a JsonStat dataset

>>> import os, jsonstat  
>>> filename = os.path.join(jsonstat._examples_dir, "www.json-stat.org", "oecd-canada-col.json")
>>> dataset = jsonstat.from_file(filename).dataset(0)
>>> dataset.label
'Unemployment rate in the OECD countries 2003-2014'
>>> print(dataset)
name:   'Unemployment rate in the OECD countries 2003-2014'
label:  'Unemployment rate in the OECD countries 2003-2014'
size: 432
+-----+---------+--------------------------------+------+--------+
| pos | id      | label                          | size | role   |
+-----+---------+--------------------------------+------+--------+
| 0   | concept | indicator                      | 1    | metric |
| 1   | area    | OECD countries, EU15 and total | 36   | geo    |
| 2   | year    | 2003-2014                      | 12   | time   |
+-----+---------+--------------------------------+------+--------+
>>> dataset.dimension(1)
+-----+--------+----------------------------+
| pos | idx    | label                      |
+-----+--------+----------------------------+
| 0   | 'AU'   | 'Australia'                |
| 1   | 'AT'   | 'Austria'                  |
| 2   | 'BE'   | 'Belgium'                  |
| 3   | 'CA'   | 'Canada'                   |
| 4   | 'CL'   | 'Chile'                    |
| 5   | 'CZ'   | 'Czech Republic'           |
| 6   | 'DK'   | 'Denmark'                  |
| 7   | 'EE'   | 'Estonia'                  |
| 8   | 'FI'   | 'Finland'                  |
| 9   | 'FR'   | 'France'                   |
| 10  | 'DE'   | 'Germany'                  |
| 11  | 'GR'   | 'Greece'                   |
| 12  | 'HU'   | 'Hungary'                  |
| 13  | 'IS'   | 'Iceland'                  |
| 14  | 'IE'   | 'Ireland'                  |
| 15  | 'IL'   | 'Israel'                   |
| 16  | 'IT'   | 'Italy'                    |
| 17  | 'JP'   | 'Japan'                    |
| 18  | 'KR'   | 'Korea'                    |
| 19  | 'LU'   | 'Luxembourg'               |
| 20  | 'MX'   | 'Mexico'                   |
| 21  | 'NL'   | 'Netherlands'              |
| 22  | 'NZ'   | 'New Zealand'              |
| 23  | 'NO'   | 'Norway'                   |
| 24  | 'PL'   | 'Poland'                   |
| 25  | 'PT'   | 'Portugal'                 |
| 26  | 'SK'   | 'Slovak Republic'          |
| 27  | 'SI'   | 'Slovenia'                 |
| 28  | 'ES'   | 'Spain'                    |
| 29  | 'SE'   | 'Sweden'                   |
| 30  | 'CH'   | 'Switzerland'              |
| 31  | 'TR'   | 'Turkey'                   |
| 32  | 'UK'   | 'United Kingdom'           |
| 33  | 'US'   | 'United States'            |
| 34  | 'EU15' | 'Euro area (15 countries)' |
| 35  | 'OECD' | 'total'                    |
+-----+--------+----------------------------+
>>> dataset.data(0)
JsonStatValue(idx=0, value=5.943826289, status=None)
__init__(name=None)[source]

Initialize an empty dataset.

Dataset could have a name (key) if we parse a jsonstat format version 1.

Parameters:name – dataset name (for jsonstat v.1)
name()
Getter:returns the name of the dataset
Type:string
label()
Getter:returns the label of the dataset
Type:string
__len__()[source]

returns the size of the dataset

dimensions

JsonStatDataSet.dimension(spec)[source]

get a JsonStatDimension by spec

Parameters:spec – spec can be: - (string) or id of the dimension - int position of dimension
Returns:a JsonStatDimension
JsonStatDataSet.dimensions()[source]

returns list of JsonStatDimension

JsonStatDataSet.info_dimensions()[source]

print same info on dimensions on stdout

querying methods

JsonStatDataSet.data(*args, **kargs)[source]

Returns a JsonStatValue containings value and status about a datapoint The datapoint will be retrieved according the parameters

Parameters:
  • args
    • data(<int>) where i is index into the
    • data(<list>) where lst = [i1,i2,i3,...]) each i indicate the dimension len(lst) == number of dimension
    • data(<dict>) where dict is {k1:v1, k2:v2, ...} dimension of size 1 can be ommitted
  • kargs
    • data(k1=v1,k2=v2,...) where ki are the id or label of dimension vi are the index or label of the category dimension of size 1 can be ommitted
Returns:

a JsonStatValue object

kargs { cat1:value1, ..., cati:valuei, ... } cati can be the id of the dimension or the label of dimension valuei can be the index or label of category ex.:{country:”AU”, “year”:”2014”}

>>> import os, jsonstat  
>>> filename = os.path.join(jsonstat._examples_dir, "www.json-stat.org", "oecd-canada-col.json")
>>> dataset = jsonstat.from_file(filename).dataset(0)
>>> dataset.data(0)
JsonStatValue(idx=0, value=5.943826289, status=None)
>>> dataset.data(concept='UNR', area='AU', year='2003')
JsonStatValue(idx=0, value=5.943826289, status=None)
>>> dataset.data(area='AU', year='2003')
JsonStatValue(idx=0, value=5.943826289, status=None)
>>> dataset.data({'area':'AU', 'year':'2003'})
JsonStatValue(idx=0, value=5.943826289, status=None)
JsonStatDataSet.value(*args, **kargs)[source]

get a value For the parameters see py:meth:jsonstat.JsonStatDataSet.data.

Returns:value (typically a number)
JsonStatDataSet.status(*args, **kargs)[source]

get datapoint status

For the parameters see py:meth:jsonstat.JsonStatDataSet.data.

Returns:status (typically a string)

transforming

JsonStatDataSet.to_table(content=u'label', order=None, rtype=<type 'list'>, blocked_dims={}, value_column=u'Value', without_one_dimensions=False)[source]

Transforms a dataset into a table (a list of row)

table len is the size of dataset + 1 for headers

Parameters:
  • content – can be “label” or “id”
  • order
  • rtype
  • blocked_dims
Returns:

a list of row, first line is the header

JsonStatDataSet.to_data_frame(index=None, content=u'label', order=None, blocked_dims={}, value_column=u'Value')[source]

Transform dataset to pandas data frame

extract_bidimensional(“year”, “country”) generate the following dataframe: year | country 2010 | 1 2011 | 2 2012 | 3

Parameters:
  • index
  • content
  • blocked_dims
  • order
  • value_column
Returns:

parsing

JsonStatDataSet.from_file(filename)[source]

read a jsonstat from a file and parse it to initialize this dataset.

It is better to use jsonstat.from_file()

Parameters:filename – path of the file.
Returns:itself to chain calls
JsonStatDataSet.from_string(json_string)[source]

parse a string containing a jsonstat and initialize this dataset

It is better to use jsonstat.from_string()

Parameters:json_string – string containing a jsonstat
Returns:itself to chain calls
JsonStatDataSet.from_json(json_data)[source]

parse a json structure and initialize this dataset

It is better to use py:meth:jsonstat.from_json

Parameters:json_data – json structure
Returns:itself to chain calls