diff --git a/src/pystatis/table.py b/src/pystatis/table.py index ed374e3..6b644d3 100644 --- a/src/pystatis/table.py +++ b/src/pystatis/table.py @@ -39,9 +39,11 @@ def get_data(self, area: str = "all", prettify: bool = True, **kwargs): endpoint="data", method="tablefile", params=params, as_json=False ) assert isinstance(raw_data, str) # nosec assert_used + self.raw_data = raw_data data_str = StringIO(raw_data) self.data = pd.read_csv(data_str, sep=";") + if prettify: self.data = self.prettify_table(self.data) @@ -49,6 +51,7 @@ def get_data(self, area: str = "all", prettify: bool = True, **kwargs): endpoint="metadata", method="table", params=params, as_json=True ) assert isinstance(metadata, dict) # nosec assert_used + self.metadata = metadata @staticmethod @@ -75,9 +78,7 @@ def prettify_table(data: pd.DataFrame) -> pd.DataFrame: # Given a name like BEV036__Bevoelkerung_in_Hauptwohnsitzhaushalten__1000 # extracts the readable label and omit both the code and the unit - values.columns = [ - " ".join(name.split("_")[1:-1]) for name in values.columns - ] + values.columns = [name.split("__")[1] for name in values.columns] pretty_data = pd.concat([time, attributes, values], axis=1) return pretty_data diff --git a/tests/test_config.py b/tests/test_config.py index 138deb7..6c05b49 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,4 +1,3 @@ -import copy import os from configparser import ConfigParser from pathlib import Path diff --git a/tests/test_db.py b/tests/test_db.py index 6840704..1a0f96b 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -1,10 +1,8 @@ -import logging from configparser import ConfigParser import pytest from pystatis import config, db -from pystatis.exception import PystatisConfigError @pytest.fixture() diff --git a/tests/test_table.py b/tests/test_table.py new file mode 100644 index 0000000..2b5b467 --- /dev/null +++ b/tests/test_table.py @@ -0,0 +1,46 @@ +import pandas as pd +import pytest + +import pystatis + +EASY_TABLE = """Statistik_Code;Statistik_Label;Zeit_Code;Zeit_Label;Zeit;1_Merkmal_Code;1_Merkmal_Label;1_Auspraegung_Code;1_Auspraegung_Label; FLC006__Gebietsflaeche__qkm +11111;Feststellung des Gebietsstands;STAG;Stichtag;31.12.2022;DLAND;Bundesländer;08;Baden-Württemberg;35747,85 +11111;Feststellung des Gebietsstands;STAG;Stichtag;31.12.2022;DLAND;Bundesländer;09;Bayern;70541,58 +11111;Feststellung des Gebietsstands;STAG;Stichtag;31.12.2022;DLAND;Bundesländer;11;Berlin;891,12 +11111;Feststellung des Gebietsstands;STAG;Stichtag;31.12.2022;DLAND;Bundesländer;12;Brandenburg;29654,38 +11111;Feststellung des Gebietsstands;STAG;Stichtag;31.12.2022;DLAND;Bundesländer;04;Bremen;419,61 +11111;Feststellung des Gebietsstands;STAG;Stichtag;31.12.2022;DLAND;Bundesländer;02;Hamburg;755,09 +11111;Feststellung des Gebietsstands;STAG;Stichtag;31.12.2022;DLAND;Bundesländer;06;Hessen;21115,62 +11111;Feststellung des Gebietsstands;STAG;Stichtag;31.12.2022;DLAND;Bundesländer;13;Mecklenburg-Vorpommern;23294,90 +11111;Feststellung des Gebietsstands;STAG;Stichtag;31.12.2022;DLAND;Bundesländer;03;Niedersachsen;47709,90 +11111;Feststellung des Gebietsstands;STAG;Stichtag;31.12.2022;DLAND;Bundesländer;05;Nordrhein-Westfalen;34112,72 +11111;Feststellung des Gebietsstands;STAG;Stichtag;31.12.2022;DLAND;Bundesländer;07;Rheinland-Pfalz;19857,97 +11111;Feststellung des Gebietsstands;STAG;Stichtag;31.12.2022;DLAND;Bundesländer;10;Saarland;2571,52 +11111;Feststellung des Gebietsstands;STAG;Stichtag;31.12.2022;DLAND;Bundesländer;14;Sachsen;18449,86 +11111;Feststellung des Gebietsstands;STAG;Stichtag;31.12.2022;DLAND;Bundesländer;15;Sachsen-Anhalt;20467,20 +11111;Feststellung des Gebietsstands;STAG;Stichtag;31.12.2022;DLAND;Bundesländer;01;Schleswig-Holstein;15804,30 +11111;Feststellung des Gebietsstands;STAG;Stichtag;31.12.2022;DLAND;Bundesländer;16;Thüringen;16202,37 +11111;Feststellung des Gebietsstands;STAG;Stichtag;31.12.2022;DLAND;Bundesländer;;Insgesamt;357595,99""" + + +def test_get_data(mocker): + mocker.patch("pystatis.http_helper.load_data", return_value=EASY_TABLE) + table = pystatis.Table(name="11111-0001") + table.get_data(prettify=False) + assert table.data.shape == (17, 10) + assert isinstance(table.data, pd.DataFrame) + assert not table.data.empty + assert isinstance(table.raw_data, str) + assert table.raw_data != "" + + +def test_prettify(mocker): + mocker.patch("pystatis.http_helper.load_data", return_value=EASY_TABLE) + table = pystatis.Table(name="11111-0001") + table.get_data(prettify=True) + assert table.data.shape == (17, 3) + assert table.data.columns.to_list() == [ + "Stichtag", + "Bundesländer", + "Gebietsflaeche", + ]