Ejemplo n.º 1
0
class TestStructSpecSeriesData_RateGroup_TS(unittest.TestCase):

    def setUp(self):
        self.estat = Request()
        filepath = os.path.join(
            pkg_path, 'data/exr/ecb_exr_rg/structured/ecb_exr_rg_ts.xml')
        dsd_resp = self.estat.datastructure(
            fromfile=os.path.join(pkg_path, 'data/exr/ecb_exr_rg/ecb_exr_rg.xml'))
        dsd = dsd_resp.datastructure.DataStructure
        self.resp = self.estat.get(fromfile=filepath, dsd=dsd)

    def test_groups(self):
        data = self.resp.data
        self.assertEqual(len(list(data.groups)), 5)
        self.assertEqual(len(list(data.series)), 4)
        g2 = list(data.groups)[2]
        self.assertEqual(g2.key.CURRENCY, 'GBP')
        self.assertEqual(
            g2.attrib.TITLE, 'ECB reference exchange rate, U.K. Pound sterling /Euro')
        # Check group attributes of a series
        s = list(data.series)[0]
        g_attrib = s.group_attrib
        self.assertEqual(len(g_attrib), 5)
        self.assertIsInstance(g_attrib, tuple)
        self.assertEqual(len(g_attrib), 5)
class TestStructSpecSeriesData_RateGroup_TS(unittest.TestCase):

    def setUp(self):
        self.estat = Request()
        filepath = os.path.join(
            pkg_path, 'data/exr/ecb_exr_rg/structured/ecb_exr_rg_ts.xml')
        dsd_resp = self.estat.datastructure(
            fromfile=os.path.join(pkg_path, 'data/exr/ecb_exr_rg/ecb_exr_rg.xml'))
        dsd = dsd_resp.datastructure.DataStructure
        self.resp = self.estat.get(fromfile=filepath, dsd=dsd)

    def test_groups(self):
        data = self.resp.data
        self.assertEqual(len(list(data.groups)), 5)
        self.assertEqual(len(list(data.series)), 4)
        g2 = list(data.groups)[2]
        self.assertEqual(g2.key.CURRENCY, 'GBP')
        self.assertEqual(
            g2.attrib.TITLE, 'ECB reference exchange rate, U.K. Pound sterling /Euro')
        # Check group attributes of a series
        s = list(data.series)[0]
        g_attrib = s.group_attrib
        self.assertEqual(len(g_attrib), 5)
        self.assertIsInstance(g_attrib, tuple)
        self.assertEqual(len(g_attrib), 5)
Ejemplo n.º 3
0
class TestStructSpecSeriesDataSet2(unittest.TestCase):

    def setUp(self):
        self.estat = Request('ESTAT')
        filepath = os.path.join(
            pkg_path, 'data/exr/ecb_exr_ng/structured/ecb_exr_ng_ts.xml')
        dsd_resp = self.estat.datastructure(
            fromfile=os.path.join(pkg_path, 'data/exr/ecb_exr_ng/ecb_exr_ng_full.xml'))
        dsd = dsd_resp.datastructure.DataStructure
        self.resp = self.estat.data(fromfile=filepath, dsd=dsd)

    def test_header_attributes(self):
        self.assertEqual(self.resp.header.structured_by, 'STR1')
        self.assertEqual(self.resp.header.dim_at_obs, 'TIME_PERIOD')

    def test_dataset_cls(self):
        self.assertIsInstance(self.resp.data, model.DataSet)

    def test_structured_obs(self):
        data = self.resp.data
        # empty obs iterator
        self.assertEqual(len(list(data.obs())), 0)
        series_list = list(data.series)
        self.assertEqual(len(series_list), 4)
        s3 = series_list[3]
        self.assertIsInstance(s3, model.Series)
        self.assertIsInstance(s3.key, tuple)
        self.assertEqual(len(s3.key), 4)
        self.assertEqual(s3.key.CURRENCY, 'USD')
        self.assertEqual(s3.attrib.DECIMALS, '4')
        obs_list = list(s3.obs(reverse_obs=True))
        self.assertEqual(len(obs_list), 3)
        o0 = obs_list[2]
        self.assertEqual(len(o0), 3)
        self.assertEqual(o0.dim, '2010-08')
        self.assertEqual(o0.value, '1.2894')
        self.assertIsInstance(o0.attrib, tuple)
        self.assertEqual(o0.attrib.OBS_STATUS, 'A')

    def test_dataframe(self):
        data = self.resp.data
        df = self.resp.write(
            data, attributes='', asframe=True, reverse_obs=True)
        self.assertIsInstance(df, pandas.core.frame.DataFrame)
        self.assertEqual(df.shape, (3, 4))
