funpack.importing.reindex

This module contains functions used by the core.importData() function, for re-arranging a data frame that is indexed by subject into a data frame that is indexed by subject and visit.

funpack.importing.reindex.genReindexedColumns(cols, vartable, onlyRealVisits=True)[source]

Figures out how to re-arrange the columns of a data set so that it is indexed by row ID and visit. This function is called by loadFile() when the indexVisits option is used.

The onlyRealVisits argument controls whether the re-indexing is only applied to variables which are labelled with instancing 2 (Biobank assessment centre visit), or is solely based on the instance component of the column name (see the parseColumnName() function for details).

Parameters:
  • cols – list of Column objects dewscribing the existing data.

  • vartablepandas.DataFrame containing the variable table

  • onlyRealVisits – Re-index all columns according to the instance component of their column names, not just columns associated with variables that follow instancing 2.

Returns:

None if the file does not contain any data with multiple visits, or a tuple containing:

  • list of Column objects describing the re-indexed data set.

  • Dict of {old Column : new Column} mappings, describing how the old data maps to the new data.

  • A set containing all visit codes in the data.

funpack.importing.reindex.reindexByVisit(df, oldcols, newcols, oldnewmap, visits)[source]

Re-arranges the data so that visits form part of the row indices, rather than being stored in separate columns for each variable.

The generateReindexedColumns() function is used to create the newcols, oldnewmap, and visits arguments.

The given dataframe is assumed to be indexed by a single-level pandas.Index This is replaced with a pandas.MultiIndex, where the first level corresponds to the original index, and the second level to the visit number.

Parameters:
  • dfpandas.DataFrame containing the data.

  • oldcols – list of Column objects describing the existing data.

  • newcols – List of Column objects decsribing each column in the new adjusted data.

:arg oldnewmap Dict of {old Column : new Column} mappings, describing

how the old data maps to the new data.

Returns:

adjusted pandas.DataFrame