polars-data-profiling 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (207) hide show
  1. polars_data_profiling/__init__.py +19 -0
  2. polars_data_profiling/compare_reports.py +386 -0
  3. polars_data_profiling/config.py +558 -0
  4. polars_data_profiling/config_default.yaml +219 -0
  5. polars_data_profiling/config_minimal.yaml +218 -0
  6. polars_data_profiling/controller/__init__.py +1 -0
  7. polars_data_profiling/controller/console.py +127 -0
  8. polars_data_profiling/controller/namespace.py +38 -0
  9. polars_data_profiling/model/__init__.py +5 -0
  10. polars_data_profiling/model/alerts.py +779 -0
  11. polars_data_profiling/model/correlations.py +332 -0
  12. polars_data_profiling/model/dataframe.py +24 -0
  13. polars_data_profiling/model/describe.py +197 -0
  14. polars_data_profiling/model/description.py +106 -0
  15. polars_data_profiling/model/duplicates.py +64 -0
  16. polars_data_profiling/model/engine.py +192 -0
  17. polars_data_profiling/model/expressions.py +215 -0
  18. polars_data_profiling/model/handler.py +84 -0
  19. polars_data_profiling/model/images.py +179 -0
  20. polars_data_profiling/model/missing.py +161 -0
  21. polars_data_profiling/model/pairwise.py +59 -0
  22. polars_data_profiling/model/probe.py +217 -0
  23. polars_data_profiling/model/sample.py +77 -0
  24. polars_data_profiling/model/summaries.py +641 -0
  25. polars_data_profiling/model/summarizer.py +177 -0
  26. polars_data_profiling/model/summary.py +143 -0
  27. polars_data_profiling/model/summary_algorithms.py +301 -0
  28. polars_data_profiling/model/table.py +47 -0
  29. polars_data_profiling/model/timeseries.py +312 -0
  30. polars_data_profiling/model/timeseries_index.py +48 -0
  31. polars_data_profiling/model/type_rules.py +35 -0
  32. polars_data_profiling/model/typeset.py +262 -0
  33. polars_data_profiling/model/typeset_base.py +367 -0
  34. polars_data_profiling/model/typeset_relations.py +132 -0
  35. polars_data_profiling/profile_report.py +508 -0
  36. polars_data_profiling/py.typed +0 -0
  37. polars_data_profiling/report/__init__.py +5 -0
  38. polars_data_profiling/report/formatters.py +344 -0
  39. polars_data_profiling/report/presentation/__init__.py +1 -0
  40. polars_data_profiling/report/presentation/core/__init__.py +43 -0
  41. polars_data_profiling/report/presentation/core/alerts.py +18 -0
  42. polars_data_profiling/report/presentation/core/collapse.py +25 -0
  43. polars_data_profiling/report/presentation/core/container.py +51 -0
  44. polars_data_profiling/report/presentation/core/correlation_table.py +21 -0
  45. polars_data_profiling/report/presentation/core/dropdown.py +45 -0
  46. polars_data_profiling/report/presentation/core/duplicate.py +16 -0
  47. polars_data_profiling/report/presentation/core/frequency_table.py +14 -0
  48. polars_data_profiling/report/presentation/core/frequency_table_small.py +16 -0
  49. polars_data_profiling/report/presentation/core/html.py +14 -0
  50. polars_data_profiling/report/presentation/core/image.py +34 -0
  51. polars_data_profiling/report/presentation/core/item_renderer.py +16 -0
  52. polars_data_profiling/report/presentation/core/renderable.py +42 -0
  53. polars_data_profiling/report/presentation/core/root.py +36 -0
  54. polars_data_profiling/report/presentation/core/sample.py +29 -0
  55. polars_data_profiling/report/presentation/core/scores.py +33 -0
  56. polars_data_profiling/report/presentation/core/table.py +27 -0
  57. polars_data_profiling/report/presentation/core/toggle_button.py +14 -0
  58. polars_data_profiling/report/presentation/core/variable.py +51 -0
  59. polars_data_profiling/report/presentation/core/variable_info.py +36 -0
  60. polars_data_profiling/report/presentation/flavours/__init__.py +9 -0
  61. polars_data_profiling/report/presentation/flavours/flavour_html.py +65 -0
  62. polars_data_profiling/report/presentation/flavours/flavour_widget.py +62 -0
  63. polars_data_profiling/report/presentation/flavours/flavours.py +42 -0
  64. polars_data_profiling/report/presentation/flavours/html/__init__.py +57 -0
  65. polars_data_profiling/report/presentation/flavours/html/alerts.py +10 -0
  66. polars_data_profiling/report/presentation/flavours/html/collapse.py +7 -0
  67. polars_data_profiling/report/presentation/flavours/html/container.py +85 -0
  68. polars_data_profiling/report/presentation/flavours/html/correlation_table.py +21 -0
  69. polars_data_profiling/report/presentation/flavours/html/dropdown.py +7 -0
  70. polars_data_profiling/report/presentation/flavours/html/duplicate.py +23 -0
  71. polars_data_profiling/report/presentation/flavours/html/frame_table.py +61 -0
  72. polars_data_profiling/report/presentation/flavours/html/frequency_table.py +20 -0
  73. polars_data_profiling/report/presentation/flavours/html/frequency_table_small.py +15 -0
  74. polars_data_profiling/report/presentation/flavours/html/html.py +6 -0
  75. polars_data_profiling/report/presentation/flavours/html/image.py +7 -0
  76. polars_data_profiling/report/presentation/flavours/html/root.py +15 -0
  77. polars_data_profiling/report/presentation/flavours/html/sample.py +17 -0
  78. polars_data_profiling/report/presentation/flavours/html/scores.py +12 -0
  79. polars_data_profiling/report/presentation/flavours/html/table.py +7 -0
  80. polars_data_profiling/report/presentation/flavours/html/templates/alerts/alert_constant.html +1 -0
  81. polars_data_profiling/report/presentation/flavours/html/templates/alerts/alert_constant_length.html +1 -0
  82. polars_data_profiling/report/presentation/flavours/html/templates/alerts/alert_dirty_category.html +1 -0
  83. polars_data_profiling/report/presentation/flavours/html/templates/alerts/alert_duplicates.html +1 -0
  84. polars_data_profiling/report/presentation/flavours/html/templates/alerts/alert_empty.html +1 -0
  85. polars_data_profiling/report/presentation/flavours/html/templates/alerts/alert_high_cardinality.html +1 -0
  86. polars_data_profiling/report/presentation/flavours/html/templates/alerts/alert_high_correlation.html +4 -0
  87. polars_data_profiling/report/presentation/flavours/html/templates/alerts/alert_imbalance.html +1 -0
  88. polars_data_profiling/report/presentation/flavours/html/templates/alerts/alert_infinite.html +1 -0
  89. polars_data_profiling/report/presentation/flavours/html/templates/alerts/alert_missing.html +1 -0
  90. polars_data_profiling/report/presentation/flavours/html/templates/alerts/alert_near_duplicates.html +1 -0
  91. polars_data_profiling/report/presentation/flavours/html/templates/alerts/alert_non_stationary.html +1 -0
  92. polars_data_profiling/report/presentation/flavours/html/templates/alerts/alert_seasonal.html +1 -0
  93. polars_data_profiling/report/presentation/flavours/html/templates/alerts/alert_skewed.html +1 -0
  94. polars_data_profiling/report/presentation/flavours/html/templates/alerts/alert_truncated.html +1 -0
  95. polars_data_profiling/report/presentation/flavours/html/templates/alerts/alert_type_date.html +1 -0
  96. polars_data_profiling/report/presentation/flavours/html/templates/alerts/alert_uniform.html +1 -0
  97. polars_data_profiling/report/presentation/flavours/html/templates/alerts/alert_unique.html +1 -0
  98. polars_data_profiling/report/presentation/flavours/html/templates/alerts/alert_unsupported.html +1 -0
  99. polars_data_profiling/report/presentation/flavours/html/templates/alerts/alert_zeros.html +1 -0
  100. polars_data_profiling/report/presentation/flavours/html/templates/alerts.html +47 -0
  101. polars_data_profiling/report/presentation/flavours/html/templates/collapse.html +31 -0
  102. polars_data_profiling/report/presentation/flavours/html/templates/correlation_table.html +5 -0
  103. polars_data_profiling/report/presentation/flavours/html/templates/diagram.html +13 -0
  104. polars_data_profiling/report/presentation/flavours/html/templates/dropdown.html +25 -0
  105. polars_data_profiling/report/presentation/flavours/html/templates/duplicate.html +5 -0
  106. polars_data_profiling/report/presentation/flavours/html/templates/frequency_table.html +45 -0
  107. polars_data_profiling/report/presentation/flavours/html/templates/frequency_table_small.html +34 -0
  108. polars_data_profiling/report/presentation/flavours/html/templates/report.html +42 -0
  109. polars_data_profiling/report/presentation/flavours/html/templates/sample.html +10 -0
  110. polars_data_profiling/report/presentation/flavours/html/templates/scores.html +78 -0
  111. polars_data_profiling/report/presentation/flavours/html/templates/sequence/batch_grid.html +16 -0
  112. polars_data_profiling/report/presentation/flavours/html/templates/sequence/cards.html +41 -0
  113. polars_data_profiling/report/presentation/flavours/html/templates/sequence/correlations.html +53 -0
  114. polars_data_profiling/report/presentation/flavours/html/templates/sequence/grid.html +39 -0
  115. polars_data_profiling/report/presentation/flavours/html/templates/sequence/interactions.html +45 -0
  116. polars_data_profiling/report/presentation/flavours/html/templates/sequence/list.html +7 -0
  117. polars_data_profiling/report/presentation/flavours/html/templates/sequence/named_list.html +8 -0
  118. polars_data_profiling/report/presentation/flavours/html/templates/sequence/overview_tabs.html +47 -0
  119. polars_data_profiling/report/presentation/flavours/html/templates/sequence/scores.html +3 -0
  120. polars_data_profiling/report/presentation/flavours/html/templates/sequence/sections.html +15 -0
  121. polars_data_profiling/report/presentation/flavours/html/templates/sequence/select.html +40 -0
  122. polars_data_profiling/report/presentation/flavours/html/templates/sequence/tabs.html +30 -0
  123. polars_data_profiling/report/presentation/flavours/html/templates/table.html +38 -0
  124. polars_data_profiling/report/presentation/flavours/html/templates/toggle_button.html +18 -0
  125. polars_data_profiling/report/presentation/flavours/html/templates/variable.html +7 -0
  126. polars_data_profiling/report/presentation/flavours/html/templates/variable_info.html +49 -0
  127. polars_data_profiling/report/presentation/flavours/html/templates/wrapper/assets/bootstrap.bundle.min.js +7 -0
  128. polars_data_profiling/report/presentation/flavours/html/templates/wrapper/assets/bootstrap.min.css +6 -0
  129. polars_data_profiling/report/presentation/flavours/html/templates/wrapper/assets/cosmo.bootstrap.min.css +12 -0
  130. polars_data_profiling/report/presentation/flavours/html/templates/wrapper/assets/flatly.bootstrap.min.css +12 -0
  131. polars_data_profiling/report/presentation/flavours/html/templates/wrapper/assets/script.js +398 -0
  132. polars_data_profiling/report/presentation/flavours/html/templates/wrapper/assets/simplex.bootstrap.min.css +12 -0
  133. polars_data_profiling/report/presentation/flavours/html/templates/wrapper/assets/style.css +1455 -0
  134. polars_data_profiling/report/presentation/flavours/html/templates/wrapper/assets/united.bootstrap.min.css +12 -0
  135. polars_data_profiling/report/presentation/flavours/html/templates/wrapper/footer.html +6 -0
  136. polars_data_profiling/report/presentation/flavours/html/templates/wrapper/javascript.html +18 -0
  137. polars_data_profiling/report/presentation/flavours/html/templates/wrapper/navigation.html +22 -0
  138. polars_data_profiling/report/presentation/flavours/html/templates/wrapper/style.html +20 -0
  139. polars_data_profiling/report/presentation/flavours/html/templates.py +79 -0
  140. polars_data_profiling/report/presentation/flavours/html/theme.py +74 -0
  141. polars_data_profiling/report/presentation/flavours/html/toggle_button.py +7 -0
  142. polars_data_profiling/report/presentation/flavours/html/variable.py +7 -0
  143. polars_data_profiling/report/presentation/flavours/html/variable_info.py +7 -0
  144. polars_data_profiling/report/presentation/flavours/widget/__init__.py +59 -0
  145. polars_data_profiling/report/presentation/flavours/widget/alerts.py +43 -0
  146. polars_data_profiling/report/presentation/flavours/widget/collapse.py +43 -0
  147. polars_data_profiling/report/presentation/flavours/widget/container.py +127 -0
  148. polars_data_profiling/report/presentation/flavours/widget/correlation_table.py +16 -0
  149. polars_data_profiling/report/presentation/flavours/widget/dropdown.py +31 -0
  150. polars_data_profiling/report/presentation/flavours/widget/duplicate.py +14 -0
  151. polars_data_profiling/report/presentation/flavours/widget/frequency_table.py +57 -0
  152. polars_data_profiling/report/presentation/flavours/widget/frequency_table_small.py +64 -0
  153. polars_data_profiling/report/presentation/flavours/widget/html.py +11 -0
  154. polars_data_profiling/report/presentation/flavours/widget/image.py +29 -0
  155. polars_data_profiling/report/presentation/flavours/widget/notebook.py +82 -0
  156. polars_data_profiling/report/presentation/flavours/widget/root.py +10 -0
  157. polars_data_profiling/report/presentation/flavours/widget/sample.py +14 -0
  158. polars_data_profiling/report/presentation/flavours/widget/table.py +30 -0
  159. polars_data_profiling/report/presentation/flavours/widget/toggle_button.py +17 -0
  160. polars_data_profiling/report/presentation/flavours/widget/variable.py +12 -0
  161. polars_data_profiling/report/presentation/flavours/widget/variable_info.py +11 -0
  162. polars_data_profiling/report/presentation/frequency_table_utils.py +169 -0
  163. polars_data_profiling/report/structure/__init__.py +1 -0
  164. polars_data_profiling/report/structure/correlations.py +202 -0
  165. polars_data_profiling/report/structure/overview.py +506 -0
  166. polars_data_profiling/report/structure/report.py +547 -0
  167. polars_data_profiling/report/structure/variables/__init__.py +41 -0
  168. polars_data_profiling/report/structure/variables/render_boolean.py +130 -0
  169. polars_data_profiling/report/structure/variables/render_categorical.py +562 -0
  170. polars_data_profiling/report/structure/variables/render_common.py +34 -0
  171. polars_data_profiling/report/structure/variables/render_complex.py +102 -0
  172. polars_data_profiling/report/structure/variables/render_count.py +172 -0
  173. polars_data_profiling/report/structure/variables/render_date.py +147 -0
  174. polars_data_profiling/report/structure/variables/render_file.py +68 -0
  175. polars_data_profiling/report/structure/variables/render_generic.py +45 -0
  176. polars_data_profiling/report/structure/variables/render_image.py +202 -0
  177. polars_data_profiling/report/structure/variables/render_path.py +138 -0
  178. polars_data_profiling/report/structure/variables/render_real.py +314 -0
  179. polars_data_profiling/report/structure/variables/render_text.py +213 -0
  180. polars_data_profiling/report/structure/variables/render_timeseries.py +371 -0
  181. polars_data_profiling/report/structure/variables/render_url.py +132 -0
  182. polars_data_profiling/report/utils.py +32 -0
  183. polars_data_profiling/serialize_report.py +144 -0
  184. polars_data_profiling/utils/__init__.py +1 -0
  185. polars_data_profiling/utils/collect.py +41 -0
  186. polars_data_profiling/utils/common.py +29 -0
  187. polars_data_profiling/utils/dataframe.py +166 -0
  188. polars_data_profiling/utils/notebook.py +8 -0
  189. polars_data_profiling/utils/paths.py +46 -0
  190. polars_data_profiling/utils/progress_bar.py +16 -0
  191. polars_data_profiling/utils/styles.py +22 -0
  192. polars_data_profiling/version.py +8 -0
  193. polars_data_profiling/visualisation/__init__.py +1 -0
  194. polars_data_profiling/visualisation/charts/__init__.py +16 -0
  195. polars_data_profiling/visualisation/charts/builders.py +631 -0
  196. polars_data_profiling/visualisation/charts/palette.py +129 -0
  197. polars_data_profiling/visualisation/charts/scales.py +204 -0
  198. polars_data_profiling/visualisation/charts/specs.py +249 -0
  199. polars_data_profiling/visualisation/charts/svg.py +1336 -0
  200. polars_data_profiling/visualisation/missing.py +34 -0
  201. polars_data_profiling/visualisation/plot.py +142 -0
  202. polars_data_profiling-0.1.0.dist-info/METADATA +131 -0
  203. polars_data_profiling-0.1.0.dist-info/RECORD +207 -0
  204. polars_data_profiling-0.1.0.dist-info/WHEEL +4 -0
  205. polars_data_profiling-0.1.0.dist-info/entry_points.txt +3 -0
  206. polars_data_profiling-0.1.0.dist-info/licenses/LICENSE +22 -0
  207. polars_data_profiling-0.1.0.dist-info/licenses/NOTICE +26 -0
