Adisri99 commited on
Commit
c77b4fe
·
verified ·
1 Parent(s): 922da09

Update backend/app/services/profiling_service.py

Browse files
backend/app/services/profiling_service.py CHANGED
@@ -1,14 +1,33 @@
 
 
1
  import pandas as pd
2
 
 
 
 
 
 
 
 
 
 
3
  def profile_dataframe(df: pd.DataFrame) -> dict:
4
  columns = []
 
5
  for col in df.columns:
6
- s = df[col]
7
- dtype = "numeric" if pd.api.types.is_numeric_dtype(s) else "categorical"
8
- columns.append({"name": col, "inferred_type": dtype, "missing_pct": round(float(s.isna().mean()) * 100, 2), "cardinality": int(s.nunique(dropna=True))})
9
- recommended = ["kmeans", "birch"]
10
- if any(c["inferred_type"] == "categorical" for c in columns):
11
- recommended.append("agglomerative")
12
- if len(df) >= 50:
13
- recommended.append("dbscan")
14
- return {"columns": columns, "recommended_algorithms": recommended}
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
  import pandas as pd
4
 
5
+
6
+ def _infer_type(series: pd.Series) -> str:
7
+ if pd.api.types.is_bool_dtype(series):
8
+ return "boolean"
9
+ if pd.api.types.is_numeric_dtype(series):
10
+ return "numeric"
11
+ return "categorical"
12
+
13
+
14
  def profile_dataframe(df: pd.DataFrame) -> dict:
15
  columns = []
16
+
17
  for col in df.columns:
18
+ series = df[col]
19
+ columns.append(
20
+ {
21
+ "name": col,
22
+ "inferred_type": _infer_type(series),
23
+ "missing_pct": round(float(series.isna().mean() * 100), 4),
24
+ "cardinality": int(series.nunique(dropna=True)),
25
+ }
26
+ )
27
+
28
+ recommended_algorithms = ["kmeans", "birch", "agglomerative", "dbscan"]
29
+
30
+ return {
31
+ "columns": columns,
32
+ "recommended_algorithms": recommended_algorithms,
33
+ }