Skip to content
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
23 changes: 21 additions & 2 deletions dataframe.go
Original file line number Diff line number Diff line change
Expand Up @@ -1365,10 +1365,12 @@ func (e *dfEnv) apply(c *rb.Call) (any, error) {
df := frameOf(c)
cols := df.Columns()
var blockErr error
row := -1
s, err := df.Apply(name, func(r dataframe.Row) any {
if blockErr != nil {
return nil
}
row++
ps := make([]any, len(cols))
for i, col := range cols {
ps[i] = []any{col, e.outAny(r.Get(col))}
Expand All @@ -1379,7 +1381,7 @@ func (e *dfEnv) apply(c *rb.Call) (any, error) {
blockErr = err
return nil
}
out, err := dfValue(v.Go())
out, err := blockResult(v, row)
if err != nil {
blockErr = err
}
Expand All @@ -1391,6 +1393,21 @@ func (e *dfEnv) apply(c *rb.Call) (any, error) {
return e.result(s, err)
}

// blockResult is what a map/apply block returned for a row, as one cell. A
// column holds what go-dataframe's fromValues takes (nil, bool, numbers,
// string, time.Time); the Ruby classes that become those are allowed, and
// anything else (Array, Hash, Series, an object) is refused by class, before
// Go() or dfValue converts it: a block returning a big Array or Hash per row
// of a large Series would otherwise build every one first.
func blockResult(v rb.Value, row int) (any, error) {
switch cls := v.ClassName(); cls {
case "NilClass", "TrueClass", "FalseClass", "Integer", "Float", "Complex", "String", "Symbol", "Time":
return dfValue(v.Go())
default:
return nil, argError("the block returned a %s for row %d: a column holds nil, booleans, numbers, strings, symbols and Times", cls, row)
}
}

func (e *dfEnv) resample(c *rb.Call) (any, error) {
if err := kwOnly(c, "on"); err != nil {
return nil, err
Expand Down Expand Up @@ -1496,16 +1513,18 @@ func (e *dfEnv) seriesMap(c *rb.Call) (any, error) {
return nil, err
}
var blockErr error
row := -1
s, err := seriesOf(c).Map(func(x any) any {
if blockErr != nil {
return nil
}
row++
v, err := b.Call(e.outAny(x))
if err != nil {
blockErr = err
return nil
}
out, err := dfValue(v.Go())
out, err := blockResult(v, row)
if err != nil {
blockErr = err
}
Expand Down
44 changes: 42 additions & 2 deletions dfvalue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ str = DataFrame::Series.new(["Lyon", "Rome"])`)
`df.loc(ARG)`,
`df.loc(ARG, ["x"])`,
`s.loc(ARG)`,
`s.map { ARG }`,
`df.apply { ARG }`,
`str.isin(ARG)`,
} {
for arg, msg := range map[string]string{
Expand All @@ -52,6 +50,48 @@ str = DataFrame::Series.new(["Lyon", "Rome"])`)
}
}

// A column holds scalars (go-dataframe refuses a []any), so a map/apply block
// returning an Array is refused at the first row it returns one, naming the
// row, before any conversion: 200k rows of a big Array stay small, not
// seconds and GiB.
func TestMapAndApplyRefuseAnArrayResultBeforeConverting(t *testing.T) {
defer debug.SetMemoryLimit(debug.SetMemoryLimit(1 << 30))
in := newInterp(t)
eval(t, in, `cyc = []; cyc << cyc
deep = 0; 70.times { deep = [deep] }
b = (1..64).to_a
big = DataFrame::Series.new((1..20_000).to_a)
bigdf = DataFrame.new(x: (1..20_000).to_a)
h = {}; 1000.times { |i| h[i] = i }
df = DataFrame.new(x: [1, 2], y: [3, 4])
s = DataFrame::Series.new([1, 2, 3])`)
for _, src := range []string{`s.map { cyc }`, `s.map { deep }`, `s.map { [cyc, 1] }`, `s.map { [1] }`, `df.apply { cyc }`, `df.apply { [1] }`} {
evalErr(t, in, src, "ArgumentError", "returned a Array for row 0")
}
evalErr(t, in, `s.map { |x| x == 2 ? [x] : x }`, "ArgumentError", "returned a Array for row 1")
evalErr(t, in, `s.map { h }`, "ArgumentError", "returned a Hash for row 0")
evalErr(t, in, `df.apply { h }`, "ArgumentError", "returned a Hash for row 0")
evalErr(t, in, `s.map { s }`, "ArgumentError", "for row 0")
evalErr(t, in, `s.map { Object.new }`, "ArgumentError", "returned a Object for row 0")
for _, src := range []string{`big.map { b }`, `big.map { h }`, `bigdf.apply { h }`, `big.map { big }`} {
var before, after runtime.MemStats
runtime.ReadMemStats(&before)
evalErr(t, in, src, "ArgumentError", "for row 0")
runtime.ReadMemStats(&after)
if got := after.TotalAlloc - before.TotalAlloc; got > 16<<20 {
t.Errorf("refusing %s allocated %d MiB, want under 16", src, got>>20)
}
}
// Scalar, nil, Symbol and Time results still map.
eval(t, in, `raise "bad" unless s.map { |x| x * 2 }.to_a == [2, 4, 6]
raise "bad" unless s.map { |x| x == 2 ? nil : x }.to_a == [1, nil, 3]
raise "bad" unless s.map { |x| x.odd? }.to_a == [true, false, true]
raise "bad" unless s.map { |x| x * 1.5 }.to_a == [1.5, 3.0, 4.5]
raise "bad" unless s.map { |x| :a }.to_a == ["a", "a", "a"]
raise "bad" unless s.map { |x| x.to_s }.to_a == ["1", "2", "3"]
s.map { |x| Time.at(x) }`)
}

// An Array shared at several places is converted once: 21 doublings are 2^21
// leaves from a few dozen objects, and a call that converts it once per key or
// row (query keywords, Series#map, DataFrame.new) must stay small however many
Expand Down
Loading