|
168 | 168 | @test df1b == df1 |
169 | 169 | end |
170 | 170 |
|
| 171 | + # Zero column case |
| 172 | + @test isempty(dropmissing(DataFrame())) && dropmissing(DataFrame()) isa DataFrame |
| 173 | + @test isempty(dropmissing!(DataFrame())) && dropmissing!(DataFrame()) isa DataFrame |
| 174 | + df = DataFrame(a=1:3, b=4:6) |
| 175 | + dfv = @view df[:, 2:1] |
| 176 | + # TODO: re-enable after https://github.com/JuliaData/DataFrames.jl/issues/3272 is resolved |
| 177 | + # @test isempty(dropmissing(dfv)) && dropmissing(dfv) isa DataFrame |
| 178 | + @test_throws ArgumentError dropmissing!(dfv) |
| 179 | + @test_throws ArgumentError dropmissing(df1, []) |
| 180 | + @test_throws ArgumentError dropmissing!(df1, []) |
| 181 | + |
171 | 182 | df = DataFrame(a=[1, missing, 3]) |
172 | 183 | sdf = view(df, :, :) |
173 | 184 | @test dropmissing(sdf) == DataFrame(a=[1, 3]) |
|
186 | 197 | @test eltype(df2.a) == Union{Int, Missing} |
187 | 198 | @test df.a == df2.a == [1, 3] |
188 | 199 |
|
| 200 | + # view=true |
| 201 | + df = DataFrame(a=[1, missing, 3]) |
| 202 | + @test dropmissing(df, view=false) == DataFrame(a=[1, 3]) |
| 203 | + @test dropmissing(df, view=true) == view(df, [1, 3], :) |
| 204 | + @test typeof(dropmissing(df, view=true)) <: SubDataFrame |
| 205 | + @test eltype(dropmissing(df, view=true, disallowmissing=false).a) == Union{Int, Missing} |
| 206 | + @test_throws ArgumentError dropmissing(df, view=true, disallowmissing=true) |
| 207 | + @test eltype(dropmissing(df, view=false, disallowmissing=false).a) == Union{Int, Missing} |
| 208 | + @test eltype(dropmissing(df, view=false, disallowmissing=true).a) == Int |
| 209 | + |
189 | 210 | a = [1, 2] |
190 | 211 | df = DataFrame(a=a, copycols=false) |
191 | 212 | @test dropmissing!(df) === df |
|
200 | 221 | df = DataFrame(b=b) |
201 | 222 | @test eltype(dropmissing(df).b) == Int |
202 | 223 | @test eltype(dropmissing!(df).b) == Int |
| 224 | + |
| 225 | + # disallowmissing argument |
| 226 | + a = Union{Int, Missing}[3, 4] |
| 227 | + b = Union{Int, Missing}[1, 2] |
| 228 | + df = DataFrame(;a,b) |
| 229 | + @test eltype(dropmissing(df, disallowmissing=false).a) == Union{Int, Missing} |
| 230 | + @test eltype(dropmissing!(copy(df), disallowmissing=false).a) == Union{Int, Missing} |
| 231 | + @test eltype(dropmissing(df, disallowmissing=true).a) == Int |
| 232 | + @test eltype(dropmissing!(copy(df), disallowmissing=true).a) == Int |
| 233 | + @test eltype(dropmissing(df, :a, disallowmissing=true).a) == Int |
| 234 | + @test eltype(dropmissing!(copy(df), :a, disallowmissing=true).a) == Int |
| 235 | + @test eltype(dropmissing(df, :b, disallowmissing=true).a) == Union{Int, Missing} |
| 236 | + @test eltype(dropmissing!(copy(df), :b, disallowmissing=true).a) == Union{Int, Missing} |
| 237 | + |
| 238 | + # CategoricalArrays |
| 239 | + c = categorical([1, 2, 1, missing]) |
| 240 | + df = DataFrame(c=c) |
| 241 | + @test dropmissing(df) == DataFrame(c=categorical([1, 2, 1])) |
| 242 | + @test eltype(dropmissing(df).c) == CategoricalValue{Int, UInt32} |
| 243 | + @test eltype(dropmissing!(df).c) == CategoricalValue{Int, UInt32} |
| 244 | + |
| 245 | + # Multithreaded execution test (must be at least ncol > 1, nrow > 100_000) |
| 246 | + N_rows, N_cols = 110_000, 3 |
| 247 | + df = DataFrame([rand(N_rows) for i in 1:N_cols], :auto) |> allowmissing |
| 248 | + # Deterministic drop mask: IF remainder of index position divided by 10 == column index THEN missing |
| 249 | + for i in 1:ncol(df) |
| 250 | + missing_mask = (eachindex(df[!, i]) .% 10) .== i |
| 251 | + df[missing_mask, i] .= missing |
| 252 | + end |
| 253 | + |
| 254 | + notmissing_rows = [i for i in 1:N_rows if i % 10 == 0 || i % 10 > ncol(df)] |
| 255 | + @test dropmissing(df) ≅ df[notmissing_rows, :] |
| 256 | + |
| 257 | + cols = [:x1, :x2] |
| 258 | + notmissing_rows = [i for i in 1:N_rows if i % 10 == 0 || i % 10 > length(cols)] |
| 259 | + returned = dropmissing(df, cols) |
| 260 | + @test returned ≅ df[notmissing_rows, :] |
| 261 | + @test eltype(returned[:, cols[1]]) == nonmissingtype(eltype(df[:, cols[1]])) |
| 262 | + @test eltype(returned[:, cols[2]]) == nonmissingtype(eltype(df[:, cols[2]])) |
| 263 | + @test eltype(returned[:, ncol(df)]) == eltype(df[:, ncol(df)]) |
203 | 264 | end |
204 | 265 |
|
205 | 266 | @testset "deleteat! https://github.com/JuliaLang/julia/pull/41646 bug workaround" begin |
|
0 commit comments