Skip to content

Commit 5557ee7

Browse files
committed
defining resize! for Datasets
1 parent 18001aa commit 5557ee7

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/dataset/other.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,3 +1389,29 @@ function _permute_ds_after_sort!(ds, perm; check = true, cols = :, threads = tru
13891389
end
13901390
_modified(_attributes(ds))
13911391
end
1392+
1393+
"""
1394+
resize!(ds::Dataset, n::Integer)
1395+
1396+
Resize `ds` to contain `n` elements. If `n` is smaller than the number of rows in `ds`, the first `n` elements will be retained. If `n` is larger, the new elements are initialized with missing.
1397+
"""
1398+
function Base.resize!(ds::Dataset, n::Integer)
1399+
nrows = nrow(ds)
1400+
ncols = ncol(ds)
1401+
current_col=0
1402+
try
1403+
for j in 1:ncols
1404+
resize!(_columns(ds)[j], n)
1405+
_columns(ds)[j][nrows+1:n] .= missing # new rows must be initialised by missing
1406+
current_col += 1
1407+
end
1408+
_reset_grouping_info!(ds)
1409+
_modified(_attributes(ds))
1410+
catch err
1411+
for j in 1:current_col
1412+
resize!(_columns(ds)[j], nrows)
1413+
end
1414+
rethrow(err)
1415+
end
1416+
ds
1417+
end

0 commit comments

Comments
 (0)