@@ -0,0 +1,19 @@
1
+ """Polars-native data profiling.
2
+
3
+ Profile a ``pl.DataFrame`` or ``pl.LazyFrame`` into an HTML or JSON report::
4
+
5
+ import polars as pl
6
+ from polars_data_profiling import ProfileReport
7
+
8
+ ProfileReport(pl.scan_parquet("data.parquet"), title="Data").to_file("report.html")
9
+
10
+ Importing the package also registers the ``profile`` namespace on Polars
11
+ frames: ``df.profile.report(...)``.
12
+ """
13
+
14
+ from polars_data_profiling.profile_report import ProfileReport # isort: skip
15
+ from polars_data_profiling.compare_reports import compare
16
+ from polars_data_profiling.controller import namespace # noqa: F401 (registers df.profile)
17
+ from polars_data_profiling.version import __version__
18
+
19
+ __all__ = ["ProfileReport", "__version__", "compare"]
@@ -0,0 +1,386 @@
1
+ import json
2
+ import warnings
3
+ from dataclasses import asdict
4
+ from typing import Any
5
+
6
+ import polars as pl
7
+
8
+ from polars_data_profiling.config import Correlation, Settings
9
+ from polars_data_profiling.model import BaseDescription
10
+ from polars_data_profiling.model.alerts import Alert
11
+ from polars_data_profiling.model.description import BaseAnalysis, TimeIndexAnalysis
12
+ from polars_data_profiling.model.summary_algorithms import ValueCounts
13
+ from polars_data_profiling.profile_report import ProfileReport
14
+
15
+
16
+ def _should_wrap(v1: Any, v2: Any) -> bool:
17
+ # Value counts are leaves: they are paired, not merged key by key
18
+ if isinstance(v1, ValueCounts) or isinstance(v2, ValueCounts):
19
+ return True
20
+ if isinstance(v1, (list, dict)):
21
+ return False
22
+
23
+ if isinstance(v1, pl.DataFrame) and isinstance(v2, pl.DataFrame):
24
+ return v1.equals(v2)
25
+ if isinstance(v1, pl.Series) and isinstance(v2, pl.Series):
26
+ return v1.equals(v2)
27
+
28
+ try:
29
+ return v1 == v2
30
+ except ValueError:
31
+ return False
32
+
33
+
34
+ def _update_merge_dict(d1: Any, d2: Any) -> dict:
35
+ # Unwrap d1 and d2 in new dictionary to keep non-shared keys with **d1, **d2
36
+ # Next unwrap a dict that treats shared keys
37
+ # If two keys have an equal value, we take that value as new value
38
+ # If the values are not equal, we recursively merge them
39
+
40
+ return {
41
+ **d1,
42
+ **d2,
43
+ **{
44
+ k: [d1[k], d2[k]]
45
+ if _should_wrap(d1[k], d2[k])
46
+ else _update_merge_mixed(d1[k], d2[k])
47
+ for k in {*d1} & {*d2}
48
+ },
49
+ }
50
+
51
+
52
+ def _update_merge_seq(d1: Any, d2: Any) -> list | tuple:
53
+ # This case happens when values are merged
54
+ # It bundle values in a list, making sure
55
+ # to flatten them if they are already lists
56
+
57
+ if isinstance(d1, list) and isinstance(d2, list):
58
+ # This is the tuple for alerts
59
+ return d1, d2
60
+ elif isinstance(d1, tuple) and isinstance(d2, list):
61
+ return (*d1, d2)
62
+ else:
63
+ return [
64
+ *(d1 if isinstance(d1, list) else [d1]),
65
+ *(d2 if isinstance(d2, list) else [d2]),
66
+ ]
67
+
68
+
69
+ def _update_merge_mixed(d1: Any, d2: Any) -> dict | list | tuple:
70
+ if (
71
+ isinstance(d1, dict)
72
+ and isinstance(d2, dict)
73
+ and not isinstance(d1, ValueCounts)
74
+ and not isinstance(d2, ValueCounts)
75
+ ):
76
+ return _update_merge_dict(d1, d2)
77
+ else:
78
+ return _update_merge_seq(d1, d2)
79
+
80
+
81
+ def _update_merge(d1: dict | None, d2: dict) -> dict:
82
+ # For convenience in the loop, allow d1 to be empty initially
83
+ if d1 is None:
84
+ return d2
85
+
86
+ if not isinstance(d1, dict) or not isinstance(d2, dict):
87
+ raise TypeError(
88
+ "Both arguments need to be of type dictionary (ProfileReport.description_set)"
89
+ )
90
+
91
+ return _update_merge_dict(d1, d2)
92
+
93
+
94
+ def _placeholders(reports: list[BaseDescription]) -> None:
95
+ """Generates placeholders in the dataset descriptions where needed"""
96
+
97
+ keys = {v for r in reports for v in r.scatter}
98
+ type_keys = {v for r in reports for v in r.table["types"]}
99
+ for report in reports:
100
+ # Interactions
101
+ for k1 in keys:
102
+ for k2 in keys:
103
+ if k1 not in report.scatter:
104
+ report.scatter[k1] = {}
105
+ if k2 not in report.scatter[k1]:
106
+ report.scatter[k1][k2] = ""
107
+
108
+ # Types
109
+ for type_key in type_keys:
110
+ if type_key not in report.table["types"]:
111
+ report.table["types"][type_key] = 0
112
+
113
+
114
+ def _update_titles(reports: list[ProfileReport]) -> None:
115
+ """Redefine title of reports with the default one."""
116
+ for idx, report in enumerate(reports):
117
+ if report.config.title == Settings().title:
118
+ report.config.title = f"Dataset {chr(65 + idx)}"
119
+
120
+
121
+ def _compare_title(titles: list[str]) -> str:
122
+ if all(titles[0] == title for title in titles[1:]):
123
+ return titles[0]
124
+ else:
125
+ title = ", ".join(titles[:-1])
126
+ return f"<em>Comparing</em> {title} <em>and</em> {titles[-1]}"
127
+
128
+
129
+ def _compare_profile_report_preprocess(
130
+ reports: list[ProfileReport],
131
+ config: Settings | None = None,
132
+ ) -> tuple[list[str], list[BaseDescription]]:
133
+ # Use titles as labels
134
+ labels = [report.config.title for report in reports]
135
+
136
+ # Use color per report if not custom set
137
+ if config is None:
138
+ if len(reports[0].config.html.style.primary_colors) > 1:
139
+ for idx, report in enumerate(reports):
140
+ report.config.html.style.primary_colors = [
141
+ report.config.html.style.primary_colors[idx]
142
+ ]
143
+ else:
144
+ if len(config.html.style.primary_colors) > 1:
145
+ for report in reports:
146
+ report.config.html.style.primary_colors = (
147
+ config.html.style.primary_colors
148
+ )
149
+
150
+ # Obtain description sets
151
+ descriptions = [report.get_description() for report in reports]
152
+ for label, description in zip(labels, descriptions):
153
+ description.analysis.title = label
154
+
155
+ return labels, descriptions
156
+
157
+
158
+ def _compare_dataset_description_preprocess(
159
+ reports: list[BaseDescription],
160
+ ) -> tuple[list[str], list[BaseDescription]]:
161
+ labels = [report.analysis.title for report in reports]
162
+ return labels, reports
163
+
164
+
165
+ def validate_reports(
166
+ reports: list[ProfileReport] | list[BaseDescription], configs: list[dict]
167
+ ) -> None:
168
+ """Validate if the reports are comparable.
169
+
170
+ Args:
171
+ reports: two reports to compare
172
+ input may either be a ProfileReport, or the summary obtained from report.get_description()
173
+ """
174
+ if len(reports) < 2:
175
+ raise ValueError("At least two reports are required for this comparison")
176
+
177
+ if len(reports) > 2:
178
+ warnings.warn(
179
+ "Comparison of more than two reports is not supported. "
180
+ "Reports may be produced, but may yield unexpected formatting.",
181
+ stacklevel=2,
182
+ )
183
+
184
+ report_types = [c.vars.timeseries.active for c in configs] # type: ignore
185
+ if all(report_types) != any(report_types):
186
+ raise ValueError(
187
+ "Comparison between timeseries and tabular reports is not supported."
188
+ )
189
+
190
+ if isinstance(reports[0], ProfileReport):
191
+ is_df_available = [r.df is not None for r in reports] # type: ignore
192
+ if not all(is_df_available):
193
+ raise ValueError("Reports where not initialized with a DataFrame.")
194
+
195
+ if isinstance(reports[0], ProfileReport):
196
+ features = [set(r.df.collect_schema().names()) for r in reports] # type: ignore
197
+ else:
198
+ features = [set(r.variables.keys()) for r in reports] # type: ignore
199
+
200
+ if not all(features[0] == x for x in features):
201
+ warnings.warn(
202
+ "The datasets being profiled have a different set of columns. "
203
+ "Only the left side profile will be calculated.",
204
+ stacklevel=2,
205
+ )
206
+
207
+
208
+ def _apply_config(description: BaseDescription, config: Settings) -> BaseDescription:
209
+ """Apply the configuration for visualilzation purposes.
210
+
211
+ This handles the cases in which the report description
212
+ was computed prior to comparison with a different config
213
+
214
+ Args:
215
+ description: report summary
216
+ config: the settings object for the ProfileReport
217
+
218
+ Returns:
219
+ the updated description
220
+ """
221
+ description.missing = {
222
+ k: v for k, v in description.missing.items() if config.missing_diagrams[k]
223
+ }
224
+
225
+ description.correlations = {
226
+ k: v
227
+ for k, v in description.correlations.items()
228
+ if config.correlations.get(k, Correlation(calculate=False).calculate)
229
+ }
230
+
231
+ samples = [config.samples.head, config.samples.tail, config.samples.random]
232
+ samples = [s > 0 for s in samples]
233
+ description.sample = description.sample if any(samples) else []
234
+ description.duplicates = (
235
+ description.duplicates if config.duplicates.head > 0 else None
236
+ )
237
+ description.scatter = description.scatter if config.interactions.continuous else {}
238
+
239
+ return description
240
+
241
+
242
+ def _is_alert_present(alert: Alert, alert_list: list) -> bool:
243
+ return any(
244
+ a.column_name == alert.column_name and a.alert_type == alert.alert_type
245
+ for a in alert_list
246
+ )
247
+
248
+
249
+ def _create_placehoder_alerts(report_alerts: tuple) -> tuple:
250
+ from copy import copy
251
+
252
+ fixed: list = [[] for _ in report_alerts]
253
+ for idx, alerts in enumerate(report_alerts):
254
+ for alert in alerts:
255
+ fixed[idx].append(alert)
256
+ for i, fix in enumerate(fixed):
257
+ if i == idx:
258
+ continue
259
+ if not _is_alert_present(alert, report_alerts[i]):
260
+ empty_alert = copy(alert)
261
+ empty_alert._is_empty = True
262
+ fix.append(empty_alert)
263
+ return tuple(fixed)
264
+
265
+
266
+ def compare(
267
+ reports: list[ProfileReport] | list[BaseDescription],
268
+ config: Settings | None = None,
269
+ compute: bool = False,
270
+ ) -> ProfileReport:
271
+ """
272
+ Compare Profile reports
273
+
274
+ Args:
275
+ reports: two reports to compare
276
+ input may either be a ProfileReport, or the summary obtained from report.get_description()
277
+ config: the settings object for the merged ProfileReport
278
+ compute: recompute the profile report using config or the left report config
279
+ recommended in cases where the reports were created using different settings
280
+
281
+ """
282
+ if len(reports) == 0:
283
+ raise ValueError("No reports available for comparison.")
284
+
285
+ report_dtypes = [type(r) for r in reports]
286
+ if len(set(report_dtypes)) > 1:
287
+ raise TypeError(
288
+ "The input must have the same data type for all reports. Comparing ProfileReport objects to summaries obtained from the get_description() method is not supported."
289
+ )
290
+
291
+ if isinstance(reports[0], ProfileReport):
292
+ all_configs = [r.config for r in reports] # type: ignore
293
+ else:
294
+ configs_str = [
295
+ json.loads(r.package["polars_data_profiling_config"])
296
+ for r in reports # type: ignore
297
+ ]
298
+ all_configs = []
299
+ for c_str in configs_str:
300
+ c_setting = Settings()
301
+ c_setting = c_setting.update(c_str)
302
+ all_configs.append(c_setting)
303
+
304
+ validate_reports(reports=reports, configs=all_configs)
305
+
306
+ if isinstance(reports[0], ProfileReport):
307
+ base_features = reports[0].df.collect_schema().names() # type: ignore
308
+ for report in reports[1:]:
309
+ columns = set(report.df.collect_schema().names()) # type: ignore
310
+ cols_2_compare = [col for col in base_features if col in columns]
311
+ report.df = report.df.select(cols_2_compare) # type: ignore
312
+ reports = [r for r in reports if len(r.df.collect_schema()) > 0] # type: ignore
313
+ if len(reports) == 1:
314
+ return reports[0] # type: ignore
315
+ else:
316
+ base_features = list(reports[0].variables.keys())
317
+ non_empty_reports = 0
318
+ for report in reports[1:]:
319
+ cols_2_compare = [
320
+ col
321
+ for col in base_features
322
+ if col in list(report.variables.keys()) # type: ignore
323
+ ]
324
+ if len(cols_2_compare) > 0:
325
+ non_empty_reports += 1
326
+ if non_empty_reports == 0:
327
+ profile = ProfileReport(None, config=all_configs[0])
328
+ profile._description_set = reports[0]
329
+ return profile
330
+
331
+ _config = None
332
+ if config is None:
333
+ _config = all_configs[0].model_copy(deep=True)
334
+ else:
335
+ _config = config.model_copy(deep=True)
336
+ if isinstance(reports[0], ProfileReport):
337
+ for report in reports:
338
+ tsmode = report.config.vars.timeseries.active # type: ignore
339
+ title = report.config.title # type: ignore
340
+ report.config = config.model_copy(deep=True) # type: ignore
341
+ report.config.title = title # type: ignore
342
+ report.config.vars.timeseries.active = tsmode # type: ignore
343
+ if compute:
344
+ report._description_set = None # type: ignore
345
+
346
+ if all(isinstance(report, ProfileReport) for report in reports):
347
+ # Type ignore is needed as mypy does not pick up on the type narrowing
348
+ # Consider using TypeGuard (3.10): https://docs.python.org/3/library/typing.html#typing.TypeGuard
349
+ _update_titles(reports) # type: ignore
350
+ labels, descriptions = _compare_profile_report_preprocess(reports, _config) # type: ignore
351
+ elif all(isinstance(report, BaseDescription) for report in reports):
352
+ labels, descriptions = _compare_dataset_description_preprocess(reports) # type: ignore
353
+ else:
354
+ raise TypeError(
355
+ "The input must have the same data type for all reports. Comparing ProfileReport objects to summaries obtained from the get_description() method is not supported."
356
+ )
357
+
358
+ _config.html.style._labels = labels
359
+
360
+ _placeholders(descriptions)
361
+
362
+ descriptions_dict = [asdict(_apply_config(d, _config)) for d in descriptions]
363
+
364
+ res: dict = _update_merge(None, descriptions_dict[0])
365
+ for r in descriptions_dict[1:]:
366
+ res = _update_merge(res, r)
367
+
368
+ res["analysis"]["title"] = _compare_title(res["analysis"]["title"])
369
+ res["alerts"] = _create_placehoder_alerts(res["alerts"])
370
+ if not any(res["time_index_analysis"]):
371
+ res["time_index_analysis"] = None
372
+ profile = ProfileReport(None, config=_config)
373
+ profile._description_set = _description_from_dict(res)
374
+ return profile
375
+
376
+
377
+ def _description_from_dict(data: dict) -> BaseDescription:
378
+ """Rebuild a (merged) description from its dict form."""
379
+ data = dict(data)
380
+ analysis = data["analysis"]
381
+ data["analysis"] = BaseAnalysis(
382
+ analysis["title"], analysis["date_start"], analysis["date_end"]
383
+ )
384
+ if data.get("time_index_analysis"):
385
+ data["time_index_analysis"] = TimeIndexAnalysis(**data["time_index_analysis"])
386
+ return BaseDescription(**data)