class TestStructSpecSeriesDataSet2(unittest.TestCase):

    def setUp(self):
        self.estat = Request('ESTAT')
        filepath = os.path.join(
            pkg_path, 'data/exr/ecb_exr_ng/structured/ecb_exr_ng_ts.xml')
        dsd_resp = self.estat.datastructure(
            fromfile=os.path.join(pkg_path, 'data/exr/ecb_exr_ng/ecb_exr_ng_full.xml'))
        dsd = dsd_resp.datastructure.DataStructure
        self.resp = self.estat.data(fromfile=filepath, dsd=dsd)

    def test_header_attributes(self):
        self.assertEqual(self.resp.header.structured_by, 'STR1')
        self.assertEqual(self.resp.header.dim_at_obs, 'TIME_PERIOD')

    def test_dataset_cls(self):
        self.assertIsInstance(self.resp.data, model.DataSet)

    def test_structured_obs(self):
        data = self.resp.data
        # empty obs iterator
        self.assertEqual(len(list(data.obs())), 0)
        series_list = list(data.series)
        self.assertEqual(len(series_list), 4)
        s3 = series_list[3]
        self.assertIsInstance(s3, model.Series)
        self.assertIsInstance(s3.key, tuple)
        self.assertEqual(len(s3.key), 4)
        self.assertEqual(s3.key.CURRENCY, 'USD')
        self.assertEqual(s3.attrib.DECIMALS, '4')
        obs_list = list(s3.obs(reverse_obs=True))
        self.assertEqual(len(obs_list), 3)
        o0 = obs_list[2]
        self.assertEqual(len(o0), 3)
        self.assertEqual(o0.dim, '2010-08')
        self.assertEqual(o0.value, '1.2894')
        self.assertIsInstance(o0.attrib, tuple)
        self.assertEqual(o0.attrib.OBS_STATUS, 'A')

    def test_dataframe(self):
        data = self.resp.data
        df = self.resp.write(
            data, attributes='', asframe=True, reverse_obs=True)
        self.assertIsInstance(df, pandas.core.frame.DataFrame)
        self.assertEqual(df.shape, (3, 4))
Ejemplo n.º 5
0
class TestStructSpecFlatDataSet(unittest.TestCase):

    def setUp(self):
        self.estat = Request('ESTAT')
        filepath = os.path.join(
            pkg_path, 'data/exr/ecb_exr_ng/structured/ecb_exr_ng_flat.xml')
        dsd_resp = self.estat.datastructure(
            fromfile=os.path.join(pkg_path, 'data/exr/ecb_exr_ng/ecb_exr_ng_full.xml'))
        dsd = dsd_resp.datastructure.DataStructure
        self.resp = self.estat.get(fromfile=filepath, dsd=dsd)

    def test_msg_type(self):
        self.assertIsInstance(self.resp.msg, model.DataMessage)

    def test_header_attributes(self):
        self.assertEqual(self.resp.header.structured_by, 'STR1')
        self.assertEqual(self.resp.header.dim_at_obs, 'AllDimensions')

    def test_dataset_cls(self):
        self.assertIsInstance(self.resp.data, model.DataSet)
        self.assertEqual(self.resp.msg.data.dim_at_obs, 'AllDimensions')

    def test_generic_obs(self):
        data = self.resp.data
        # empty series list
        self.assertEqual(len(list(data.series)), 0)
        obs_list = list(data.obs())
        self.assertEqual(len(obs_list), 12)
        o0 = obs_list[0]
        self.assertEqual(len(o0), 3)
        self.assertIsInstance(o0.key, tuple)  # obs_key
        self.assertEqual(o0.key.FREQ, 'M')
        self.assertEqual(o0.key.CURRENCY, 'CHF')
        self.assertEqual(o0.value, '1.3413')
        self.assertIsInstance(o0.attrib, tuple)
        self.assertEqual(o0.attrib.OBS_STATUS, 'A')
        self.assertEqual(o0.attrib.DECIMALS, '4')

    def test_write2pandas(self):
        data_series = self.resp.write(attributes='',
                                      asframe=False, reverse_obs=False)
        self.assertIsInstance(data_series, pandas.Series)
