scbhelpers module¶
-
tripsender.scbhelpers.
check_connection
(response)¶ A utility function to check the response code and print the status.
- Parameters
response – (response object)
- Returns
-
tripsender.scbhelpers.
get_area
(area)¶ Function that searches a list of official area names and ensures that it is spelt correctly. Searches the partial primary area code, non-capitalised partial area names and returns the full string.
- Example:
Fetching the
pxWeb
compatible area name for Haga:# Importing libraries import tripsender as ts # Searching areas with name only my_area_name = ts.scbhelpers.get_area('Haga') # Searching areas with area code my_area_code = ts.scbhelpers.get_area('107') # Print the results print(my_area_name==my_area_code, my_area_name) out: True 107 Haga
- Parameters
area – (str) The partial area name or code
- Returns
(str) Full name string of matched area
-
tripsender.scbhelpers.
get_area_sp
(foundArea)¶ A function that takes a correctly formatted area string and converts it into the scb special formatting. This special formatting is required for specific queries on the
pxWeb
platform.- Parameters
foundArea – The correctly formatted area name as provided by the
get_area
function- Returns
Area name with special
pxWeb
formatting
-
tripsender.scbhelpers.
get_distribution
(df)¶ A function that returns the total sum, dataframe of probability distribution, marginals_x and marginals_y.
- Parameters
df – (pandas DataFrame)
- Returns
(total,probability distribution,marginals on x, marginals on y)
-
tripsender.scbhelpers.
get_table
(first, second, area, year, showResponse=False, plot=True)¶ A function to fetch data on two requested variables from the
pxWeb
server. Note: The server takes instructions using Swedish keywords, hence explicitly typed function.- Parameters
first – (str) First variable
second – (str) Second Variable
area – (str) Partial Primary Area or Primary Area code
year – (str) Year. Note : Some variables are not updated since 2019.
showResponse – (bool: Optional) If
True
, print the response codeplot – (bool: Optional) If
True
, Plot the data
- Returns
(Pandas DataFrame)
-
tripsender.scbhelpers.
get_trend
(first, second, area, showResponse=False)¶ A function that fetches multi-year data for two valid variables. Currently only age and household are supported.
- Parameters
first – (str) First variable
second – (str) Second variable
area – (str) A partial area name or partial area code
showResponse – (bool) If
True
, show the request response code
- Returns
(pandas DataFrame, plot)
-
tripsender.scbhelpers.
prep_data
(df, headings, title='Plot of requested data', plot=True)¶ A function that prepares the input DataFrame by resetting the index and headings from a list of headings.
- Parameters
df – (pandas DataFrame)
headings – (list)
title – (Optional : str) Optional plot title
plot – (Optional : bool) If
True
, plot the prepared DataFrame
- Returns
(pandas DataFrame)
-
tripsender.scbhelpers.
prep_data_income
(df, headings, title='Plot of requested data', plot=True)¶ Note: Use this function for income related data. This is due to the unique age groups that cannot be changed through the query from the
pxWeb
server. A function that prepares the input DataFrame by resetting the index and headings from a list of headings.- Parameters
df – (pandas DataFrame)
headings – (list)
title – (Optional : str) Optional plot title
plot – (Optional : bool) If
True
, plot the prepared DataFrame
- Returns
(pandas DataFrame)
-
tripsender.scbhelpers.
query_data
(url, query, foundArea, showResponse=True, timeout=1.5)¶ A function to post the json query to the
pxWeb
server for Gothenburg.- Parameters
url – (str) Query URL
query – (str) Query JSON
foundArea – (str) Correctly formatted area name
showResponse – (bool : Optional) If True : Show the request response code
timeout – (float: Optional) Request timeout, default at 1.5 seconds.
- Returns
(DataFrame, heading (str), title (str))