neurotools.util.pandas module
- neurotools.util.pandas.get(df, col, val, drop=True)[source]
Get all rows with column matching value (discards the column matched)
- Parameters:
df (pandas.DataFrame)
- Return type:
pd.DataFrame
- neurotools.util.pandas.is_empty_col(c)[source]
Check if a value is
NaN
- Parameters:
c (object) – tuple of
str
ornan
representing multi- level column name.- Returns:
is_empty
- Return type:
boolean
- neurotools.util.pandas.from_hierarchical_columns(df, delimeter='__', deep=False)[source]
Flatten hierarchical columns into strings. Inverse of
to_hierarchical_columns
.- Parameters:
df (pandas.DataFrame)
delimeter (str, default '__') – String delimiter for hierarchical column levels.
deep (boolean, default False) – Whether retured array is a deep copy.
- Return type:
pd.DataFrame
- neurotools.util.pandas.to_ndarray(df, flatten_columns=True)[source]
Convert DataFrame to structured np.ndarray.
- Parameters:
df (pandas.DataFrame)
flatten_columns (boolean; default True) – Whether to flatten hierarchical columns by merging the coulmn-name tuples using the delimeter
'__'
.
- Returns:
Dataframe as numpy structured array
- Return type:
np.ndarray
- neurotools.util.pandas.camelcase_columns(df)[source]
Switch column names from Python’s
underscore_convention
to the Java/Matlab stylelowerCaseCamelCase
convention.Your colleagues working in Matlab may appreciate this. This frees up underscore
_
to use as a delimeter for hierarchical columns. Inverse ofsnakecase_columns(df)
.- Parameters:
df (pandas.DataFrame)
- Return type:
pd.DataFrame
- neurotools.util.pandas.snakecase_columns(df)[source]
Switch column names from
underscoreDelimited_camelCase
to flattened-hieraricalsnake_case__columns
. Inverse ofcamelcase_columns(df)
.- Parameters:
df (pandas.DataFrame)
- Return type:
pd.DataFrame
- neurotools.util.pandas.ndarray_from_mat(filename, key=None)[source]
Load structured
np.ndarray
from matfile- Parameters:
filename (str) – Matfile path to load
flatten_columns (boolean; default True) – Whether to flatten hierarchical columns by merging the coulmn-name tuples using the delimeter
'__'
.
- Returns:
data (np.ndarray) – numpy structured array
readme (str) –
README
string from this matfile, if present.
- neurotools.util.pandas.read_mat(filename, key=None, restore_hierarchical_columns=True, delimeter='__')[source]
Load DataFrame from matfile.
- Parameters:
filename (str) – Matfile path to load
key (str; default None) – Name of variable to load from matfile. If none, expects matfile containing only a single variable.
restore_hierarchical_columns (boolean; default True) –
to_mat
flattens hierarchical columns by merging the coulmn-name tuples using the delimeter'__'
. If true, we will restore the hierarchical column structre in the loaded DataFrame.delimeter (str, default '__') – String delimiter for hierarchical column levels.
- Return type:
pd.DataFrame
- neurotools.util.pandas.to_mat(df, saveas, tablename='data', readme=None)[source]
Save dataframe as
.mat
matfile.- Parameters:
df (pandas.DataFrame)
saveas (str) – File path to save to
tablename (str; default 'data') – Variable name to use for dataframe in the matfile.
readme (str; default None) – Optional string to save in a
README
variable to document the contents of this.mat
archive.
- neurotools.util.pandas.conform(x, dtype=<class 'numpy.float32'>, fill_value=nan)[source]
Recursively coerce nested iterables into a numpy array with a uniform shape by padding uneven shapes with
np.NaN
or~0
.Useful for ensuring columns have a uniform type signature.
- Parameters:
x (iterable) – Hierarichal (nested) iterable structure that we wish to coerce into a single
np.ndarray
dtype (np.dtype; default np.float32) – Desired type of the resulting array. Float and int types are supported. Float types will pad with
np.NaN
. Int types will pad with~0
.fill_value (number; default np.NaN)
- Return type:
np.ndarray