|
287 | 287 | modify(origninal_ds::AbstractDataset, @nospecialize(args...)) = modify!(copy(origninal_ds), args...)
|
288 | 288 |
|
289 | 289 | modify!(ds::Dataset) = parent(ds)
|
290 |
| -function modify!(ds::Dataset, @nospecialize(args...)) |
291 |
| - idx_cpy = Index(copy(index(ds).lookup), copy(index(ds).names), copy(index(ds).format)) |
| 290 | +function modify!(ds::AbstractDataset, @nospecialize(args...)) |
| 291 | + if ds isa SubDataset |
| 292 | + idx_cpy = copy(index(parent(ds))) |
| 293 | + else |
| 294 | + idx_cpy = Index(copy(index(ds).lookup), copy(index(ds).names), copy(index(ds).format)) |
| 295 | + end |
292 | 296 | if isgrouped(ds)
|
293 | 297 | norm_var = normalize_modify_multiple!(idx_cpy, index(ds), args...)
|
294 | 298 | allnewvars = map(x -> x.second.second, norm_var)
|
|
339 | 343 | function _resize_result!(ds, _res, newcol)
|
340 | 344 | resize_col = _is_scalar(_res, nrow(ds))
|
341 | 345 | if resize_col
|
342 |
| - ds[!, newcol] = fill!(Tables.allocatecolumn(typeof(_res), nrow(ds)), _res) |
| 346 | + if ds isa SubDataset |
| 347 | + if haskey(index(ds), newcol) |
| 348 | + ds[:, newcol] = fill!(Tables.allocatecolumn(typeof(_res), nrow(ds)), _res) |
| 349 | + elseif !haskey(index(parent(ds)), newcol) |
| 350 | + parent(ds)[!, newcol] = missings(typeof(_res), nrow(parent(ds))) |
| 351 | + _update_subindex!(index(ds), index(parent(ds)), newcol) |
| 352 | + ds[:, newcol] = fill!(Tables.allocatecolumn(typeof(_res), nrow(ds)), _res) |
| 353 | + else |
| 354 | + throw(ArgumentError("modifing a parent's column which doesn't appear in SubDataset is not allowed")) |
| 355 | + end |
| 356 | + else |
| 357 | + ds[!, newcol] = fill!(Tables.allocatecolumn(typeof(_res), nrow(ds)), _res) |
| 358 | + end |
343 | 359 | else
|
344 |
| - ds[!, newcol] = _res |
| 360 | + if ds isa SubDataset |
| 361 | + if haskey(index(ds), mslast) |
| 362 | + ds[:, newcol] = _res |
| 363 | + elseif !haskey(index(parent(ds)), newcol) |
| 364 | + parent(ds)[!, newcol] = missings(eltype(_res), nrow(parent(ds))) |
| 365 | + _update_subindex!(index(ds), index(parent(ds)), newcol) |
| 366 | + ds[:, newcol] = _res |
| 367 | + else |
| 368 | + throw(ArgumentError("modifing a parent's column which doesn't appear in SubDataset is not allowed")) |
| 369 | + end |
| 370 | + else |
| 371 | + ds[!, newcol] = _res |
| 372 | + end |
345 | 373 | end
|
346 | 374 | end
|
347 | 375 |
|
@@ -377,10 +405,23 @@ function _modify_f_barrier(ds, msfirst, mssecond, mslast)
|
377 | 405 | end
|
378 | 406 | elseif (mssecond isa Expr) && mssecond.head == :BYROW
|
379 | 407 | try
|
380 |
| - ds[!, mslast] = byrow(ds, mssecond.args[1], msfirst; mssecond.args[2]...) |
| 408 | + if ds isa SubDataset |
| 409 | + _res = byrow(ds, mssecond.args[1], msfirst; mssecond.args[2]...) |
| 410 | + if haskey(index(ds), mslast) |
| 411 | + ds[:, mslast] = _res |
| 412 | + elseif !haskey(index(parent(ds)), mslast) |
| 413 | + parent(ds)[!, mslast] = missings(eltype(_res), nrow(parent(ds))) |
| 414 | + _update_subindex!(index(ds), index(parent(ds)), mslast) |
| 415 | + ds[:, mslast] = _res |
| 416 | + else |
| 417 | + throw(ArgumentError("modifing a parent's column which doesn't appear in SubDataset is not allowed")) |
| 418 | + end |
| 419 | + else |
| 420 | + ds[!, mslast] = byrow(ds, mssecond.args[1], msfirst; mssecond.args[2]...) |
| 421 | + end |
381 | 422 | catch e
|
382 | 423 | if e isa MethodError
|
383 |
| - throw(ArgumentError("output of `byrow` operation must be a vector")) |
| 424 | + throw(ArgumentError("There is problem in your `byrow`, make sure that the output of `byrow` is a vector")) |
384 | 425 | end
|
385 | 426 | rethrow(e)
|
386 | 427 | end
|
|
0 commit comments