Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unified error handling #124

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions dataframe/dataframe.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"
"unicode/utf8"

"github.com/go-gota/gota/errors"
"github.com/go-gota/gota/series"
)

Expand All @@ -38,7 +39,7 @@ type DataFrame struct {
// New is the generic DataFrame constructor
func New(se ...series.Series) DataFrame {
if se == nil || len(se) == 0 {
return DataFrame{Err: fmt.Errorf("empty DataFrame")}
return DataFrame{Err: errors.ErrEmptyDataFrame}
}

columns := make([]series.Series, len(se))
Expand Down Expand Up @@ -80,7 +81,7 @@ func checkColumnsDimensions(se ...series.Series) (nrows, ncols int, err error) {
nrows = s.Len()
}
if nrows != s.Len() {
err = fmt.Errorf("arguments have different dimensions")
err = errors.ErrDimensionsDiffers
return
}
}
Expand Down Expand Up @@ -380,7 +381,7 @@ func (df DataFrame) Rename(newname, oldname string) DataFrame {
colnames := df.Names()
idx := findInStringSlice(oldname, colnames)
if idx == -1 {
return DataFrame{Err: fmt.Errorf("rename: can't find column name")}
return DataFrame{Err: errors.ErrUnknownColumn}
}

copy := df.Copy()
Expand Down Expand Up @@ -480,7 +481,7 @@ func (df DataFrame) Filter(filters ...F) DataFrame {
for i, f := range filters {
idx := findInStringSlice(f.Colname, df.Names())
if idx < 0 {
return DataFrame{Err: fmt.Errorf("filter: can't find column name")}
return DataFrame{Err: errors.ErrUnknownColumn}
}
res := df.columns[idx].Compare(f.Comparator, f.Comparando)
if err := res.Err; err != nil {
Expand Down Expand Up @@ -530,7 +531,7 @@ func (df DataFrame) Arrange(order ...Order) DataFrame {
return df
}
if order == nil || len(order) == 0 {
return DataFrame{Err: fmt.Errorf("rename: no arguments")}
return DataFrame{Err: errors.ErrNoArgs}
}

// Check that all colnames exist before starting to sort
Expand Down Expand Up @@ -636,7 +637,7 @@ func (df DataFrame) Rapply(f func(series.Series) series.Series) DataFrame {
}

if rowlen != -1 && rowlen != row.Len() {
return DataFrame{Err: fmt.Errorf("error applying function: rows have different lengths")}
return DataFrame{Err: errors.ErrDimensionsDiffers}
}
rowlen = row.Len()

Expand Down Expand Up @@ -936,16 +937,16 @@ func LoadRecords(records [][]string, options ...LoadOption) DataFrame {
}

if len(records) == 0 {
return DataFrame{Err: fmt.Errorf("load records: empty DataFrame")}
return DataFrame{Err: errors.ErrEmptyDataFrame}
}
if cfg.hasHeader && len(records) <= 1 {
return DataFrame{Err: fmt.Errorf("load records: empty DataFrame")}
return DataFrame{Err: errors.ErrEmptyDataFrame}
}
if cfg.names != nil && len(cfg.names) != len(records[0]) {
if len(cfg.names) > len(records[0]) {
return DataFrame{Err: fmt.Errorf("load records: too many column names")}
return DataFrame{Err: errors.ErrTooManyCols}
}
return DataFrame{Err: fmt.Errorf("load records: not enough column names")}
return DataFrame{Err: errors.ErrTooFewCols}
}

// Extract headers
Expand Down Expand Up @@ -1187,8 +1188,10 @@ func (df DataFrame) SetNames(colnames ...string) error {
if df.Err != nil {
return df.Err
}
if len(colnames) != df.ncols {
return fmt.Errorf("setting names: wrong dimensions")
if len(colnames) < df.ncols {
return errors.ErrTooManyCols
} else if len(colnames) > df.ncols {
return errors.ErrTooFewCols
}
for k, s := range colnames {
df.columns[k].Name = s
Expand Down Expand Up @@ -1219,15 +1222,15 @@ func (df DataFrame) Col(colname string) series.Series {
// Check that colname exist on dataframe
idx := findInStringSlice(colname, df.Names())
if idx < 0 {
return series.Series{Err: fmt.Errorf("unknown column name")}
return series.Series{Err: errors.ErrUnknownColumn}
}
return df.columns[idx].Copy()
}

// InnerJoin returns a DataFrame containing the inner join of two DataFrames.
func (df DataFrame) InnerJoin(b DataFrame, keys ...string) DataFrame {
if len(keys) == 0 {
return DataFrame{Err: fmt.Errorf("join keys not specified")}
return DataFrame{Err: errors.ErrJoinKeysAreNotSpecified}
}
// Check that we have all given keys in both DataFrames
var iKeysA []int
Expand Down Expand Up @@ -1306,7 +1309,7 @@ func (df DataFrame) InnerJoin(b DataFrame, keys ...string) DataFrame {
// LeftJoin returns a DataFrame containing the left join of two DataFrames.
func (df DataFrame) LeftJoin(b DataFrame, keys ...string) DataFrame {
if len(keys) == 0 {
return DataFrame{Err: fmt.Errorf("join keys not specified")}
return DataFrame{Err: errors.ErrJoinKeysAreNotSpecified}
}
// Check that we have all given keys in both DataFrames
var iKeysA []int
Expand Down Expand Up @@ -1404,7 +1407,7 @@ func (df DataFrame) LeftJoin(b DataFrame, keys ...string) DataFrame {
// RightJoin returns a DataFrame containing the right join of two DataFrames.
func (df DataFrame) RightJoin(b DataFrame, keys ...string) DataFrame {
if len(keys) == 0 {
return DataFrame{Err: fmt.Errorf("join keys not specified")}
return DataFrame{Err: errors.ErrJoinKeysAreNotSpecified}
}
// Check that we have all given keys in both DataFrames
var iKeysA []int
Expand Down Expand Up @@ -1512,7 +1515,7 @@ func (df DataFrame) RightJoin(b DataFrame, keys ...string) DataFrame {
// OuterJoin returns a DataFrame containing the outer join of two DataFrames.
func (df DataFrame) OuterJoin(b DataFrame, keys ...string) DataFrame {
if len(keys) == 0 {
return DataFrame{Err: fmt.Errorf("join keys not specified")}
return DataFrame{Err: errors.ErrJoinKeysAreNotSpecified}
}
// Check that we have all given keys in both DataFrames
var iKeysA []int
Expand Down Expand Up @@ -1838,10 +1841,10 @@ func parseSelectIndexes(l int, indexes SelectIndexes, colnames []string) ([]int,
xs := indexes.(series.Series).Records()
return parseSelectIndexes(l, xs, colnames)
default:
return nil, fmt.Errorf("indexing error: unknown indexing mode")
return nil, errors.ErrUnknownIndexingMode
}
default:
return nil, fmt.Errorf("indexing error: unknown indexing mode")
return nil, errors.ErrUnknownIndexingMode
}
return idx, nil
}
Expand Down
14 changes: 14 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package errors

import "fmt"

var (
ErrEmptyDataFrame = fmt.Errorf("empty DataFrame")
ErrDimensionsDiffers = fmt.Errorf("arguments have different dimensions")
ErrUnknownColumn = fmt.Errorf("unknown column name")
ErrJoinKeysAreNotSpecified = fmt.Errorf("join keys not specified")
ErrNoArgs = fmt.Errorf("no arguments")
ErrTooManyCols = fmt.Errorf("too many column names")
ErrTooFewCols = fmt.Errorf("not enough column names")
ErrUnknownIndexingMode = fmt.Errorf("indexing error: unknown indexing mode")
)
9 changes: 5 additions & 4 deletions series/series.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package series

import (
"fmt"
"github.com/go-gota/gota/errors"
"reflect"
"sort"
"strings"
Expand Down Expand Up @@ -330,7 +331,7 @@ func (s Series) Set(indexes Indexes, newvalues Series) Series {
return s
}
if len(idx) != newvalues.Len() {
s.Err = fmt.Errorf("set error: dimensions mismatch")
s.Err = errors.ErrDimensionsDiffers
return s
}
for k, i := range idx {
Expand Down Expand Up @@ -433,7 +434,7 @@ func (s Series) Compare(comparator Comparator, comparando interface{}) Series {
// Multiple element comparison
if s.Len() != comp.Len() {
s := s.Empty()
s.Err = fmt.Errorf("can't compare: length mismatch")
s.Err = errors.ErrDimensionsDiffers
return s
}
for i := 0; i < s.Len(); i++ {
Expand Down Expand Up @@ -609,10 +610,10 @@ func parseIndexes(l int, indexes Indexes) ([]int, error) {
}
return parseIndexes(l, bools)
default:
return nil, fmt.Errorf("indexing error: unknown indexing mode")
return nil, errors.ErrUnknownIndexingMode
}
default:
return nil, fmt.Errorf("indexing error: unknown indexing mode")
return nil, errors.ErrUnknownIndexingMode
}
return idx, nil
}
Expand Down