Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I insert missing values to my dataframe in Julia

df3[10, :A] = missing
df3[15, :B] = missing
df3[15, :C] = missing

Even NA is not working.

I am getting an error

MethodError: Cannot convert an object of type Missings.Missing to an object of type Int64 This may have arisen from a call to the constructor Int64(...), since type constructors fall back to convert methods. Stacktrace: [1] setindex!(::Array{Int64,1}, ::Missings.Missing, ::Int64) at ./array.jl:583 [2] insert_single_entry!(::DataFrames.DataFrame, ::Missings.Missing, ::Int64, ::Symbol) at /home/jrun/.julia/v0.6/DataFrames/src/dataframe/dataframe.jl:361 [3] setindex!(::DataFrames.DataFrame, ::Missings.Missing, ::Int64, ::Symbol) at /home/jrun/.julia/v0.6/DataFrames/src/dataframe/dataframe.jl:448 [4] include_string(::String, ::String) at ./loading.jl:522

like image 862
Himanshu Poddar Avatar asked Oct 09 '18 08:10

Himanshu Poddar


People also ask

How do you drop missing values in Julia?

That in mind, how can we use conditionals in order to detect our missing values? In Julia, we actually use methods in order to do this. The two methods are isnan() and ismissing(). In most cases, you might not have to drop down to the level of using conditionals to process your data.

What is missing in Julia?

Julia provides support for representing missing values in the statistical sense, that is for situations where no value is available for a variable in an observation, but a valid value theoretically exists. Missing values are represented via the missing object, which is the singleton instance of the type Missing .

How do I add a row to a DataFrame Julia?

Create an empty Julia DataFrame by enclosing column names and datatype of column inside DataFrame() function. Now you can add rows one by one using push!() function. This is like row binding.

How do you use DataFrame in Julia?

You can simply create a data frame using the DataFrame() function. You can mention the columns and their values in between the brackets of the DataFrame() function as the argument and run it as shown below. Before this you have to tell Julia that you are going to use data frames by using the command 'using DataFrames'.


1 Answers

Use allowmissing! function.

julia> using DataFrames

julia> df = DataFrame(a=[1,2,3])
3×1 DataFrame
│ Row │ a     │
│     │ Int64 │
├─────┼───────┤
│ 1   │ 1     │
│ 2   │ 2     │
│ 3   │ 3     │

julia> df.a[1] = missing
ERROR: MethodError: Cannot `convert` an object of type Missing to an object of type Int64

julia> allowmissing!(df)
3×1 DataFrame
│ Row │ a      │
│     │ Int64⍰ │
├─────┼────────┤
│ 1   │ 1      │
│ 2   │ 2      │
│ 3   │ 3      │

julia> df.a[1] = missing
missing

julia> df
3×1 DataFrame
│ Row │ a       │
│     │ Int64⍰  │
├─────┼─────────┤
│ 1   │ missing │
│ 2   │ 2       │
│ 3   │ 3       │

You can see which columns in a DataFrame allow missing because they are highlighted with after type name under column name.

You can also use allowmissing function to create a new DataFrame.

Both functions optionally accept columns that are to be converted.

Finally there is a disallowmissing/disallowmissing! pair that does the reverse (i.e. strips optional Missing union from eltype if a vector actually contains no missing values).

like image 60
Bogumił Kamiński Avatar answered Oct 27 '22 20:10

Bogumił Kamiński