<jupyter_start><jupyter_text>ABS Quarterly National Accounts 5206 Table of Contents1&nbsp;&nbsp;Python set-up2&nbsp;&nbsp;Get data from ABS3&nbsp;&nbsp;Plot3.1&nbsp;&nbsp;Set-up some constants we will use across all plots3.2&nbsp;&nbsp;Growth charts - based on seasonally adjusted series3.3&nbsp;&nbsp;Four quarter rolling totals - original series3.4&nbsp;&nbsp;Gross Value Added (GVA) by Industry3.5&nbsp;&nbsp;Household Final Consumption Expenditure3.6&nbsp;&nbsp;Taxes3.7&nbsp;&nbsp;Government Benefits Payments4&nbsp;&nbsp;Save nominal GDP (it is useful elsewhere)5&nbsp;&nbsp;Finished  Python set-up<jupyter_code># system imports
import pathlib  
import calendar
import re

# analytic imports
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt

# local imports
from finalise_plot import finalise_plot
from abs_common import (
    get_fs_constants,
    get_plot_constants,
    get_ABS_meta_and_data,
    get_identifier,
    plot_Qgrowth,
)

# pandas display settings
pd.options.display.max_rows = 999
pd.options.display.max_columns = 999

# plotting stuff
plt.style.use('ggplot')
catalogue_id = '5206'
source, CHART_DIR, META_DATA = get_fs_constants(catalogue_id)<jupyter_output><empty_output><jupyter_text>Get data from ABS<jupyter_code>abs = get_ABS_meta_and_data(catalogue_id)
if abs is None:
    sys.exit(-1)<jupyter_output>Retrieving zip-file from cache ./ABS_CACHE/-statistics-economy-national-accounts-australian-national-accounts-national-income-expenditure-and-product-dec-2020-All_time_series_workbooks.zip<jupyter_text>Plot  Set-up some constants we will use across all plots<jupyter_code>meta = abs[META_DATA]
RECENT, plot_times, plot_tags = get_plot_constants(meta)
CVM = 'Chain volume measures'<jupyter_output><empty_output><jupyter_text>Growth charts - based on seasonally adjusted series<jupyter_code># identify available headline series from metadata
table = '1'
data = abs[table]

series_type = 'Seasonally Adjusted'
headline_plots = meta[
    (meta['Table'] == table) &
    (meta['Series Type'] == series_type) &
    (meta['Unit'] == '$ Millions') &
    (meta['Data Item Description'].str.contains(CVM))    
]['Data Item Description']

for description in headline_plots:
    
    # identify data
    series, _ = get_identifier(meta, description,
                               series_type, table)
    
    # plot
    for plot_from, plot_tag in zip(plot_times, plot_tags):

        title = description.replace(' ;', '')
        
        plot_Qgrowth(data[series].copy(), 
            title, plot_from, 
            plot_tag, CHART_DIR,
            rfooter=f'{source} {table}',
            lfooter=f'From the {series_type.lower()} series')<jupyter_output><empty_output><jupyter_text>Four quarter rolling totals - original series<jupyter_code># uses same series from previous cell
series_type = "Original"
for description in headline_plots:

    series, units = get_identifier(meta, description,
                               series_type, table)
    rolling4Q = data[series].rolling(4).sum()
    units = f'{units} / Year'
    
    for plot_time, plot_tag in zip(plot_times, plot_tags):
        plot_data = rolling4Q
        if plot_time:
            plot_data = rolling4Q[rolling4Q.index >= plot_time]
        ax = plot_data.plot(lw=2, color="#dd0000")
        
        title = description.replace(' ;', '')
        
        finalise_plot(ax, title, units, plot_tag, CHART_DIR,
            rfooter=f'{source} {table}',
            lfooter=f'4Q rolling sum {series_type.lower()} series'
        )<jupyter_output><empty_output><jupyter_text>Gross Value Added (GVA) by Industry<jupyter_code># identify available series from metadata
table = '6'
data = abs[table]

series_type = 'Seasonally Adjusted'
industries = meta[
    (meta['Table'] == table) &
    (meta['Series Type'] == series_type) &
    (meta['Unit'] == '$ Millions') # limit to money
]['Data Item Description']
industry_start = pd.Timestamp('1974-09-01')

for industry in industries:

    # get data
    series, units = get_identifier(meta, industry,
                               series_type, table)
    units = f'{units}/Qtr'
    
    # plot
    for start, tag in zip(plot_times, plot_tags):
        frame = data[data.index >= start] if start else data
        used_frame = frame[frame.index >= industry_start]
        ax = used_frame[series].plot(lw=2, color="#dd0000")

        # we limit subgroup titles to keep the width
        # within what would fit on the top of a plot
        title = industry
        if not title.endswith(") ;"):
            title = re.sub("^[^\(]+\(", "(", title)
        title = f' GVA: {title}'

        finalise_plot(ax, title, units, tag, CHART_DIR,
            rfooter=f'{source} {table}',
            lfooter=(f'{series_type.capitalize()}, '
                     f'{CVM.lower()}')
        )<jupyter_output><empty_output><jupyter_text>Household Final Consumption Expenditure<jupyter_code># identify available series from metadata
