Search code examples
stringnullkdbfill

kdb: how to fill empty values of string column with previous value


I have a table like this:

t:([] c1:1 0n 3 4;c2:("abcd";"efgh";"igkl";""))
c1 c2    
---------
1  "abcd"
  "efgh"
3  "igkl"
4  ""

I want to use the previous line to fill the empty values (including the empty string ""), the expected result is this:

c1 c2    
---------
1  "abcd"
1  "efgh"
3  "igkl"
4  "igkl"

I tried to do this with the function fills, but without success,

fills t

The content of the error is "length: Incompatible list lengths." Thank you to those who can help me.


Solution

  • For a large table the below might be quicker than the other answers (with the caveat that it's a good deal more memory intensive than Sean's):

    select {x (where l) -1+sums l:not x~\:""}[c2] from t
    c2
    ------
    "abcd"
    "efgh"
    "igkl"
    "igkl"
    
    q)t2:100000#t
    q)\ts select {x (where l) -1+sums l:not x~\:""}[c2] from t2
    2 3277952
    q)\ts select {$[count y;y;x]}\[c2] from t2
    11 1049888
    
    
    

    This avoids the loop by creating a list of booleans showing where x is not empty. We also get the actual indices of the non-empty values using where. We can sums our boolean list to get a list of indices which we use to pull out only the required fill values from our original list.