Documentation
¶
Overview ¶
Package golars provides a high-performance DataFrame library for Go, inspired by Python's Polars. It offers columnar data storage, a composable expression DSL, lazy evaluation with query optimization, SQL querying, and comprehensive I/O support for CSV, JSON, Parquet, and Excel formats.
Import ¶
import "github.com/msjurset/golars"
All public types and functions are re-exported at the top level. Internal sub-packages use Go's internal/ convention and cannot be imported directly.
Key Design Principles ¶
Immutability: DataFrames and Series are immutable after construction. Every transformation returns a new value, making concurrent reads safe without synchronization. The underlying array storage is shared where possible to minimize copies.
Expression DSL: Rather than embedding logic in method chains on DataFrames, golars uses a composable expression system. Expressions like Col, Lit, and When build up computation trees that can be evaluated against a DataFrame context, enabling reuse, composition, and optimization.
Lazy Evaluation: The Lazy function wraps a DataFrame in a LazyFrame that records operations as a logical plan. The plan is optimized (predicate pushdown, projection pruning) and only executed when [LazyFrame.Collect] is called.
Constructing DataFrames ¶
Create typed Series and combine them into a DataFrame:
df, err := golars.NewDataFrame(
golars.NewInt64Series("id", []int64{1, 2, 3}),
golars.NewStringSeries("name", []string{"Alice", "Bob", "Charlie"}),
golars.NewFloat64Series("score", []float64{88.5, 92.3, 76.1}),
)
Series with nullable values use explicit validity bitmaps:
s := golars.NewInt64SeriesWithValidity("val", []int64{10, 0, 30}, []bool{true, false, true})
Selecting, Filtering, and Sorting ¶
selected, err := df.Select("name", "score")
dropped, err := df.Drop("id")
sorted, err := df.Sort("score", true) // descending
Filtering uses the expression system to produce a boolean mask:
ctx := &golars.ExprContext{DF: df}
mask, err := golars.Col("score").Gt(golars.Lit(80.0)).Evaluate(ctx)
filtered, err := df.Filter(mask)
Expressions ¶
Expressions are composable building blocks for column-level computation:
// Arithmetic
golars.Col("price").Mul(golars.Lit(1.1)).Alias("with_tax")
// Comparison and logical
golars.Col("age").Gte(golars.Lit(18)).And(golars.Col("active").Eq(golars.Lit(true)))
// Aggregation
golars.Col("score").Mean()
golars.Col("revenue").Sum().Over("region") // window function
// Conditional
golars.When(golars.Col("score").Gt(golars.Lit(90))).
Then(golars.Lit("A")).
Otherwise(golars.Lit("B"))
GroupBy and Aggregation ¶
grouped, err := df.GroupBy("department")
result, err := grouped.Agg(map[string]golars.AggFunc{
"salary": golars.AggMean,
"id": golars.AggCount,
})
Available aggregation functions: AggSum, AggMean, AggMin, AggMax, AggCount, AggFirst, AggLast.
Joins ¶
joined, err := left.Join(right, []string{"id"}, golars.InnerJoin)
Supported join types: InnerJoin, LeftJoin, RightJoin, FullJoin, SemiJoin, AntiJoin, CrossJoin.
Reshaping ¶
pivoted, err := df.Pivot("name", "subject", "score")
melted, err := df.Unpivot([]string{"id"}, []string{"math", "english"})
transposed, err := df.Transpose()
Lazy Evaluation ¶
Build a query plan and execute it in one optimized pass:
result, err := golars.Lazy(df).
Filter(golars.Col("age").Gt(golars.Lit(25))).
Sort("score", true).
Head(10).
Collect()
Inspect the logical plan with Explain or ExplainOptimized:
plan := golars.Lazy(df).Filter(expr).Sort("col", false).Explain()
I/O ¶
Read and write CSV, JSON, NDJSON, Parquet, and Excel files. All readers accept file paths; most also offer io.Reader variants for streaming:
df, err := golars.ReadCSV("data.csv", golars.WithSeparator('\t'))
df, err := golars.ReadCSVFromReader(r, golars.WithHasHeader(false))
err = golars.WriteCSV(df, w)
df, err = golars.ReadParquet("data.parquet")
err = golars.WriteParquetFile(df, "out.parquet")
df, err = golars.ReadJSON("data.json")
df, err = golars.ReadExcel("data.xlsx")
CSV options: WithSeparator, WithNullValues, WithCSVColumns, WithInferSchemaLength, WithSkipRows, WithNRows, WithHasHeader.
SQL ¶
Register DataFrames and query them with standard SQL syntax:
ctx := golars.NewSQLContext()
ctx.Register("users", df)
result, err := ctx.Execute("SELECT name, AVG(score) FROM users GROUP BY name")
Series Operations ¶
Series provide rich per-column operations:
s.Sum() // aggregation (returns float64, bool) s.Mean() s.RollingMean(3) // rolling window (returns *Series) s.CumSum() // cumulative operations s.Shift(1) // shift values s.Str().ToUpper() // string namespace methods s.Cast(golars.Float64)
Row-Level Operations ¶
row := df.Row(0) // map[string]any
rows := df.ToMaps() // []map[string]any
s := df.MapRows("sum", fn) // apply function per row
Example (CumulativeRolling) ¶
package main
import (
"fmt"
"github.com/msjurset/golars"
)
func main() {
df, _ := golars.NewDataFrame(
golars.NewInt64Series("x", []int64{1, 2, 3, 4, 5}),
)
ctx := &golars.ExprContext{DF: df}
// Cumulative sum
cumSum, _ := golars.Col("x").Cum().Sum().Evaluate(ctx)
for i := 0; i < cumSum.Len(); i++ {
v, _ := cumSum.GetInt64(i)
if i > 0 {
fmt.Print(" ")
}
fmt.Printf("%d", v)
}
fmt.Println()
// Rolling mean (window=3)
rollMean, _ := golars.Col("x").Cast(golars.Float64).Rolling(3).Mean().Evaluate(ctx)
for i := 0; i < rollMean.Len(); i++ {
if i > 0 {
fmt.Print(" ")
}
if rollMean.IsNull(i) {
fmt.Print("null")
} else {
v, _ := rollMean.GetFloat64(i)
fmt.Printf("%.1f", v)
}
}
fmt.Println()
}
Output: 1 3 6 10 15 null null 2.0 3.0 4.0
Example (FirstLastExpr) ¶
package main
import (
"fmt"
"github.com/msjurset/golars"
)
func main() {
df, _ := golars.NewDataFrame(
golars.NewStringSeries("name", []string{"Alice", "Bob", "Charlie"}),
)
ctx := &golars.ExprContext{DF: df}
first, _ := golars.Col("name").First().Evaluate(ctx)
last, _ := golars.Col("name").Last().Evaluate(ctx)
v1, _ := first.GetString(0)
v2, _ := last.GetString(0)
fmt.Printf("first=%s last=%s\n", v1, v2)
}
Output: first=Alice last=Charlie
Example (MathExpressions) ¶
package main
import (
"fmt"
"github.com/msjurset/golars"
)
func main() {
df, _ := golars.NewDataFrame(
golars.NewFloat64Series("x", []float64{-2.5, 4.0, 9.0, 1.0}),
)
ctx := &golars.ExprContext{DF: df}
// Abs
abs, _ := golars.Col("x").Abs().Evaluate(ctx)
for i := 0; i < abs.Len(); i++ {
v, _ := abs.GetFloat64(i)
if i > 0 {
fmt.Print(" ")
}
fmt.Printf("%.1f", v)
}
fmt.Println()
// Round
df2, _ := golars.NewDataFrame(golars.NewFloat64Series("v", []float64{3.14159, 2.71828}))
ctx2 := &golars.ExprContext{DF: df2}
rounded, _ := golars.Col("v").Round(2).Evaluate(ctx2)
for i := 0; i < rounded.Len(); i++ {
v, _ := rounded.GetFloat64(i)
if i > 0 {
fmt.Print(" ")
}
fmt.Printf("%.2f", v)
}
fmt.Println()
}
Output: 2.5 4.0 9.0 1.0 3.14 2.72
Example (ShiftDiffPctChange) ¶
package main
import (
"fmt"
"github.com/msjurset/golars"
)
func main() {
df, _ := golars.NewDataFrame(
golars.NewFloat64Series("price", []float64{100, 105, 103, 110}),
)
ctx := &golars.ExprContext{DF: df}
// Percentage change
pct, _ := golars.Col("price").PctChange(1).Evaluate(ctx)
for i := 0; i < pct.Len(); i++ {
if pct.IsNull(i) {
fmt.Print("null ")
} else {
v, _ := pct.GetFloat64(i)
fmt.Printf("%.4f ", v)
}
}
fmt.Println()
}
Output: null 0.0500 -0.0190 0.0680
Example (SortBy) ¶
package main
import (
"fmt"
"github.com/msjurset/golars"
)
func main() {
df, _ := golars.NewDataFrame(
golars.NewStringSeries("name", []string{"Alice", "Bob", "Charlie"}),
golars.NewInt64Series("score", []int64{90, 70, 80}),
)
ctx := &golars.ExprContext{DF: df}
// Sort names by score (ascending)
sorted, _ := golars.Col("name").SortBy(golars.Col("score"), false).Evaluate(ctx)
for i := 0; i < sorted.Len(); i++ {
v, _ := sorted.GetString(i)
fmt.Println(v)
}
}
Output: Bob Charlie Alice
Example (StrCapitalize) ¶
package main
import (
"fmt"
"github.com/msjurset/golars"
)
func main() {
df, _ := golars.NewDataFrame(
golars.NewStringSeries("word", []string{"hello world", "FOO BAR", "go lang"}),
)
ctx := &golars.ExprContext{DF: df}
capped, _ := golars.Col("word").Str().Capitalize().Evaluate(ctx)
for i := 0; i < capped.Len(); i++ {
v, _ := capped.GetString(i)
fmt.Println(v)
}
}
Output: Hello world Foo bar Go lang
Example (WindowOver) ¶
package main
import (
"fmt"
"github.com/msjurset/golars"
)
func main() {
df, _ := golars.NewDataFrame(
golars.NewStringSeries("team", []string{"A", "A", "B", "B"}),
golars.NewFloat64Series("score", []float64{10, 20, 30, 40}),
)
ctx := &golars.ExprContext{DF: df}
teamMean, _ := golars.Col("score").Mean().Over("team").Evaluate(ctx)
teamCol, _ := df.Column("team")
for i := 0; i < teamMean.Len(); i++ {
team, _ := teamCol.GetString(i)
mean, _ := teamMean.GetFloat64(i)
fmt.Printf("%s: %.1f\n", team, mean)
}
}
Output: A: 15.0 A: 15.0 B: 35.0 B: 35.0
Index ¶
- Constants
- Variables
- func WriteCSV(df *DataFrame, w io.Writer, opts ...csvio.WriteOption) error
- func WriteCSVFile(df *DataFrame, path string, opts ...csvio.WriteOption) error
- func WriteExcel(df *DataFrame, w io.Writer) error
- func WriteExcelFile(df *DataFrame, path string) error
- func WriteJSON(df *DataFrame, w io.Writer) error
- func WriteJSONFile(df *DataFrame, path string) error
- func WriteNDJSON(df *DataFrame, w io.Writer) error
- func WriteNDJSONFile(df *DataFrame, path string) error
- func WriteParquet(df *DataFrame, w io.Writer, opts ...WriteParquetOption) error
- func WriteParquetFile(df *DataFrame, path string, opts ...WriteParquetOption) error
- type AggFunc
- type CumNamespace
- type DataFrame
- func ConcatDataFrames(dfs ...*DataFrame) (*DataFrame, error)
- func ConcatDataFramesHorizontal(dfs ...*DataFrame) (*DataFrame, error)
- func DataFrameFromSchema(schema *Schema, height int) *DataFrame
- func GroupByAgg(g *GroupByResult, exprs ...Expr) (*DataFrame, error)
- func NewDataFrame(columns ...*Series) (*DataFrame, error)
- func ReadCSV(path string, opts ...ReadCSVOption) (*DataFrame, error)
- func ReadCSVFromReader(r io.Reader, opts ...ReadCSVOption) (*DataFrame, error)
- func ReadCSVWithContext(ctx context.Context, path string, opts ...ReadCSVOption) (*DataFrame, error)
- func ReadExcel(path string) (*DataFrame, error)
- func ReadExcelFromReader(r io.ReaderAt, size int64) (*DataFrame, error)
- func ReadJSON(path string) (*DataFrame, error)
- func ReadJSONFromReader(r io.Reader) (*DataFrame, error)
- func ReadNDJSON(path string) (*DataFrame, error)
- func ReadNDJSONFromReader(r io.Reader) (*DataFrame, error)
- func ReadParquet(path string) (*DataFrame, error)
- func ReadParquetFromReader(r io.ReadSeeker) (*DataFrame, error)
- func ReadSQL(db *sql.DB, query string, args ...any) (*DataFrame, error)
- func ReadSQLRows(rows *sql.Rows) (*DataFrame, error)
- type DataType
- type DtNamespace
- type Expr
- type ExprContext
- type Field
- type GroupByExpr
- type GroupByResult
- type JoinType
- type LazyFrame
- type LazyGroupBy
- type NameNamespace
- type ReadCSVOption
- type RollingNamespace
- type RowAccessor
- type SQLContext
- type Schema
- type Series
- func NewBooleanSeries(name string, data []bool) *Series
- func NewBooleanSeriesWithValidity(name string, data []bool, valid []bool) *Series
- func NewDateSeries(name string, data []int32) *Series
- func NewDateSeriesFromTime(name string, times []time.Time) *Series
- func NewDateTimeSeries(name string, data []int64) *Series
- func NewDateTimeSeriesFromTime(name string, times []time.Time) *Series
- func NewDurationSeries(name string, data []int64) *Series
- func NewFloat32Series(name string, data []float32) *Series
- func NewFloat64Series(name string, data []float64) *Series
- func NewFloat64SeriesWithValidity(name string, data []float64, valid []bool) *Series
- func NewInt8Series(name string, data []int8) *Series
- func NewInt16Series(name string, data []int16) *Series
- func NewInt32Series(name string, data []int32) *Series
- func NewInt64Series(name string, data []int64) *Series
- func NewInt64SeriesWithValidity(name string, data []int64, valid []bool) *Series
- func NewStringSeries(name string, data []string) *Series
- func NewStringSeriesWithValidity(name string, data []string, valid []bool) *Series
- func NewTimeSeries(name string, data []int64) *Series
- func NewUInt8Series(name string, data []uint8) *Series
- func NewUInt16Series(name string, data []uint16) *Series
- func NewUInt32Series(name string, data []uint32) *Series
- func NewUInt64Series(name string, data []uint64) *Series
- type SortDirection
- type StrNamespace
- type ThenBuilder
- type TimeUnit
- type WhenBuilder
- type WriteParquetOption
Examples ¶
- Package (CumulativeRolling)
- Package (FirstLastExpr)
- Package (MathExpressions)
- Package (ShiftDiffPctChange)
- Package (SortBy)
- Package (StrCapitalize)
- Package (WindowOver)
- ConcatDataFrames
- Lazy
- NewDataFrame
- NewDataFrame (Basic)
- ReadCSV
- SQLContext
- Series (Aggregation)
- Series (NullHandling)
- Series (Rolling)
- When
Constants ¶
const ( Null = dtype.Null Boolean = dtype.Boolean Int8 = dtype.Int8 Int16 = dtype.Int16 Int32 = dtype.Int32 Int64 = dtype.Int64 UInt8 = dtype.UInt8 UInt16 = dtype.UInt16 UInt32 = dtype.UInt32 UInt64 = dtype.UInt64 Float32 = dtype.Float32 Float64 = dtype.Float64 Decimal = dtype.Decimal String = dtype.String Binary = dtype.Binary Date = dtype.Date DateTime = dtype.DateTime Time = dtype.Time Duration = dtype.Duration // Sort direction constants for Sort operations. Ascending SortDirection = false Descending SortDirection = true )
Type constants for all supported data types.
const ( Nanoseconds = dtype.Nanoseconds Microseconds = dtype.Microseconds Milliseconds = dtype.Milliseconds )
TimeUnit constants.
const ( AggSum = dataframe.AggSum AggMean = dataframe.AggMean AggMin = dataframe.AggMin AggMax = dataframe.AggMax AggCount = dataframe.AggCount AggFirst = dataframe.AggFirst AggLast = dataframe.AggLast )
Aggregation function constants.
const ( InnerJoin = dataframe.InnerJoin LeftJoin = dataframe.LeftJoin RightJoin = dataframe.RightJoin FullJoin = dataframe.FullJoin SemiJoin = dataframe.SemiJoin AntiJoin = dataframe.AntiJoin CrossJoin = dataframe.CrossJoin )
Join type constants.
Variables ¶
var ( // WithSeparator sets the CSV field separator character. WithSeparator = csvio.WithSeparator // WithNullValues sets the strings treated as null in CSV reading. WithNullValues = csvio.WithNullValues // WithCSVColumns restricts CSV reading to the named columns. WithCSVColumns = csvio.WithColumns // WithInferSchemaLength sets how many rows to scan for CSV type inference. WithInferSchemaLength = csvio.WithInferSchemaLength // WithSkipRows skips the first n rows of a CSV file. WithSkipRows = csvio.WithSkipRows // WithNRows limits CSV reading to at most n data rows. WithNRows = csvio.WithNRows // WithHasHeader controls whether the first CSV row is a header. WithHasHeader = csvio.WithHasHeader )
CSV option re-exports for convenience.
var WithParquetCompression = parquetio.WithCompression
WithParquetCompression sets the compression codec for Parquet writing. Supported values: "none" (or "uncompressed"), "snappy".
Functions ¶
func WriteCSVFile ¶
func WriteCSVFile(df *DataFrame, path string, opts ...csvio.WriteOption) error
WriteCSVFile writes a DataFrame to a CSV file at the given path.
func WriteExcel ¶
WriteExcel writes a DataFrame as an Excel .xlsx file to an io.Writer.
func WriteExcelFile ¶
WriteExcelFile writes a DataFrame to an Excel .xlsx file at the given path.
func WriteJSONFile ¶
WriteJSONFile writes a DataFrame as JSON to a file.
func WriteNDJSON ¶
WriteNDJSON writes a DataFrame as NDJSON to an io.Writer.
func WriteNDJSONFile ¶
WriteNDJSONFile writes a DataFrame as NDJSON to a file.
func WriteParquet ¶
func WriteParquet(df *DataFrame, w io.Writer, opts ...WriteParquetOption) error
WriteParquet writes a DataFrame as Parquet to an io.Writer.
func WriteParquetFile ¶
func WriteParquetFile(df *DataFrame, path string, opts ...WriteParquetOption) error
WriteParquetFile writes a DataFrame to a Parquet file at the given path.
Types ¶
type CumNamespace ¶
type CumNamespace = expr.CumNamespace
CumNamespace provides cumulative operations on expressions.
type DataFrame ¶
DataFrame is an immutable collection of named, typed columns (Series).
func ConcatDataFrames ¶
ConcatDataFrames vertically concatenates DataFrames that share the same schema.
Example ¶
package main
import (
"fmt"
"github.com/msjurset/golars"
)
func main() {
df1, _ := golars.NewDataFrame(
golars.NewInt64Series("x", []int64{1, 2}),
)
df2, _ := golars.NewDataFrame(
golars.NewInt64Series("x", []int64{3, 4}),
)
combined, _ := golars.ConcatDataFrames(df1, df2)
fmt.Println(combined.Height())
}
Output: 4
func ConcatDataFramesHorizontal ¶
ConcatDataFramesHorizontal concatenates DataFrames side by side.
func DataFrameFromSchema ¶
DataFrameFromSchema creates an empty DataFrame with the given schema and row count.
func GroupByAgg ¶
func GroupByAgg(g *GroupByResult, exprs ...Expr) (*DataFrame, error)
GroupByAgg performs expression-based groupby aggregation.
func NewDataFrame ¶
NewDataFrame creates a new DataFrame from the given Series columns. All columns must have the same length and unique names.
Example ¶
package main
import (
"fmt"
"github.com/msjurset/golars"
)
func main() {
df, _ := golars.NewDataFrame(
golars.NewInt64Series("age", []int64{25, 30, 35}),
golars.NewStringSeries("name", []string{"Alice", "Bob", "Charlie"}),
)
fmt.Println(df.Height(), df.Width())
}
Output: 3 2
Example (Basic) ¶
package main
import (
"fmt"
"github.com/msjurset/golars"
)
func main() {
df, err := golars.NewDataFrame(
golars.NewInt64Series("id", []int64{1, 2, 3}),
golars.NewStringSeries("name", []string{"Alice", "Bob", "Charlie"}),
golars.NewFloat64Series("score", []float64{88.5, 92.3, 76.1}),
)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Printf("rows=%d cols=%d\n", df.Height(), df.Width())
col, _ := df.Column("name")
for i := 0; i < col.Len(); i++ {
v, _ := col.GetString(i)
fmt.Println(v)
}
}
Output: rows=3 cols=3 Alice Bob Charlie
func ReadCSV ¶
func ReadCSV(path string, opts ...ReadCSVOption) (*DataFrame, error)
ReadCSV reads a CSV file into a DataFrame.
Example ¶
package main
import (
"bytes"
"fmt"
"strings"
"github.com/msjurset/golars"
)
func main() {
// Write CSV data to a buffer, then read it back with options.
csvData := "name|age|active\nAlice|25|true\nBob|30|false\n"
reader := strings.NewReader(csvData)
df, err := golars.ReadCSVFromReader(reader, golars.WithSeparator('|'))
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Printf("rows=%d cols=%d\n", df.Height(), df.Width())
// Round-trip to CSV with default separator.
var buf bytes.Buffer
_ = golars.WriteCSV(df, &buf)
fmt.Print(buf.String())
}
Output: rows=2 cols=3 name,age,active Alice,25,true Bob,30,false
func ReadCSVFromReader ¶
func ReadCSVFromReader(r io.Reader, opts ...ReadCSVOption) (*DataFrame, error)
ReadCSVFromReader reads CSV data from an io.Reader into a DataFrame.
func ReadCSVWithContext ¶
func ReadCSVWithContext(ctx context.Context, path string, opts ...ReadCSVOption) (*DataFrame, error)
ReadCSVWithContext reads a CSV file into a DataFrame with cancellation support.
func ReadExcelFromReader ¶
ReadExcelFromReader reads an Excel file from an io.ReaderAt with the given size.
func ReadJSONFromReader ¶
ReadJSONFromReader reads JSON data from an io.Reader into a DataFrame.
func ReadNDJSON ¶
ReadNDJSON reads a newline-delimited JSON file into a DataFrame.
func ReadNDJSONFromReader ¶
ReadNDJSONFromReader reads NDJSON from an io.Reader into a DataFrame.
func ReadParquet ¶
ReadParquet reads a Parquet file into a DataFrame.
func ReadParquetFromReader ¶
func ReadParquetFromReader(r io.ReadSeeker) (*DataFrame, error)
ReadParquetFromReader reads Parquet data from an io.ReadSeeker into a DataFrame.
type DtNamespace ¶
type DtNamespace = expr.DtNamespace
DtNamespace provides temporal operations on expressions.
type ExprContext ¶
ExprContext holds the evaluation context for expressions.
type GroupByExpr ¶
type GroupByExpr = dataframe.GroupByExpr
GroupByExpr is an interface for expression-based GroupBy aggregation.
type GroupByResult ¶
type GroupByResult = dataframe.GroupByResult
GroupByResult holds the result of a GroupBy operation.
type LazyFrame ¶
LazyFrame represents a lazy computation over a DataFrame. Operations are recorded and only executed when Collect is called.
func Lazy ¶
Lazy creates a LazyFrame from an eager DataFrame.
Example ¶
package main
import (
"fmt"
"github.com/msjurset/golars"
)
func main() {
df, _ := golars.NewDataFrame(
golars.NewStringSeries("name", []string{"Alice", "Bob", "Charlie", "Diana"}),
golars.NewInt64Series("age", []int64{25, 30, 35, 40}),
golars.NewFloat64Series("score", []float64{88.5, 92.3, 76.1, 95.0}),
)
result, _ := golars.Lazy(df).
Filter(golars.Col("age").Gt(golars.Lit(25))).
Sort("score", true). // descending
Head(2).
Collect()
nameCol, _ := result.Column("name")
scoreCol, _ := result.Column("score")
for i := 0; i < result.Height(); i++ {
name, _ := nameCol.GetString(i)
score, _ := scoreCol.GetFloat64(i)
fmt.Printf("%s: %.1f\n", name, score)
}
}
Output: Diana: 95.0 Bob: 92.3
func ScanCSV ¶
func ScanCSV(path string, opts ...ReadCSVOption) *LazyFrame
ScanCSV creates a LazyFrame that defers reading a CSV file until Collect.
func ScanParquet ¶
ScanParquet creates a LazyFrame that defers reading a Parquet file until Collect.
type LazyGroupBy ¶
type LazyGroupBy = lazy.LazyGroupBy
LazyGroupBy holds a deferred groupby operation on a LazyFrame.
type NameNamespace ¶
type NameNamespace = expr.NameNamespace
NameNamespace provides operations on expression output names.
type ReadCSVOption ¶
type ReadCSVOption = csvio.ReadOption
ReadCSVOption is a functional option for CSV reading.
type RollingNamespace ¶
type RollingNamespace = expr.RollingNamespace
RollingNamespace provides rolling window operations on expressions.
type RowAccessor ¶
type RowAccessor = dataframe.RowAccessor
RowAccessor provides typed access to a single row of a DataFrame. Use with the DataFrame.Rows() iterator.
type SQLContext ¶
SQLContext holds registered DataFrames that can be queried via SQL.
Example ¶
package main
import (
"fmt"
"github.com/msjurset/golars"
)
func main() {
df, _ := golars.NewDataFrame(
golars.NewInt64Series("id", []int64{1, 2, 3, 4}),
golars.NewStringSeries("name", []string{"Alice", "Bob", "Charlie", "Diana"}),
golars.NewFloat64Series("score", []float64{88.5, 92.3, 76.1, 95.0}),
)
ctx := golars.NewSQLContext()
ctx.Register("students", df)
result, _ := ctx.Execute("SELECT name, score FROM students WHERE score > 80 ORDER BY score DESC LIMIT 3")
nameCol, _ := result.Column("name")
scoreCol, _ := result.Column("score")
for i := 0; i < result.Height(); i++ {
name, _ := nameCol.GetString(i)
score, _ := scoreCol.GetFloat64(i)
fmt.Printf("%s: %.1f\n", name, score)
}
}
Output: Diana: 95.0 Bob: 92.3 Alice: 88.5
func NewSQLContext ¶
func NewSQLContext() *SQLContext
NewSQLContext creates a new SQL context for querying DataFrames with SQL.
type Series ¶
Series is a named, typed column of data.
Example (Aggregation) ¶
package main
import (
"fmt"
"github.com/msjurset/golars"
)
func main() {
s := golars.NewFloat64Series("values", []float64{10, 20, 30, 40, 50})
sum, _ := s.Sum()
mean, _ := s.Mean()
min, _ := s.Min()
max, _ := s.Max()
fmt.Printf("sum=%.0f mean=%.0f min=%.0f max=%.0f\n", sum, mean, min, max)
fmt.Println("count:", s.Count())
fmt.Println("nunique:", s.NUnique())
}
Output: sum=150 mean=30 min=10 max=50 count: 5 nunique: 5
Example (NullHandling) ¶
package main
import (
"fmt"
"github.com/msjurset/golars"
)
func main() {
s := golars.NewInt64SeriesWithValidity("data",
[]int64{10, 0, 30, 0, 50},
[]bool{true, false, true, false, true},
)
fmt.Println("nulls:", s.NullCount())
fmt.Println("has_nulls:", s.HasNulls())
// Drop nulls.
clean := s.DropNulls()
fmt.Println("after drop:", clean.Len())
// Fill nulls.
filled := s.FillNullInt64(0)
for i := 0; i < filled.Len(); i++ {
v, _ := filled.GetInt64(i)
fmt.Print(v, " ")
}
fmt.Println()
}
Output: nulls: 2 has_nulls: true after drop: 3 10 0 30 0 50
Example (Rolling) ¶
package main
import (
"fmt"
"github.com/msjurset/golars"
)
func main() {
s := golars.NewFloat64Series("x", []float64{1, 2, 3, 4, 5})
rm := s.RollingMean(3)
for i := 0; i < rm.Len(); i++ {
if rm.IsNull(i) {
fmt.Println("null")
} else {
v, _ := rm.GetFloat64(i)
fmt.Printf("%.1f\n", v)
}
}
}
Output: null null 2.0 3.0 4.0
func NewBooleanSeries ¶
NewBooleanSeries creates a new Series of boolean values.
func NewBooleanSeriesWithValidity ¶
NewBooleanSeriesWithValidity creates a Boolean Series with explicit null tracking.
func NewDateSeries ¶
NewDateSeries creates a new Series of Date values (days since Unix epoch as int32).
func NewDateSeriesFromTime ¶
NewDateSeriesFromTime creates a Date Series from a slice of time.Time values.
func NewDateTimeSeries ¶
NewDateTimeSeries creates a new Series of DateTime values (microseconds since epoch as int64).
func NewDateTimeSeriesFromTime ¶
NewDateTimeSeriesFromTime creates a DateTime Series from a slice of time.Time values.
func NewDurationSeries ¶
NewDurationSeries creates a new Series of Duration values (microseconds as int64).
func NewFloat32Series ¶
NewFloat32Series creates a new Series of float32 values.
func NewFloat64Series ¶
NewFloat64Series creates a new Series of float64 values.
func NewFloat64SeriesWithValidity ¶
NewFloat64SeriesWithValidity creates a Float64 Series with explicit null tracking.
func NewInt8Series ¶
NewInt8Series creates a new Series of int8 values.
func NewInt16Series ¶
NewInt16Series creates a new Series of int16 values.
func NewInt32Series ¶
NewInt32Series creates a new Series of int32 values.
func NewInt64Series ¶
NewInt64Series creates a new Series of int64 values.
func NewInt64SeriesWithValidity ¶
NewInt64SeriesWithValidity creates an Int64 Series with explicit null tracking. Positions where valid[i] is false are marked as null.
func NewStringSeries ¶
NewStringSeries creates a new Series of string values.
func NewStringSeriesWithValidity ¶
NewStringSeriesWithValidity creates a String Series with explicit null tracking.
func NewTimeSeries ¶
NewTimeSeries creates a new Series of Time values (nanoseconds since midnight as int64).
func NewUInt8Series ¶
NewUInt8Series creates a new Series of uint8 values.
func NewUInt16Series ¶
NewUInt16Series creates a new Series of uint16 values.
func NewUInt32Series ¶
NewUInt32Series creates a new Series of uint32 values.
func NewUInt64Series ¶
NewUInt64Series creates a new Series of uint64 values.
type SortDirection ¶
type SortDirection bool
SortDirection specifies ascending or descending sort order.
type StrNamespace ¶
type StrNamespace = expr.StrNamespace
StrNamespace provides string operations on expressions.
type ThenBuilder ¶
type ThenBuilder = expr.ThenBuilder
ThenBuilder is the intermediate builder after When().Then().
type WhenBuilder ¶
type WhenBuilder = expr.WhenBuilder
WhenBuilder is the intermediate builder for When/Then/Otherwise chains.
func When ¶
func When(condition Expr) *WhenBuilder
When starts a conditional expression chain.
Example ¶
package main
import (
"fmt"
"github.com/msjurset/golars"
)
func main() {
df, _ := golars.NewDataFrame(
golars.NewStringSeries("name", []string{"Alice", "Bob", "Charlie"}),
golars.NewFloat64Series("score", []float64{95.0, 72.0, 88.0}),
)
ctx := &golars.ExprContext{DF: df}
grade, _ := golars.When(golars.Col("score").Gte(golars.Lit(90.0))).
Then(golars.Lit("A")).
Otherwise(golars.Lit("B")).
Evaluate(ctx)
for i := 0; i < grade.Len(); i++ {
v, _ := grade.GetString(i)
fmt.Println(v)
}
}
Output: A B B
type WriteParquetOption ¶
type WriteParquetOption = parquetio.WriteOption
WriteParquetOption is a functional option for Parquet writing.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
array
Package array provides columnar storage arrays for the Golars DataFrame library.
|
Package array provides columnar storage arrays for the Golars DataFrame library. |
|
bitmap
Package bitmap provides a compact bitset implementation for null tracking in columnar data structures.
|
Package bitmap provides a compact bitset implementation for null tracking in columnar data structures. |
|
dataframe
Package dataframe provides the DataFrame type, an immutable collection of named, typed columns (Series) that supports relational-style operations.
|
Package dataframe provides the DataFrame type, an immutable collection of named, typed columns (Series) that supports relational-style operations. |
|
dtype
Package dtype defines the type system for the Golars DataFrame library.
|
Package dtype defines the type system for the Golars DataFrame library. |
|
expr
Package expr provides a composable expression DSL for evaluating column-level operations against a DataFrame context.
|
Package expr provides a composable expression DSL for evaluating column-level operations against a DataFrame context. |
|
io/csv
Package csv provides CSV reading and writing for Golars DataFrames.
|
Package csv provides CSV reading and writing for Golars DataFrames. |
|
io/database
Package database provides DataFrame I/O via Go's database/sql interface.
|
Package database provides DataFrame I/O via Go's database/sql interface. |
|
io/excel
Package excel provides XLSX reading and writing for Golars DataFrames.
|
Package excel provides XLSX reading and writing for Golars DataFrames. |
|
io/json
Package json provides JSON and NDJSON reading and writing for Golars DataFrames.
|
Package json provides JSON and NDJSON reading and writing for Golars DataFrames. |
|
io/parquet
Package parquet provides a minimal Apache Parquet reader and writer for the golars DataFrame library.
|
Package parquet provides a minimal Apache Parquet reader and writer for the golars DataFrame library. |
|
series
Package series provides the Series type, a named typed column of data.
|
Package series provides the Series type, a named typed column of data. |
|
sql
Package sql provides a SQL query interface for golars DataFrames.
|
Package sql provides a SQL query interface for golars DataFrames. |