imantha
imantha

Reputation: 3828

Julia dataframes method error is ambigious when filtering multiple conditions

I want to filter the data frame on multiple conditions and assign values if these conditions are true, I have written the following code, but I get the following error ERROR: MethodError: convert(::Type{Union{}}, ::String) is ambiguous.

#Make a new column c2 with missing values
df[:,:c2] .= missing

#Filter and assign values to column c2
df[(.!ismissing.(df.preconV) .& .!ismissing.(df.delayV) .& ismissing.(df.budgetV) .& .!ismissing.(df.improvDeclV)), :c2 ] .= "A1"

However, when I take out the .= "A1" part the data frame gets filtered properly. Its just when its assigning value the error pops in.

#Just filtering works fine!
df[(.!ismissing.(df.preconV) .& .!ismissing.(df.delayV) .& ismissing.(df.budgetV) .& .!ismissing.(df.improvDeclV)), :c2 ]
#out > 65-element Array{Missing, 1}

I am also aware that its possible to use ifelse command to achieve the same results, but in this case, it won't work, since this is just one line of code I have shown here, but I have many other lines of code, for other combinations (preconV, delayV...) and else statement will overwrite values from the previous statement. I have attached a pic of part of the dataframe to get an idea (only 4 columns are displayed since the dataframe is just too big)

Any ideas what's causing the ambiguous error?

enter image description here

Upvotes: 2

Views: 83

Answers (2)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69819

In my opinion it is preferable to write df.c2 = missings(String, nrow(df)). First, for me it seems to read more clearly. Second, what I propose does not copy as opposed to df[:, :c2] = ... (and in this case you do not need to make a copy).

Upvotes: 3

imantha
imantha

Reputation: 3828

Figured out the issue, It was a data type error.

need to change this line (df[:,:c2] .= missing) when creating a new column to df[:,:c2] = Array{String}(undef,nrow(df))

Upvotes: 0

Related Questions