Skip to content

Commit

Permalink
Change DataFrame.Matrix to convert the matrix on creation
Browse files Browse the repository at this point in the history
  • Loading branch information
kniren committed Dec 10, 2016
1 parent bd7cade commit f18e051
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions dataframe/dataframe.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,14 @@ func (df DataFrame) Copy() DataFrame {

// String implements the Stringer interface for DataFrame
func (df DataFrame) String() (str string) {
return df.print(true, true, true, true, 10, 70)
return df.print(true, true, true, true, 10, 70, "DataFrame")
}

func (df DataFrame) print(
shortRows, shortCols, showDims, showTypes bool,
maxRows int,
maxCharsTotal int) (str string) {
maxCharsTotal int,
class string) (str string) {

addRightPadding := func(s string, nchar int) string {
if utf8.RuneCountInString(s) < nchar {
Expand All @@ -103,12 +104,12 @@ func (df DataFrame) print(
}

if df.Err != nil {
str = "DataFrame error: " + df.Err.Error()
str = fmt.Sprintf("%v error: %v", class, df.Err)
return
}
nrows, ncols := df.Dims()
if nrows == 0 || ncols == 0 {
str = "Empty DataFrame"
str = fmt.Sprintf("Empty %v", class)
return
}
idx := make([]int, maxRows)
Expand All @@ -126,7 +127,7 @@ func (df DataFrame) print(
}

if showDims {
str += fmt.Sprintf("[%vx%v] DataFrame\n\n", nrows, ncols)
str += fmt.Sprintf("[%vx%v] %v\n\n", nrows, ncols, class)
}

// Add the row numbers
Expand Down Expand Up @@ -1366,7 +1367,17 @@ func (df DataFrame) Maps() []map[string]interface{} {

// Matrix returns the mat64.Matrix representation of a DataFrame
func (df DataFrame) Matrix() mat64.Matrix {
return matrix{df}
columns := make([]series.Series, df.ncols)
for i := 0; i < df.ncols; i++ {
columns[i] = series.New(df.columns[i], series.Float, df.columns[i].Name)
}
m := DataFrame{
columns: columns,
ncols: df.ncols,
nrows: df.nrows,
Err: df.Err,
}
return matrix{m}
}

// fixColnames assigns a name to the missing column names and makes it so that the
Expand Down Expand Up @@ -1566,6 +1577,10 @@ type matrix struct {
DataFrame
}

func (m matrix) String() string {
return m.print(true, true, true, true, 10, 70, "Matrix")
}

func (m matrix) At(i, j int) float64 {
return m.columns[j].Elem(i).Float()
}
Expand Down

0 comments on commit f18e051

Please sign in to comment.