Skip to content

Commit 74248b7

Browse files
committed
Fixed replace-values. It is no longer a good idea to assume a cloned column has a writable buffer.
1 parent 0376055 commit 74248b7

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

src/tech/v3/dataset_api.clj

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -600,30 +600,41 @@ test/data/stocks.csv [10 3]:
600600

601601
(defn- update-values
602602
[col missing scalar-val]
603-
(if (== 0 (dtype/ecount missing))
604-
col
605-
(let [col (Column. (bitmap/->bitmap)
606-
(dtype/clone (ds-proto/column-buffer col))
607-
(meta col) nil)]
608-
(reduce (hamf-rf/long-accumulator
609-
data idx (.writeObject ^Buffer data idx scalar-val) data)
610-
(dtype/->buffer col)
611-
missing)
612-
col)))
603+
(let [missing (bitmap/->bitmap missing)]
604+
(if (.isEmpty missing)
605+
col
606+
(let [cbuf (dtype/->buffer (ds-proto/column-buffer col))
607+
col-dt (dtype/elemwise-datatype col)
608+
ec (.lsize cbuf)]
609+
(Column. (bitmap/->bitmap)
610+
(dtype/make-reader-fn
611+
col-dt col-dt ec
612+
(case (casting/simple-operation-space col-dt)
613+
:int64 (let [sv (long scalar-val)]
614+
(hamf-fn/long-unary-operator
615+
idx
616+
(if (.contains missing idx)
617+
sv
618+
(.readLong cbuf idx))))
619+
:float64 (let [sv (double scalar-val)]
620+
(hamf-fn/long->double
621+
idx
622+
(if (.contains missing idx)
623+
sv
624+
(.readDouble cbuf idx))))
625+
(hamf-fn/long->obj
626+
idx
627+
(if (.contains missing idx)
628+
scalar-val
629+
(.readObject cbuf idx)))))
630+
(meta col) nil)))))
613631

614632

615633
(defn replace-missing-value
616634
([dataset filter-fn-or-ds scalar-value]
617635
(update-columnwise dataset filter-fn-or-ds
618636
(fn [col]
619-
(let [missing (ds-col/missing col)]
620-
(if (.isEmpty missing)
621-
col
622-
(ds-col/new-column
623-
(:name (meta col))
624-
(update-values col missing scalar-value)
625-
(meta col)
626-
(bitmap/->bitmap)))))))
637+
(update-values col (missing col) scalar-value))))
627638
([dataset scalar-value]
628639
(replace-missing-value dataset identity scalar-value)))
629640

test/tech/v3/dataset_test.clj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,12 @@
857857
(is (= [5.0 5.0 5.0 1.0 2.0 5.0 5.0 5.0 5.0 5.0 4.0 5.0 11.0 5.0 5.0]
858858
(vec ((ds/replace-missing ds :all :value 5.0) :a))))))
859859

860+
(deftest replace-missing-string-table
861+
(is (= ["one" "two" "three"]
862+
(-> (ds/->dataset {:a ["one" nil "three"]})
863+
(ds/replace-missing-value "two")
864+
(ds/column :a)))))
865+
860866

861867
(deftest replace-missing-all-values-missing
862868
(let [empty-col (ds/->dataset {:a [nil nil]})]

0 commit comments

Comments
 (0)