table = '8'
data = abs[table]
series_type = 'Seasonally Adjusted'
hfce = meta[
    (meta['Table'] == table) &
    (meta['Series Type'] == series_type) &
    (meta['Unit'] == '$ Millions') & # limit to money
    (meta['Data Item Description'].str.contains(CVM))
]['Data Item Description']

for description in hfce:
    
    # get data
    series, units = get_identifier(meta, description,
                                   series_type, table)
    units = f'{units}/Qtr'
    
    # plot
    for start, tag in zip(plot_times, plot_tags):
        frame = data[data.index >= start] if start else data
        ax = frame[series].plot(lw=2, color="#dd0000")
        
        title = description.replace(f': {CVM} ;', '')
        title = f'Household spending: {title}'
        
        finalise_plot(ax, title, units, tag, CHART_DIR,
            rfooter=f'{source} {table}',
            lfooter=(f'{series_type.capitalize()}, '
                     f'{CVM.lower()}')
        )<jupyter_output><empty_output><jupyter_text>Taxes<jupyter_code># identify available series from metadata
table = '22'
data = abs[table]
series_type = 'Seasonally Adjusted'
tax_plots = meta[
    (meta['Table'] == table) &
    (meta['Series Type'] == series_type) 
]['Data Item Description']

for plot in tax_plots:
    
    # data capture
    ident, units = get_identifier(meta, plot, series_type, table)
    units = f'{units}/Qtr'
    series = data[ident]
    series = series[series.index >= RECENT]
    if series.isna().all():
        # don't plot empty series
        continue
    
    # plot
    ax = series.plot(lw=2, color="#dd0000")
    title = plot.replace(' ;', '')
    finalise_plot(ax, title, 
                  f'{units} (Current Prices)', 
                  'govt-tax', CHART_DIR,
                  rfooter=f'{source} {table}',
                  lfooter=f'{series_type.capitalize()} series')<jupyter_output><empty_output><jupyter_text>Government Benefits Payments<jupyter_code>table = '23'
data = abs[table]
series_type = 'Original'
plots_payments = meta[
    (meta['Table'] == table) &
    (meta['Series Type'] == series_type) 
]['Data Item Description']

for plot in plots_payments:
    
    # get data
    ident, units = get_identifier(meta, plot, series_type, table)
    units = f'{units}/Qtr'
    series = data[ident]
    series = series[series.index >= RECENT]
    if series.isna().all():
        # don't plot empty series
        continue

    # plot
    ax = series.plot(lw=2, color="#dd0000")
    
    # - from ABS description to plot title
    title = plot.replace('General government - National ;  ',
                         'Federal Govt - ')
    title = title.replace('General government - State and local ;  ',
                         'State or Local Govt - ')
    title = title.replace('General government ;  ',
                         'All Govt - ')
    title = title.replace(' ;', '')
    
    finalise_plot(ax, title, f'{units} (Current Prices)', 
                  'govt-spending', CHART_DIR,
                  rfooter=f'{source} {table}',
                  lfooter=f'{series_type.capitalize()} series')<jupyter_output><empty_output><jupyter_text>Save nominal GDP (it is useful elsewhere)<jupyter_code>NOMINAL_GDP_DIR = "./DATA/"
pathlib.Path(NOMINAL_GDP_DIR).mkdir(parents=True, exist_ok=True)
NOMINAL_GDP_CSV = NOMINAL_GDP_DIR + nominal_gdp.csv

table = '24' # Selected Analytical Series
data = abs[table]
series_type = 'Original'
descriptions = [
    'Non-farm ;  Gross domestic product: Current prices ;',
    'Farm ;  Gross domestic product: Current prices ;',
]
frame = {}
for series in descriptions:
    ident, units = get_identifier(meta, series, series_type, table)
    frame[series] = data[ident]
print(f'From table {table}: {units}')
frame = pd.DataFrame(frame)
frame['Total quarterly'] = frame.sum(axis=1)
frame['Rolling Annual'] = frame['Total quarterly'].rolling(4).sum()

table = '34' # Key Aggregates and analytical series, Annual
data = abs[table]
series = 'Gross domestic product: Current prices ;'
ident, units = get_identifier(meta, series, series_type, table)
print(f'From table {table}: {units}')
frame[f'Annual {series}'] =  data[ident]

frame.to_csv(NOMINAL_GDP_CSV)<jupyter_output><empty_output><jupyter_text>Finished<jupyter_code>print('Finished')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12509411638162848, 'fraction_numerical': 0.00968054211035818, 'mean_word_length': 3.3428304530593183, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '202630', 'license': 'MIT', 'n_tokens_mistral': 3087, 'n_tokens_neox': 2959, 'n_words': 691, 'path': 'notebooks/ABS Quarterly National Accounts 5206.ipynb', 'repo_name': 'bpalmer4/Charts-based-on-ABS-data', 'size': '9297'}