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 theindexVisitsoption is used.The
onlyRealVisitsargument 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 theinstancecomponent of the column name (see theparseColumnName()function for details).- Parameters:
cols – list of
Columnobjects dewscribing the existing data.vartable –
pandas.DataFramecontaining the variable tableonlyRealVisits – Re-index all columns according to the
instancecomponent of their column names, not just columns associated with variables that follow instancing 2.
- Returns:
Noneif the file does not contain any data with multiple visits, or atuplecontaining:list of
Columnobjects describing the re-indexed data set.Dict of
{old Column : new Column}mappings, describing how the old data maps to the new data.A
setcontaining 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, andvisitsarguments.The given dataframe is assumed to be indexed by a single-level
pandas.IndexThis 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