pudl.extract.excel#

Load excel metadata CSV files form a python data package.

Module Contents#

Classes#

Metadata

Load Excel metadata from Python package data.

GenericExtractor

Contains logic for extracting panda.DataFrames from excel spreadsheets.

Attributes#

pudl.extract.excel.logger[source]#
class pudl.extract.excel.Metadata(dataset_name: str)[source]#

Load Excel metadata from Python package data.

Excel sheet files may contain many different tables. When we load those into dataframes, metadata tells us how to do this. Metadata generally informs us about the position of a given page in the file (which sheet and which row) and it informs us how to translate excel column names into standardized column names.

When metadata object is instantiated, it is given ${dataset} name and it will attempt to load csv files from pudl.package_data.${dataset} package.

It expects the following kinds of files:

  • skiprows.csv tells us how many initial rows should be skipped when loading data for given (partition, page).

  • skipfooter.csv tells us how many bottom rows should be skipped when loading data for given partition (partition, page).

  • page_map.csv tells us what is the excel sheet name that should be read when loading data for given (partition, page)

  • column_map/${page}.csv currently informs us how to translate input column names to standardized pudl names for given (partition, input_col_name). Relevant page is encoded in the filename.

get_dataset_name()[source]#

Returns the name of the dataset described by this metadata.

get_sheet_name(page, **partition)[source]#

Returns name of the excel sheet that contains the data for given partition and page.

get_skiprows(page, **partition)[source]#

Returns number of initial rows to skip when loading given partition and page.

get_skipfooter(page, **partition)[source]#

Returns number of bottom rows to skip when loading given partition and page.

get_file_name(page, **partition)[source]#

Returns file name of given partition and page.

get_column_map(page, **partition)[source]#

Returns the dictionary mapping input columns to pudl columns for given partition and page.

get_all_columns(page)[source]#

Returns list of all pudl (standardized) columns for a given page (across all partition).

get_all_pages()[source]#

Returns list of all known pages.

static _load_csv(package, filename)[source]#

Load metadata from a filename that is found in a package.

static _get_partition_key(partition)[source]#

Grab the partition key.

class pudl.extract.excel.GenericExtractor(ds)[source]#

Contains logic for extracting panda.DataFrames from excel spreadsheets.

This class implements the generic dataset agnostic logic to load data from excel spreadsheet simply by using excel Metadata for given dataset.

It is expected that individual datasets wil subclass this code and add custom business logic by overriding necessary methods.

When implementing custom business logic, the following should be modified:

  1. DATASET class attribute controls which excel metadata should be loaded.

2. BLACKLISTED_PAGES class attribute specifies which pages should not be loaded from the underlying excel files even if the metadata is available. This can be used for experimental/new code that should not be run yet.

3. dtypes() should return dict with {column_name: pandas_datatype} if you need to specify which datatypes should be uded upon loading.

4. If data cleanup is necessary, you can apply custom logic by overriding one of the following functions (they all return the modified dataframe):

  • process_raw() is applied right after loading the excel DataFrame from the disk.

  • process_renamed() is applied after input columns were renamed to standardized pudl columns.

  • process_final_page() is applied when data from all available years is merged into single DataFrame for a given page.

5. get_datapackage_resources() if partition is anything other than a year, this method should be overwritten in the dataset-specific extractor.

METADATA[source]#

Instance of metadata object to use with this extractor.

BLACKLISTED_PAGES = [][source]#

List of supported pages that should not be extracted.

process_raw(df, page, **partition)[source]#

Transforms raw dataframe and rename columns.

static process_renamed(df, page, **partition)[source]#

Transforms dataframe after columns are renamed.

static process_final_page(df, page)[source]#

Final processing stage applied to a page DataFrame.

static get_dtypes(page, **partition)[source]#

Provide custom dtypes for given page and partition.

extract(**partitions)[source]#

Extracts dataframes.

Returns dict where keys are page names and values are DataFrames containing data across given years.

Parameters:

partitions (list, tuple or string) – list of partitions to extract. (Ex: [2009, 2010] if dataset is partitioned by years or ‘2020-08’ if dataset is partitioned by year_month)

load_excel_file(page, **partition)[source]#

Produce the ExcelFile object for the given (partition, page).

Parameters:
  • page (str) – pudl name for the dataset contents, eg “boiler_generator_assn” or “coal_stocks”

  • partition – partition to load. (ex: 2009 for year partition or “2020-08” for year_month partition)

Returns:

pd.ExcelFile instance with the parsed excel spreadsheet frame

excel_filename(page, **partition)[source]#

Produce the xlsx document file name as it will appear in the archive.

Parameters:
  • page – pudl name for the dataset contents, eg “boiler_generator_assn” or “coal_stocks”

  • partition – partition to load. (ex: 2009 for year partition or “2020-08” for year_month partition)

Returns:

string name of the xlsx file