class TestStructSpecFlatDataSet(unittest.TestCase):

    def setUp(self):
        self.estat = Request('ESTAT')
        filepath = os.path.join(
            pkg_path, 'data/exr/ecb_exr_ng/structured/ecb_exr_ng_flat.xml')
        dsd_resp = self.estat.datastructure(
            fromfile=os.path.join(pkg_path, 'data/exr/ecb_exr_ng/ecb_exr_ng_full.xml'))
        dsd = dsd_resp.datastructure.DataStructure
        self.resp = self.estat.get(fromfile=filepath, dsd=dsd)

    def test_msg_type(self):
        self.assertIsInstance(self.resp.msg, model.DataMessage)

    def test_header_attributes(self):
        self.assertEqual(self.resp.header.structured_by, 'STR1')
        self.assertEqual(self.resp.header.dim_at_obs, 'AllDimensions')

    def test_dataset_cls(self):
        self.assertIsInstance(self.resp.data, model.DataSet)
        self.assertEqual(self.resp.msg.data.dim_at_obs, 'AllDimensions')

    def test_generic_obs(self):
        data = self.resp.data
        # empty series list
        self.assertEqual(len(list(data.series)), 0)
        obs_list = list(data.obs())
        self.assertEqual(len(obs_list), 12)
        o0 = obs_list[0]
        self.assertEqual(len(o0), 3)
        self.assertIsInstance(o0.key, tuple)  # obs_key
        self.assertEqual(o0.key.FREQ, 'M')
        self.assertEqual(o0.key.CURRENCY, 'CHF')
        self.assertEqual(o0.value, '1.3413')
        self.assertIsInstance(o0.attrib, tuple)
        self.assertEqual(o0.attrib.OBS_STATUS, 'A')
        self.assertEqual(o0.attrib.DECIMALS, '4')

    def test_write2pandas(self):
        data_series = self.resp.write(attributes='',
                                      asframe=False, reverse_obs=False)
        self.assertIsInstance(data_series, pandas.Series)
