How to use pyjstat - 10 common examples

To help you get started, we’ve selected a few pyjstat examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github predicador37 / pyjstat / examples / examples.py View on Github external
df_list = collection.write('dataframe_list')
print(df_list)

#
# EXAMPLE_URL = 'http://json-stat.org/samples/hierarchy.json'
#
# dataset = pyjstat.Dataset.read(EXAMPLE_URL)
# df = dataset.write('dataframe')
# print(df)


EXAMPLE_URL = 'http://web.ons.gov.uk/ons/api/data/dataset/DC1104EW.json?'\
              'context=Census&jsontype=json-stat&apikey=DCOpn8BU2i&'\
              'geog=2011HTWARDH&diff=&totals=false&'\
              'dm/2011HTWARDH=E12000007'
dataset = pyjstat.Dataset.read(EXAMPLE_URL)
df = dataset.write('dataframe')
print(df)
github predicador37 / pyjstat / pyjstat / pyjstat.py View on Github external
def get(self, element):
        """Gets ith element of a collection in an object of the corresponding \
           class.
        Args:
            output(string): can accept 'jsonstat' or 'dataframe_list'

        Returns:
            Serialized JSONstat or a list of Pandas Dataframes,depending on \
            the 'output' parameter.

        """

        if self['link']['item'][element]['class'] == 'dataset':
            return Dataset.read(self['link']['item'][element]['href'])
        elif self['link']['item'][element]['class'] == 'collection':
            return Collection.read(self['link']['item'][element]['href'])
        elif self['link']['item'][element]['class'] == 'dimension':
            return Dimension.read(self['link']['item'][element]['href'])
        else:
            raise ValueError(
                "Class not allowed. Please use dataset, collection or "
                "dimension'")
github predicador37 / pyjstat / examples / example-2.0.py View on Github external
# -*- coding: utf-8 -*-
""" pyjstat 1.X example for JSON-stat 2.0"""

import time
from pyjstat import pyjstat

EXAMPLE_URL = 'http://www.cso.ie/StatbankServices/StatbankServices.svc/' \
              'jsonservice/responseinstance/TSM01'

# Elapsed time for pyjstat operations only (without network latency)
start = time.time()

# read dataset from url
dataset_from_json_url = pyjstat.Dataset.read(EXAMPLE_URL)

# write dataframe
dataframe = dataset_from_json_url.write('dataframe')
print(dataframe)

# read dataset from dataframe
dataset_from_dataframe = pyjstat.Dataset.read(dataframe)
print(dataset_from_dataframe)

# write dataset to json-stat string
json_string = dataset_from_dataframe.write()
print(json_string)

