2323@brief Downloads data about population statistic
2424
2525"""
26- import configparser
2726import warnings
28- import getpass
2927import requests
3028import os
3129import io
4139pd .options .mode .copy_on_write = True
4240
4341
44- def read_population_data (username , password ):
42+ def read_population_data ():
4543 """! Reads Population data from regionalstatistik.de
4644
47- Username and Password are required to sign in on regionalstatistik.de.
4845 A request is made to regionalstatistik.de and the StringIO is read in as a csv into the dataframe format.
49-
50- @param username Username to sign in at regionalstatistik.de.
51- @param password Password to sign in at regionalstatistik.de.
5246 @return DataFrame
5347 """
5448
5549 download_url = 'https://www.regionalstatistik.de/genesis/online?operation=download&code=12411-02-03-4&option=csv'
56- req = requests .get (download_url , auth = ( username , password ) )
50+ req = requests .get (download_url )
5751 df_pop_raw = pd .read_csv (io .StringIO (req .text ), sep = ';' , header = 6 )
5852
5953 return df_pop_raw
6054
61- # This function is needed for unittests
62- # Fakefilesystem has problems with os.path
63-
64-
65- def path_to_credential_file ():
66- """! Returns path to .ini file where credentials are stored.
67- The Path can be changed if neccessary.
68- """
69- return os .path .join (os .path .dirname (os .path .abspath (__file__ )), 'CredentialsRegio.ini' )
70-
71-
72- def manage_credentials (interactive ):
73- """! Manages credentials for regionalstatistik.de (needed for dowload).
74-
75- A connfig file inside the epidata folder is either written (if not existent yet)
76- with input from user or read with following format:
77- [CREDENTIALS]
78- Username = XXXXX
79- Password = XXXXX
80-
81- @return Username and password to sign in at regionalstatistik.de.
82- """
83- # path where ini file is found
84- path = path_to_credential_file ()
85-
86- gd .default_print (
87- 'Info' , 'No passwaord and/or username for regionalstatistik.de provided. Try to read from .ini file.' )
88-
89- # check if .ini file exists
90- if not os .path .exists (path ):
91- if interactive :
92- gd .default_print (
93- 'Info' , '.ini file not found. Writing CredentialsRegio.ini...' )
94- username = input (
95- "Please enter username for https://www.regionalstatistik.de/genesis/online\n " )
96- password = getpass .getpass (
97- "Please enter password for https://www.regionalstatistik.de/genesis/online\n " )
98- # create file
99- write_ini = gd .user_choice (
100- message = 'Do you want the credentials to be stored in an unencrypted .ini file?\n ' +
101- 'The next time this function is called, the credentials can be read from that file.' )
102- if write_ini :
103- string = '[CREDENTIALS]\n Username = ' + \
104- username + '\n Password = ' + password
105- with open (path , 'w+' ) as file :
106- file .write (string )
107- else :
108- raise gd .DataError (
109- 'No .ini file found. Cannot access regionalstatistik.de for downloading population data.' )
110-
111- else :
112- parser = configparser .ConfigParser ()
113- parser .read (path )
114-
115- username = parser ['CREDENTIALS' ]['Username' ]
116- password = parser ['CREDENTIALS' ]['Password' ]
117-
118- return username , password
119-
12055
12156def export_population_dataframe (df_pop : pd .DataFrame , directory : str , file_format : str , merge_eisenach : bool ):
12257 """! Writes population dataframe into directory with new column names and age groups
@@ -285,8 +220,6 @@ def test_total_population(df_pop, age_cols):
285220
286221def fetch_population_data (read_data : bool = dd .defaultDict ['read_data' ],
287222 out_folder : str = dd .defaultDict ['out_folder' ],
288- username = '' ,
289- password = '' ,
290223 ** kwargs
291224 ) -> pd .DataFrame :
292225 """! Downloads or reads the population data.
@@ -299,9 +232,6 @@ def fetch_population_data(read_data: bool = dd.defaultDict['read_data'],
299232 downloaded. Default defined in defaultDict.
300233 @param out_folder Path to folder where data is written in folder
301234 out_folder/Germany. Default defined in defaultDict.
302- @param username Username to sign in at regionalstatistik.de.
303- @param password Password to sign in at regionalstatistik.de.
304-
305235 @return DataFrame with adjusted population data for all ages to current level.
306236 """
307237 conf = gd .Conf (out_folder , ** kwargs )
@@ -312,14 +242,10 @@ def fetch_population_data(read_data: bool = dd.defaultDict['read_data'],
312242 'Warning' , 'Read_data is not supportet for getPopulationData.py. Setting read_data = False' )
313243 read_data = False
314244
315- # If no username or password is provided, the credentials are either read from an .ini file or,
316- # if the file does not exist they have to be given as user input.
317- if (username is None ) or (password is None ):
318- username , password = manage_credentials (conf .interactive )
319245 directory = os .path .join (out_folder , 'Germany' )
320246 gd .check_dir (directory )
321247
322- df_pop_raw = read_population_data (username , password )
248+ df_pop_raw = read_population_data ()
323249
324250 return df_pop_raw
325251
@@ -411,8 +337,6 @@ def get_population_data(read_data: bool = dd.defaultDict['read_data'],
411337 file_format : str = dd .defaultDict ['file_format' ],
412338 out_folder : str = dd .defaultDict ['out_folder' ],
413339 merge_eisenach : bool = True ,
414- username = '' ,
415- password = '' ,
416340 ** kwargs
417341 ):
418342 """! Download age-stratified population data for the German counties.
@@ -453,8 +377,6 @@ def get_population_data(read_data: bool = dd.defaultDict['read_data'],
453377 read_data = read_data ,
454378 out_folder = out_folder ,
455379 file_format = file_format ,
456- username = username ,
457- password = password ,
458380 ** kwargs
459381 )
460382 preprocess_df = preprocess_population_data (
0 commit comments