Files
fn_registry/functions/datascience/pivot.md
T
egutierrez 53200cbc0d feat: funciones Go — core (cron, join_by_key, validate_struct), datascience (pivot, diff_entities), infra (http, cache, cron_ticker)
Nuevas funciones Go con tests en tres dominios:
- core: parse_cron_expr, next_cron_time, join_by_key, validate_struct_fields + tipo CronSchedule
- datascience: pivot (tabla dinámica), diff_entities (comparación de entidades)
- infra: http_get_json, http_post_json, http_download_file, cache_to_sqlite, cron_ticker

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 17:11:12 +02:00

44 lines
1.4 KiB
Markdown

---
name: pivot
kind: function
lang: go
domain: datascience
version: "1.0.0"
purity: pure
signature: "func Pivot(rows []map[string]any, index, columns, values, agg string) []map[string]any"
description: "Pivot table sin dependencias. Agrupa por index, expande valores unicos de columns como nuevas columnas y agrega values con la funcion indicada (sum, count, mean, min, max, first, last). Valores faltantes se rellenan con 0."
tags: [datascience, tabular, pivot, transform, aggregation, go]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: ""
imports: []
tested: true
tests:
- "Pivot basico con sum"
- "Pivot con count y mean"
- "Valores faltantes rellenados con 0"
- "Una sola fila"
- "Multiples valores por celda requieren agregacion"
test_file_path: "functions/datascience/pivot_test.go"
file_path: "functions/datascience/pivot.go"
---
## Ejemplo
```go
rows := []map[string]any{
{"region": "US", "product": "A", "sales": 10},
{"region": "US", "product": "B", "sales": 20},
{"region": "EU", "product": "A", "sales": 15},
}
result := Pivot(rows, "region", "product", "sales", "sum")
// [{"region": "US", "A": 10.0, "B": 20.0}, {"region": "EU", "A": 15.0, "B": 0}]
```
## Notas
Funcion pura sin dependencias externas. Usa map[string]any para trabajar con datos JSON/SQL deserializados.
Las agregaciones numericas (sum, mean, min, max) convierten valores a float64 via type assertion.