Ejemplo n.º 7
0
def test_doc_usage_structure():
    """Code examples in walkthrough.rst."""
    ecb = Request("ECB")

    ecb_via_proxy = Request("ECB", proxies={"http": "http://1.2.3.4:5678"})
    assert all(
        getattr(ecb_via_proxy.session, k) == v for k, v in (
            ("proxies", {
                "http": "http://1.2.3.4:5678"
            }),
            ("stream", False),
            ("timeout", 30.1),
        ))

    msg1 = ecb.categoryscheme()

    assert msg1.response.url == (
        "http://sdw-wsrest.ecb.int/service/categoryscheme/ECB/latest"
        "?references=parentsandsiblings")

    # Check specific headers
    headers = msg1.response.headers
    assert headers["Content-Type"] == ("application/vnd.sdmx.structure+xml; "
                                       "version=2.1")
    assert all(k in headers for k in ["Connection", "Date", "Server"])

    # Removed: in pandaSDMX 0.x this was a convenience method that (for this
    # structure message) returned two DataStructureDefinitions. Contra the
    # spec, that assumes:
    # - There is 1 Categorization using the CategoryScheme; there could be
    #   many.
    # - The Categorization maps DataStructureDefinitions to Categories, when
    #   there could be many.
    # list(cat_response.category_scheme['MOBILE_NAVI']['07'])

    dfs = pandasdmx.to_pandas(msg1.dataflow).head()
    assert len(dfs) == 2

    flows = ecb.dataflow()  # noqa: F841
    dsd_id = flows.dataflow.EXR.structure.id
    assert dsd_id == "ECB_EXR1"

    refs = dict(references="all")
    msg2 = ecb.datastructure(resource_id=dsd_id, params=refs)
    dsd = msg2.structure[dsd_id]

    assert pandasdmx.to_pandas(dsd.dimensions) == [
        "FREQ",
        "CURRENCY",
        "CURRENCY_DENOM",
        "EXR_TYPE",
        "EXR_SUFFIX",
        "TIME_PERIOD",
    ]

    cl = pandasdmx.to_pandas(msg2.codelist["CL_CURRENCY"]).sort_index()
    expected = pd.Series(
        {
            "ADF": "Andorran Franc (1-1 peg to the French franc)",
            "ADP": "Andorran Peseta (1-1 peg to the Spanish peseta)",
            "AED": "United Arab Emirates dirham",
            "AFA": "Afghanistan afghani (old)",
            "AFN": "Afghanistan, Afghanis",
        },
        name="Currency code list",
    ).rename_axis("CL_CURRENCY")
    assert_pd_equal(cl.head(), expected)
Ejemplo n.º 8
0
class TestStructSpecSeriesDataSet(unittest.TestCase):

    def setUp(self):
        self.estat = Request('ESTAT')
        filepath = os.path.join(
            pkg_path, 'data/exr/ecb_exr_ng/structured/ecb_exr_ng_ts_gf.xml')
        dsd_resp = self.estat.datastructure(
            fromfile=os.path.join(pkg_path, 'data/exr/ecb_exr_ng/ecb_exr_ng_full.xml'))
        dsd = dsd_resp.datastructure.DataStructure
        self.resp = self.estat.data(fromfile=filepath, dsd=dsd)

    def test_header_attributes(self):
        self.assertEqual(self.resp.header.structured_by, 'STR1')
        self.assertEqual(self.resp.header.dim_at_obs, 'TIME_PERIOD')

    def test_dataset_cls(self):
        self.assertIsInstance(self.resp.msg.data, model.DataSet)

    def test_obs(self):
        data = self.resp.data
        # empty obs iterator
        self.assertEqual(len(list(data.obs())), 0)
        series_list = list(data.series)
        self.assertEqual(len(series_list), 4)
        s3 = series_list[3]
        self.assertIsInstance(s3, model.Series)
        self.assertIsInstance(s3.key, tuple)
        self.assertEqual(len(s3.key), 4)
        self.assertEqual(s3.key.CURRENCY, 'USD')
        self.assertEqual(s3.attrib.DECIMALS, '4')
        obs_list = list(s3.obs(reverse_obs=True))
        self.assertEqual(len(obs_list), 3)
        o0 = obs_list[2]
        self.assertEqual(len(o0), 3)
        self.assertEqual(o0.dim, '2010-08')
        self.assertEqual(o0.value, '1.2894')
        self.assertIsInstance(o0.attrib, tuple)
        self.assertEqual(o0.attrib.OBS_STATUS, 'A')

    def test_pandas(self):
        resp = self.resp
        data = resp.data
        pd_series = [s for s in resp.write(
            data, attributes='', reverse_obs=True, asframe=False)]
        self.assertEqual(len(pd_series), 4)
        s3 = pd_series[3]
        self.assertIsInstance(s3, pandas.core.series.Series)
        self.assertEqual(s3[2], 1.2894)
        self.assertIsInstance(s3.name, tuple)
        self.assertEqual(len(s3.name), 4)
        # now with attributes
        pd_series = [s for s in resp.write(
            data, attributes='osgd', reverse_obs=True, asframe=False)]
        self.assertEqual(len(pd_series), 4)
        self.assertIsInstance(pd_series[0], tuple)  # contains 2 series
        self.assertEqual(len(pd_series[0]), 2)
        s3, a3 = pd_series[3]
        self.assertIsInstance(s3, pandas.core.series.Series)
        self.assertIsInstance(a3, pandas.core.series.Series)
        self.assertEqual(s3[2], 1.2894)
        self.assertIsInstance(s3.name, tuple)
        self.assertEqual(len(s3.name), 4)
        self.assertEqual(len(a3), 3)
        # access an attribute of the first value
        self.assertEqual(a3[0].OBS_STATUS, 'A')

    def test_write2pandas(self):
        df = self.resp.write(attributes='',
                             reverse_obs=False)
        self.assertIsInstance(df, pandas.DataFrame)
        assert df.shape == (3, 4)
        # with metadata
        df, mdf = self.resp.write(attributes='osgd',
                                  reverse_obs=False)
        assert mdf.shape == (3, 4)
        assert mdf.iloc[1, 1].OBS_STATUS == 'A'
