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 theindexVisits
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 theinstance
component of the column name (see theparseColumnName()
function for details).- Parameters:
cols – list of
Column
objects dewscribing the existing data.vartable –
pandas.DataFrame
containing the variable tableonlyRealVisits – 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 atuple
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 thenewcols
,oldnewmap
, andvisits
arguments.The given dataframe is assumed to be indexed by a single-level
pandas.Index
This is replaced with apandas.MultiIndex
, where the first level corresponds to the original index, and the second level to the visit number.- Parameters:
- :arg oldnewmap Dict of
{old Column : new Column}
mappings, describing how the old data maps to the new data.
- Returns:
adjusted
pandas.DataFrame