| Conditions | 5 |
| Total Lines | 70 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | """ |
||
| 55 | def _diff_report( |
||
| 56 | data: pd.DataFrame, |
||
| 57 | data_cleaned: pd.DataFrame, |
||
| 58 | dupl_rows: Optional[List[Union[str, int]]] = None, |
||
| 59 | single_val_cols: Optional[List[str]] = None, |
||
| 60 | show: Optional[str] = "changes", # Optional[Literal["all", "changes"]] = "changes", |
||
| 61 | ) -> None: |
||
| 62 | """ Provides information about changes between two datasets, such as dropped rows and columns, memory usage and \ |
||
| 63 | missing values. |
||
| 64 | |||
| 65 | Parameters |
||
| 66 | ---------- |
||
| 67 | data : pd.DataFrame |
||
| 68 | 2D dataset that can be coerced into Pandas DataFrame. Input the initial dataset here |
||
| 69 | data_cleaned : pd.DataFrame |
||
| 70 | 2D dataset that can be coerced into Pandas DataFrame. Input the cleaned / updated dataset here |
||
| 71 | dupl_rows : Optional[List[Union[str, int]]], optional |
||
| 72 | List of duplicate row indices, by default None |
||
| 73 | single_val_cols : Optional[List[str]], optional |
||
| 74 | List of single-valued column indices. I.e. columns where all cells contain the same value. \ |
||
| 75 | NaNs count as a separate value, by default None |
||
| 76 | show : str, optional |
||
| 77 | {'all', 'changes', None}, by default "changes" |
||
| 78 | Specify verbosity of the output: |
||
| 79 | * 'all': Print information about the data before and after cleaning as well as information about changes \ |
||
| 80 | and memory usage (deep). Please be aware, that this can slow down the function by quite a bit. |
||
| 81 | * 'changes': Print out differences in the data before and after cleaning. |
||
| 82 | * None: No information about the data and the data cleaning is printed. |
||
| 83 | |||
| 84 | Returns |
||
| 85 | ------- |
||
| 86 | None |
||
| 87 | Print statement highlighting the datasets or changes between the two datasets. |
||
| 88 | """ |
||
| 89 | |||
| 90 | if show in ["changes", "all"]: |
||
| 91 | dupl_rows = [] if dupl_rows is None else dupl_rows.copy() |
||
| 92 | single_val_cols = [] if single_val_cols is None else single_val_cols.copy() |
||
| 93 | data_mem = _memory_usage(data, deep=False) |
||
| 94 | data_cl_mem = _memory_usage(data_cleaned, deep=False) |
||
| 95 | data_mv_tot = _missing_vals(data)["mv_total"] |
||
| 96 | data_cl_mv_tot = _missing_vals(data_cleaned)["mv_total"] |
||
| 97 | |||
| 98 | if show == "all": |
||
| 99 | data_mem = _memory_usage(data, deep=True) |
||
| 100 | data_cl_mem = _memory_usage(data_cleaned, deep=True) |
||
| 101 | print("Before data cleaning:\n") |
||
| 102 | print(f"dtypes:\n{data.dtypes.value_counts()}") |
||
| 103 | print(f"\nNumber of rows: {data.shape[0]}") |
||
| 104 | print(f"Number of cols: {data.shape[1]}") |
||
| 105 | print(f"Missing values: {data_mv_tot}") |
||
| 106 | print(f"Memory usage: {data_mem} MB") |
||
| 107 | print("_______________________________________________________\n") |
||
| 108 | print("After data cleaning:\n") |
||
| 109 | print(f"dtypes:\n{data_cleaned.dtypes.value_counts()}") |
||
| 110 | print(f"\nNumber of rows: {data_cleaned.shape[0]}") |
||
| 111 | print(f"Number of cols: {data_cleaned.shape[1]}") |
||
| 112 | print(f"Missing values: {data_cl_mv_tot}") |
||
| 113 | print(f"Memory usage: {data_cl_mem} MB") |
||
| 114 | print("_______________________________________________________\n") |
||
| 115 | |||
| 116 | print(f"Shape of cleaned data: {data_cleaned.shape} - Remaining NAs: {data_cl_mv_tot}") |
||
| 117 | print("\nChanges:") |
||
| 118 | print(f"Dropped rows: {data.shape[0]-data_cleaned.shape[0]}") |
||
| 119 | print(f" of which {len(dupl_rows)} duplicates. (Rows: {dupl_rows})") |
||
| 120 | print(f"Dropped columns: {data.shape[1]-data_cleaned.shape[1]}") |
||
| 121 | print(f" of which {len(single_val_cols)} single valued. (Columns: {single_val_cols})") |
||
| 122 | print(f"Dropped missing values: {data_mv_tot-data_cl_mv_tot}") |
||
| 123 | mem_change = data_mem - data_cl_mem |
||
| 124 | print(f"Reduced memory by at least: {round(mem_change,3)} MB (-{round(100*mem_change/data_mem,2)}%)") |
||
| 125 | |||
| 227 |