Spaces:
Sleeping
Sleeping
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 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
}
|