Spaces:
Running
Running
File size: 14,580 Bytes
226ac39 227cb22 226ac39 227cb22 226ac39 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 |
"""
Data Wrangling Tools
Tools for merging, concatenating, and manipulating multiple datasets.
"""
import polars as pl
import numpy as np
from typing import Dict, Any, List, Optional, Literal
from pathlib import Path
import sys
import os
# Add parent directory to path for imports
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from ..utils.polars_helpers import (
load_dataframe,
save_dataframe,
)
from ..utils.validation import (
validate_file_exists,
validate_file_format,
validate_dataframe,
)
def merge_datasets(
left_path: str,
right_path: str,
output_path: str,
how: Literal["inner", "left", "right", "outer", "cross"] = "inner",
on: Optional[str] = None,
left_on: Optional[str] = None,
right_on: Optional[str] = None,
suffix: str = "_right"
) -> Dict[str, Any]:
"""
Merge two datasets using various join strategies (SQL-like join operations).
This function performs database-style joins on two datasets, similar to SQL JOIN operations.
Supports inner, left, right, outer, and cross joins.
Args:
left_path: Path to left dataset (CSV or Parquet)
right_path: Path to right dataset (CSV or Parquet)
output_path: Path to save merged dataset
how: Join type - "inner", "left", "right", "outer", or "cross"
- "inner": Only rows with matching keys in both datasets
- "left": All rows from left, matching rows from right (nulls if no match)
- "right": All rows from right, matching rows from left (nulls if no match)
- "outer": All rows from both (nulls where no match)
- "cross": Cartesian product (all combinations)
on: Column name to join on (if same in both datasets)
left_on: Column name in left dataset (if different from right)
right_on: Column name in right dataset (if different from left)
suffix: Suffix to add to duplicate column names from right dataset (default: "_right")
Returns:
Dictionary with merge report including:
- success: bool
- output_path: str
- left_rows: int
- right_rows: int
- result_rows: int
- merge_type: str
- join_columns: dict
- duplicate_columns: list (columns that got suffixed)
Examples:
>>> # Simple join on same column name
>>> merge_datasets(
... "customers.csv",
... "orders.csv",
... "merged.csv",
... how="left",
... on="customer_id"
... )
>>> # Join on different column names
>>> merge_datasets(
... "products.csv",
... "sales.csv",
... "product_sales.csv",
... how="inner",
... left_on="product_id",
... right_on="prod_id"
... )
"""
try:
# Validation
validate_file_exists(left_path)
validate_file_exists(right_path)
validate_file_format(left_path)
validate_file_format(right_path)
# Load datasets
left_df = load_dataframe(left_path)
right_df = load_dataframe(right_path)
validate_dataframe(left_df)
validate_dataframe(right_df)
left_rows = len(left_df)
right_rows = len(right_df)
# Determine join columns
if on:
# Same column name in both datasets
join_left_on = on
join_right_on = on
# Validate column exists
if on not in left_df.columns:
return {
"success": False,
"error": f"Column '{on}' not found in left dataset. Available: {left_df.columns}"
}
if on not in right_df.columns:
return {
"success": False,
"error": f"Column '{on}' not found in right dataset. Available: {right_df.columns}"
}
elif left_on and right_on:
# Different column names
join_left_on = left_on
join_right_on = right_on
# Validate columns exist
if left_on not in left_df.columns:
return {
"success": False,
"error": f"Column '{left_on}' not found in left dataset. Available: {left_df.columns}"
}
if right_on not in right_df.columns:
return {
"success": False,
"error": f"Column '{right_on}' not found in right dataset. Available: {right_df.columns}"
}
else:
return {
"success": False,
"error": "Must specify either 'on' (same column name) or both 'left_on' and 'right_on' (different names)"
}
# Check for duplicate column names (excluding join columns)
left_cols = set(left_df.columns)
right_cols = set(right_df.columns)
duplicate_cols = list((left_cols & right_cols) - {join_left_on, join_right_on})
# Perform merge
merged_df = left_df.join(
right_df,
left_on=join_left_on,
right_on=join_right_on,
how=how,
suffix=suffix
)
result_rows = len(merged_df)
# Save result
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
save_dataframe(merged_df, output_path)
# Build report
report = {
"success": True,
"output_path": output_path,
"left_file": Path(left_path).name,
"right_file": Path(right_path).name,
"left_rows": left_rows,
"right_rows": right_rows,
"result_rows": result_rows,
"result_columns": len(merged_df.columns),
"merge_type": how,
"join_columns": {
"left": join_left_on,
"right": join_right_on
},
"duplicate_columns": duplicate_cols,
"rows_added": result_rows - left_rows if how in ["left", "inner"] else None,
"message": f"Successfully merged {left_rows:,} rows with {right_rows:,} rows using {how} join → {result_rows:,} rows"
}
# Add warnings
if how == "inner" and result_rows < min(left_rows, right_rows):
report["warning"] = f"Inner join reduced data: only {result_rows:,} of {min(left_rows, right_rows):,} rows had matches"
elif how == "outer" and result_rows > left_rows + right_rows:
report["warning"] = "Outer join created duplicate rows - check for many-to-many relationships"
if duplicate_cols:
report["note"] = f"{len(duplicate_cols)} column(s) were suffixed with '{suffix}': {', '.join(duplicate_cols)}"
return report
except Exception as e:
return {
"success": False,
"error": str(e),
"error_type": type(e).__name__
}
def concat_datasets(
file_paths: List[str],
output_path: str,
axis: Literal["vertical", "horizontal"] = "vertical",
ignore_index: bool = True
) -> Dict[str, Any]:
"""
Concatenate multiple datasets vertically (stack rows) or horizontally (add columns).
Args:
file_paths: List of file paths to concatenate (CSV or Parquet)
output_path: Path to save concatenated dataset
axis: "vertical" to stack rows (union), "horizontal" to add columns side-by-side
ignore_index: If True, reset index after concatenation (default: True)
Returns:
Dictionary with concatenation report including:
- success: bool
- output_path: str
- input_files: int
- result_rows: int
- result_cols: int
- axis: str
Examples:
>>> # Stack multiple CSV files (union)
>>> concat_datasets(
... ["jan_sales.csv", "feb_sales.csv", "mar_sales.csv"],
... "q1_sales.csv",
... axis="vertical"
... )
>>> # Combine datasets side-by-side (add columns)
>>> concat_datasets(
... ["features.csv", "labels.csv"],
... "full_dataset.csv",
... axis="horizontal"
... )
"""
try:
# Validation
if not file_paths or len(file_paths) < 2:
return {
"success": False,
"error": "Must provide at least 2 files to concatenate"
}
for fp in file_paths:
validate_file_exists(fp)
validate_file_format(fp)
# Load all datasets
dfs = []
file_info = []
for fp in file_paths:
df = load_dataframe(fp)
validate_dataframe(df)
dfs.append(df)
file_info.append({
"file": Path(fp).name,
"rows": len(df),
"columns": len(df.columns)
})
# Perform concatenation
if axis == "vertical":
# Stack rows (union) - requires same columns
result = pl.concat(dfs, how="vertical")
else: # horizontal
# Add columns side-by-side - requires same number of rows
row_counts = [len(df) for df in dfs]
if len(set(row_counts)) > 1:
return {
"success": False,
"error": f"Horizontal concatenation requires same number of rows. Got: {row_counts}",
"file_info": file_info
}
result = pl.concat(dfs, how="horizontal")
# Save result
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
save_dataframe(result, output_path)
return {
"success": True,
"output_path": output_path,
"input_files": len(file_paths),
"file_info": file_info,
"result_rows": len(result),
"result_cols": len(result.columns),
"axis": axis,
"message": f"Successfully concatenated {len(file_paths)} files ({axis}) → {len(result):,} rows × {len(result.columns)} columns"
}
except Exception as e:
return {
"success": False,
"error": str(e),
"error_type": type(e).__name__
}
def reshape_dataset(
file_path: str,
output_path: str,
operation: Literal["pivot", "melt", "transpose"],
**kwargs
) -> Dict[str, Any]:
"""
Reshape dataset using pivot, melt, or transpose operations.
Args:
file_path: Path to CSV or Parquet file
output_path: Path to save reshaped dataset
operation: "pivot" (wide format), "melt" (long format), or "transpose"
**kwargs: Operation-specific parameters
For pivot: index, columns, values, aggregate_function
For melt: id_vars, value_vars, var_name, value_name
Returns:
Dictionary with reshape report
Examples:
>>> # Pivot: wide format
>>> reshape_dataset(
... "sales_long.csv",
... "sales_wide.csv",
... operation="pivot",
... index="date",
... columns="product",
... values="sales"
... )
>>> # Melt: long format
>>> reshape_dataset(
... "sales_wide.csv",
... "sales_long.csv",
... operation="melt",
... id_vars=["date"],
... value_vars=["product_a", "product_b"],
... var_name="product",
... value_name="sales"
... )
"""
try:
# Validation
validate_file_exists(file_path)
validate_file_format(file_path)
# Load data
df = load_dataframe(file_path)
validate_dataframe(df)
original_shape = (len(df), len(df.columns))
# Perform operation
if operation == "pivot":
# Pivot to wide format
index = kwargs.get("index")
columns = kwargs.get("columns")
values = kwargs.get("values")
if not all([index, columns, values]):
return {
"success": False,
"error": "Pivot requires: index, columns, values parameters"
}
result = df.pivot(
index=index,
columns=columns,
values=values
)
elif operation == "melt":
# Melt to long format
id_vars = kwargs.get("id_vars")
value_vars = kwargs.get("value_vars")
var_name = kwargs.get("var_name", "variable")
value_name = kwargs.get("value_name", "value")
if not id_vars:
return {
"success": False,
"error": "Melt requires: id_vars parameter"
}
result = df.melt(
id_vars=id_vars,
value_vars=value_vars,
variable_name=var_name,
value_name=value_name
)
elif operation == "transpose":
# Transpose rows and columns
result = df.transpose()
else:
return {
"success": False,
"error": f"Unknown operation: {operation}. Use 'pivot', 'melt', or 'transpose'"
}
# Save result
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
save_dataframe(result, output_path)
return {
"success": True,
"output_path": output_path,
"operation": operation,
"original_shape": {
"rows": original_shape[0],
"columns": original_shape[1]
},
"result_shape": {
"rows": len(result),
"columns": len(result.columns)
},
"message": f"Successfully {operation}ed dataset: {original_shape[0]}×{original_shape[1]} → {len(result)}×{len(result.columns)}"
}
except Exception as e:
return {
"success": False,
"error": str(e),
"error_type": type(e).__name__
}
|