# read dataset from json-stat string
dataset_from_json_string = pyjstat.Dataset.read(json_string)
print(dataset_from_json_string)
github predicador37 / pyjstat / pyjstat / pyjstat.py View on Github external
them to Pandas Dataframes.

          Args:
            collection (OrderedDict): data in JSON-stat format, previously \
                                      deserialized to a python object by \
                                      json.load() or json.loads(),
            df_list (list): list variable which will contain the converted \
                            datasets.

          Returns:
            Nothing.

          """
    for item in collection['link']['item']:
        if item['class'] == 'dataset':
            df_list.append(Dataset.read(item['href']).write('dataframe'))
        elif item['class'] == 'collection':
            nested_collection = request(item['href'])
            unnest_collection(nested_collection, df_list)
github predicador37 / pyjstat / examples / example-2.0.py View on Github external
dataset_from_json_url = pyjstat.Dataset.read(EXAMPLE_URL)

# write dataframe
dataframe = dataset_from_json_url.write('dataframe')
print(dataframe)

# read dataset from dataframe
dataset_from_dataframe = pyjstat.Dataset.read(dataframe)
print(dataset_from_dataframe)

# write dataset to json-stat string
json_string = dataset_from_dataframe.write()
print(json_string)

# read dataset from json-stat string
dataset_from_json_string = pyjstat.Dataset.read(json_string)
print(dataset_from_json_string)

end = time.time()
print("Time: " + str(end - start))
github predicador37 / pyjstat / examples / example-2.0.py View on Github external
EXAMPLE_URL = 'http://www.cso.ie/StatbankServices/StatbankServices.svc/' \
              'jsonservice/responseinstance/TSM01'

# Elapsed time for pyjstat operations only (without network latency)
start = time.time()

# read dataset from url
dataset_from_json_url = pyjstat.Dataset.read(EXAMPLE_URL)

# write dataframe
dataframe = dataset_from_json_url.write('dataframe')
print(dataframe)

# read dataset from dataframe
dataset_from_dataframe = pyjstat.Dataset.read(dataframe)
print(dataset_from_dataframe)

# write dataset to json-stat string
json_string = dataset_from_dataframe.write()
print(json_string)

# read dataset from json-stat string
dataset_from_json_string = pyjstat.Dataset.read(json_string)
print(dataset_from_json_string)

end = time.time()
print("Time: " + str(end - start))
github predicador37 / pyjstat / examples / example.py View on Github external
# -*- coding: utf-8 -*-
""" pyjstat example with 0.3.5-like syntax for JSON-stat 1.3."""

from collections import OrderedDict
import json
import requests
from pyjstat import pyjstat

EXAMPLE_URL = 'http://json-stat.org/samples/us-labor-ds.json'

data = requests.get(EXAMPLE_URL)
results = pyjstat.from_json_stat(data.json(object_pairs_hook=OrderedDict))
print(results)
print(json.dumps(json.loads(pyjstat.to_json_stat(results))))
github predicador37 / pyjstat / examples / examples.py View on Github external
# print(df)
#
# dataset_from_df = pyjstat.Dataset.read(df)
# print(dataset_from_df.write())
#
# EXAMPLE_URL = 'http://json-stat.org/samples/oecd.json'
# query = [{'concept': 'UNR'}, {'area': 'US'}, {'year': '2010'}]
#
# dataset = pyjstat.Dataset.read(EXAMPLE_URL)
# print(dataset.get_value(query))

from pyjstat import pyjstat

EXAMPLE_URL = 'http://json-stat.org/samples/collection.json'

collection = pyjstat.Collection.read(EXAMPLE_URL)
df_list = collection.write('dataframe_list')
print(df_list)

#
# EXAMPLE_URL = 'http://json-stat.org/samples/hierarchy.json'
#
# dataset = pyjstat.Dataset.read(EXAMPLE_URL)
# df = dataset.write('dataframe')
# print(df)


EXAMPLE_URL = 'http://web.ons.gov.uk/ons/api/data/dataset/DC1104EW.json?'\
              'context=Census&jsontype=json-stat&apikey=DCOpn8BU2i&'\
              'geog=2011HTWARDH&diff=&totals=false&'\
              'dm/2011HTWARDH=E12000007'
dataset = pyjstat.Dataset.read(EXAMPLE_URL)
github predicador37 / pyjstat / examples / example.py View on Github external
# -*- coding: utf-8 -*-
""" pyjstat example with 0.3.5-like syntax for JSON-stat 1.3."""

from collections import OrderedDict
import json
import requests
from pyjstat import pyjstat

EXAMPLE_URL = 'http://json-stat.org/samples/us-labor-ds.json'

data = requests.get(EXAMPLE_URL)
results = pyjstat.from_json_stat(data.json(object_pairs_hook=OrderedDict))
print(results)
print(json.dumps(json.loads(pyjstat.to_json_stat(results))))
github predicador37 / pyjstat / pyjstat / pyjstat.py View on Github external
warnings.warn(
        "Shouldn't use this function anymore! Now use read() methods of"
        "Dataset, Collection or Dimension.",
        DeprecationWarning
    )

    check_input(naming)
    results = []
    if type(datasets) is list:
        for idx, element in enumerate(datasets):
            for dataset in element:
                js_dict = datasets[idx][dataset]
                results.append(generate_df(js_dict, naming, value))
    elif isinstance(datasets, OrderedDict) or type(datasets) is dict or \
            isinstance(datasets, Dataset):
        if 'class' in datasets:
            if datasets['class'] == 'dataset':
                js_dict = datasets
                results.append(generate_df(js_dict, naming, value))
        else:  # 1.00 bundle type
            for dataset in datasets:
                js_dict = datasets[dataset]
                results.append(generate_df(js_dict, naming, value))
    return results