radis.misc.basics module¶
Created on Wed Nov 5 12:59:37 2014.
@author: Erwan Small functions used in other procedures ——————————————————————————-
- compare_dict(d1, d2, verbose='if_different', compare_as_paths=[], compare_as_close=[], return_string=False, df1_str='Left', df2_str='Right', ignore_keys=[])[source]¶
Returns ratio of equal keys [0-1] If verbose, also print all keys and values on 2 columns :Parameters: d1, d2 (dict) – two dictionaries to compare
- Other Parameters:
compare_as_paths (list of keys) – compare the values corresponding to given keys as path (irrespective of forward / backward slashes, or case )
compare_as_close (list of keys) – compare with
np.isclose(a,b)
rather thana==b
verbose (boolean, or
'if_different'
) –'if_different'
means results will be shown only if there is a difference.return_string (boolean) – if
True
, returns message instead of just printing it (useful in error messages) DefaultFalse
ignore_keys (list) – do not compare these keys
- Returns:
out (float [0-1]) – ratio of matching keys
if
return_string
out, string (float [0-1], str) – ratio of matching keys and comparison message
- compare_lists(l1, l2, verbose='if_different', return_string=False, l1_str='Left', l2_str='Right', print_index=False)[source]¶
Compare 2 lists of elements that may not be of the same length, irrespective of order. Returns the ratio of elements [0-1] present in both lists. If verbose, prints the differences
- Parameters:
l1, l2 (list-like)
verbose (boolean, or ‘if_different’) – ‘if_different’ means results will be shown only if there is a difference. function is called twice
- Other Parameters:
verbose (boolean, or
'if_different'
) –'if_different'
means results will be shown only if there is a difference.return_string (boolean) – if
True
, returns message instead of just printing it (useful in error messages) DefaultFalse
- Returns:
out – ratio of matching keys
- Return type:
float [0-1]
- expand_metadata(df, metadata)[source]¶
Turn metadata from a float to a column.
For some reason metadata are sometimes not copied when a DataFrame is sliced or copied, even if they explicitly figure in the df.attrs attribute. Here we add them as column before such operations.
- Parameters:
df (pandas DataFrame) – …
- Returns:
df modified in place
- Return type:
None
- is_number(s)[source]¶
Return True if
s
is a number.Works for strings, floats, int, and is compatible with Python 2/3
- make_folders(path, folders)[source]¶
Make folders if not there :Parameters: * path (str) – where to create folders
folders (list or str) – folders to create
- merge_rename_columns(df, columns1, columns2, merged_names)[source]¶
Merge all columns under easier names. Only keep the useful ones Returns a new dataframe :Parameters: * df (pandas Dataframe)
columns1 (list) – list of columns names
columns2 (list) – list of columns names, whose index match columns 1
merged_names (list) – new names
Example
- df = merge_rename_columns(df1, [‘lvl_u’, ‘ju’, ‘Eu’, ‘nu’, ‘gu’, ‘grotu’],
[‘lvl_l’, ‘jl’, ‘El’, ‘nl’, ‘gl’, ‘grotl’], [‘lvl’, ‘j’, ‘E’, ‘n’, ‘g’, ‘grot’] )
- partition(pred, iterable)[source]¶
Use a predicate to partition entries into false entries and true entries :returns: Returns two lists :rtype: positive, and negative
Example
>>> partition(is_odd, range(10)) --> [0 2 4 6 8], [1 3 5 7 9]
- remove_duplicates(l)[source]¶
Remove duplicates from a list, without changing the order.
Note that if the order doesn’t matter you could just do set(l)
- str2bool(s)[source]¶
Used to convert Pandas columns in str type to boolean (note that by default bool(“False”)==True !)
- transfer_metadata(df1, df2, metadata)[source]¶
Transfer metadata between a DataFrame df1 and df2.
For some reason metadata are sometimes not copied when a DataFrame is sliced or copied, even if they explicitly appear in the
df.attrs
attribute. See https://github.com/pandas-dev/pandas/issues/28283- Here we copy them back. Attributes can be :
keys of the
df1.attrs
dictionarysimple attributes of
df1
, i.e.,df1.X
- Parameters:
df1 (pandas DataFrame) – copy from df1
df2 (pandas DataFrame) – copy to df2