Ejemplo n.º 9
0
def test_doc_usage_structure():
    """Code examples in walkthrough.rst."""
    ecb = Request('ECB')

    ecb_via_proxy = Request('ECB', proxies={'http': 'http://1.2.3.4:5678'})
    assert all(
        getattr(ecb_via_proxy.session, k) == v for k, v in (
            ('proxies', {
                'http': 'http://1.2.3.4:5678'
            }),
            ('stream', False),
            ('timeout', 30.1),
        ))

    msg1 = ecb.categoryscheme()

    assert msg1.response.url == (
        'http://sdw-wsrest.ecb.int/service/categoryscheme/ECB/latest'
        '?references=parentsandsiblings')

    # Check specific headers
    headers = msg1.response.headers
    assert headers['Content-Type'] == ('application/vnd.sdmx.structure+xml; '
                                       'version=2.1')
    assert all(k in headers for k in ['Connection', 'Date', 'Server'])

    # Removed: in pandaSDMX 0.x this was a convenience method that (for this
    # structure message) returned two DataStructureDefinitions. Contra the
    # spec, that assumes:
    # - There is 1 Categorization using the CategoryScheme; there could be
    #   many.
    # - The Categorization maps DataStructureDefintions to Categories, when
    #   there could be many.
    # list(cat_response.category_scheme['MOBILE_NAVI']['07'])

    dfs = sdmx.to_pandas(msg1.dataflow).head()
    expected = pd.Series({
        'AME':
        'AMECO',
        'BKN':
        'Banknotes statistics',
        'BLS':
        'Bank Lending Survey Statistics',
        'BOP': ('Euro Area Balance of Payments and International Investment '
                'Position Statistics'),
        'BSI':
        'Balance Sheet Items',
    })
    assert_pd_equal(dfs, expected)

    flows = ecb.dataflow()  # noqa: F841
    dsd_id = msg1.dataflow.EXR.structure.id
    assert dsd_id == 'ECB_EXR1'

    refs = dict(references='all')
    msg2 = ecb.datastructure(resource_id=dsd_id, params=refs)
    dsd = msg2.structure[dsd_id]

    assert sdmx.to_pandas(dsd.dimensions) == [
        'FREQ', 'CURRENCY', 'CURRENCY_DENOM', 'EXR_TYPE', 'EXR_SUFFIX',
        'TIME_PERIOD'
    ]

    cl = sdmx.to_pandas(msg2.codelist['CL_CURRENCY']).sort_index()
    expected = pd.Series({
        'ADF': 'Andorran Franc (1-1 peg to the French franc)',
        'ADP': 'Andorran Peseta (1-1 peg to the Spanish peseta)',
        'AED': 'United Arab Emirates dirham',
        'AFA': 'Afghanistan afghani (old)',
        'AFN': 'Afghanistan, Afghanis',
        }, name='Currency code list') \
        .rename_axis('CL_CURRENCY')
    assert_pd_equal(cl.head(), expected)
