rocketlaunchr / dataframe-go

DataFrames for Go: For statistics, machine-learning, and data manipulation/exploration
Other
1.19k stars 95 forks source link

Getting back Float64/Int64/Mixed series from dataframe #30

Closed patgunner closed 4 years ago

patgunner commented 4 years ago

I wanted to know if there is a way to convert a series interface to get the original type of series (Float64/Int64/Mixed) underneath it. I will describe mu use case.

After creating a dataframe, I am trying to use gonum to do some analysis. For eg. linear regression of two series from dataframe. But for this I have to iterate over the whole series(using ValuesIterator) to get back each element into a []float64, which is required by gonum. ToSeriesFloat64 does not help since it is not implemented by Series.

Is there an easier way to access the whole underlying series into into corresponding concrete slice?

pjebs commented 4 years ago

For gonum use only *SeriesFloat64. You can then access .Values field.

pjebs commented 4 years ago

That is a []float64

pjebs commented 4 years ago

You can type assert series interface to *SeriesFloat64 providing the underlying is actually a SeriesFloat64

pjebs commented 4 years ago

https://godoc.org/github.com/rocketlaunchr/dataframe-go#SeriesFloat64

pjebs commented 4 years ago
s1 := dataframe.NewSeriesInt64("day", nil, 1, 2, 3, 4, 5, 6, 7, 8)
s2 := dataframe.NewSeriesFloat64("sales", nil, 50.3, 23.4, 56.2, nil, nil, 84.2, 72, 89)
df := dataframe.NewDataFrame(s1, s2)

df.Series[2].(*dataframe.SeriesFloat64).Values // []float64 for use with gonum

or df.Series[df.MustNameToColumn("sales")].Values
patgunner commented 4 years ago

Thanks @pjebs . That worked for me. One other small input I wanted for the following scenario.

If I have an input in the form of []interface{} and I want to add this to a SeriesFloat64/SeriesInt64 series, would there be some easy way to achieve this?

This is in continuation to as you mentioned "For gonum use only *SeriesFloat64". :)

pjebs commented 4 years ago
var vals []interface{}
dataframe.NewSeriesFloat64("x", nil, vals...) // when creating new series

But it doesn't seem like you can insert slice of interface into an existing Series.

But the underlying problem is your data should not be in []interface{}