Utilities#
Various utilities of this package.
Warning
These utilities are less aimed at end users and might therefore be subject to change.
- ankipandas.util.log.set_log_level(level: str | int) None [source]#
Set global log level.
- Parameters:
level – Either an int (https://docs.python.org/3/library/logging.html#levels) or one of the keywords, ‘critical’ (only the most terrifying of log messages), ‘error’, ‘warning’, ‘info’, ‘debug’ (all log messages)
- Returns:
None
DataFrame utilities.
- ankipandas.util.dataframe.replace_df_inplace(df: DataFrame, df_new: DataFrame) None [source]#
Replace dataframe ‘in place’. If the dataframe has a _metadata field, containing a list of attribute names that contain metadata, then this is copied from df to the new dataframe.
- Parameters:
df –
pandas.DataFrame
to be replaceddf_new –
pandas.DataFrame
to replace the previous one
- Returns:
None
- ankipandas.util.dataframe.merge_dfs(df: DataFrame, df_add: DataFrame, id_df: str, inplace=False, id_add='id', prepend='', replace=False, prepend_clash_only=True, columns=None, drop_columns=None) DataFrame | None [source]#
Merge information from two dataframes. If the dataframe has a _metadata field, containing a list of attribute names that contain metadata, then this is copied from df to the new dataframe.
- Parameters:
df – Original
pandas.DataFrame
df_add –
pandas.DataFrame
to be merged with originalpandas.DataFrame
id_df – Column of original dataframe that contains the id along which we merge.
inplace – If False, return new dataframe, else update old one
id_add – Column of the new dataframe that contains the id along which we merge
prepend – Prepend a string to the column names from the new dataframe
replace – Replace columns
prepend_clash_only – Only prepend string to the column names from the new dataframe if there is a name clash.
columns – Keep only these columns
drop_columns – Drop these columns
- Returns:
New merged
pandas.DataFrame
- ankipandas.util.misc.invert_dict(dct: dict) dict [source]#
Invert dictionary, i.e. reverse keys and values.
- Parameters:
dct – Dictionary
- Returns:
Dictionary with reversed keys and values.
- Raises:
- ankipandas.util.misc.flatten_list_list(lst: list[list[Any]]) list[Any] [source]#
Takes a list of lists and returns a list of all elements.
- Parameters:
lst – List of Lists
- Returns:
list
- ankipandas.util.misc.nested_dict()[source]#
This is very clever and stolen from https://stackoverflow.com/questions/16724788/ Use it to initialize a dictionary-like object which automatically adds levels. E.g.
a = nested_dict() a['test']['this']['is']['working'] = "yaaay"
- ankipandas.util.misc.defaultdict2dict(defdict: defaultdict) dict [source]#