class TestStructSpecSeriesDataSet(unittest.TestCase):

    def setUp(self):
        self.estat = Request('ESTAT')
        filepath = os.path.join(
            pkg_path, 'data/exr/ecb_exr_ng/structured/ecb_exr_ng_ts_gf.xml')
        dsd_resp = self.estat.datastructure(
            fromfile=os.path.join(pkg_path, 'data/exr/ecb_exr_ng/ecb_exr_ng_full.xml'))
        dsd = dsd_resp.datastructure.DataStructure
        self.resp = self.estat.data(fromfile=filepath, dsd=dsd)

    def test_header_attributes(self):
        self.assertEqual(self.resp.header.structured_by, 'STR1')
        self.assertEqual(self.resp.header.dim_at_obs, 'TIME_PERIOD')

    def test_dataset_cls(self):
        self.assertIsInstance(self.resp.msg.data, model.DataSet)

    def test_obs(self):
        data = self.resp.data
        # empty obs iterator
        self.assertEqual(len(list(data.obs())), 0)
        series_list = list(data.series)
        self.assertEqual(len(series_list), 4)
        s3 = series_list[3]
        self.assertIsInstance(s3, model.Series)
        self.assertIsInstance(s3.key, tuple)
        self.assertEqual(len(s3.key), 4)
        self.assertEqual(s3.key.CURRENCY, 'USD')
        self.assertEqual(s3.attrib.DECIMALS, '4')
        obs_list = list(s3.obs(reverse_obs=True))
        self.assertEqual(len(obs_list), 3)
        o0 = obs_list[2]
        self.assertEqual(len(o0), 3)
        self.assertEqual(o0.dim, '2010-08')
        self.assertEqual(o0.value, '1.2894')
        self.assertIsInstance(o0.attrib, tuple)
        self.assertEqual(o0.attrib.OBS_STATUS, 'A')

    def test_pandas(self):
        resp = self.resp
        data = resp.data
        pd_series = [s for s in resp.write(
            data, attributes='', reverse_obs=True, asframe=False)]
        self.assertEqual(len(pd_series), 4)
        s3 = pd_series[3]
        self.assertIsInstance(s3, pandas.core.series.Series)
        self.assertEqual(s3[2], 1.2894)
        self.assertIsInstance(s3.name, tuple)
        self.assertEqual(len(s3.name), 4)
        # now with attributes
        pd_series = [s for s in resp.write(
            data, attributes='osgd', reverse_obs=True, asframe=False)]
        self.assertEqual(len(pd_series), 4)
        self.assertIsInstance(pd_series[0], tuple)  # contains 2 series
        self.assertEqual(len(pd_series[0]), 2)
        s3, a3 = pd_series[3]
        self.assertIsInstance(s3, pandas.core.series.Series)
        self.assertIsInstance(a3, pandas.core.series.Series)
        self.assertEqual(s3[2], 1.2894)
        self.assertIsInstance(s3.name, tuple)
        self.assertEqual(len(s3.name), 4)
        self.assertEqual(len(a3), 3)
        # access an attribute of the first value
        self.assertEqual(a3[0].OBS_STATUS, 'A')

    def test_write2pandas(self):
        df = self.resp.write(attributes='',
                             reverse_obs=False)
        self.assertIsInstance(df, pandas.DataFrame)
        assert df.shape == (3, 4)
        # with metadata
        df, mdf = self.resp.write(attributes='osgd',
                                  reverse_obs=False)
        assert mdf.shape == (3, 4)
        assert mdf.iloc[1, 1].OBS_STATUS == 'A'