Skip to content

Commit

Permalink
do not pass empty vector to Tables.columntable (#3435)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins authored Apr 10, 2024
1 parent 3b4fcd8 commit 8c1d98e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
* Ensure that `allunique(::AbstractDataFrame, ::Any)` always gets
interpreted as test for uniqueness of rows in the first positional argument
([#3434](https://github.com/JuliaData/DataFrames.jl/issues/3434))
* Make sure that an empty vector of `Any` or of `AbstractVector` is treated as having
no columns when a data frame is being processed with `combine`/`select`/`transform`.
([#3435](https://github.com/JuliaData/DataFrames.jl/issues/3435))

# DataFrames.jl v1.6.1 Release Notes

Expand Down
7 changes: 6 additions & 1 deletion src/abstractdataframe/selection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,12 @@ function select_transform!((nc,)::Ref{Any}, df::AbstractDataFrame, newdf::DataFr
res = newres
elseif !(res isa Union{AbstractDataFrame, NamedTuple, DataFrameRow, AbstractMatrix,
Tables.AbstractRow})
res = Tables.columntable(res)
if res isa Union{AbstractVector{Any}, AbstractVector{<:AbstractVector}}
@assert isempty(res)
res = DataFrame()
else
res = Tables.columntable(res)
end
end
end

Expand Down
18 changes: 18 additions & 0 deletions test/select.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3024,4 +3024,22 @@ end
@test_throws ArgumentError combine(gdf, :x => (x -> x[1] == 2 ? "x" : cr) => AsTable)
end

@testset "empty vector" begin
df = DataFrame(a=1:3)

@test_throws ArgumentError select(df, :a => (x -> Vector{Any}[]))

for T in (Vector{Any}, Any, NamedTuple{(:x,),Tuple{Int64}})
v = combine(df, :a => (x -> T[])).a_function
@test isempty(v)
@test eltype(v) === T
end

@test size(combine(df, :a => (x -> Vector{Any}[]) => AsTable)) == (0, 0)
@test size(combine(df, :a => (x -> Any[]) => AsTable)) == (0, 0)
df2 = combine(df, :a => (x -> NamedTuple{(:x,),Tuple{Int64}}[]) => AsTable)
@test size(df2) == (0, 1)
@test eltype(df2.x) === Int
end

end # module

0 comments on commit 8c1d98e

Please sign in to comment.