Vector of missing and float

Hello,

I dropped missing values from a dataframe and then turned a column into a vector. The vector does not have missing values, but it has type:

7554-element Vector{Union{Missing, Float64}}:

This prevents me from using the autocov function. Does anyone know how to convert it to just a vector of only Float64? Filtering missing values again does not drop any observations and it still remains the wrong type:

inv_nomiss = filter(!ismissing,inv)

Maybe

inv_nomiss = convert(Vector{Float64}, collect(skipmissing(inv)))

?

EDIT: Probably cleaner for you: Once you have inv_nomiss, you can just do

convert(Vector{Float64}, inv_nomiss)

so that you aren’t skipping missings in a vector that doesn’t have any.

1 Like

I think you want disallowmissing or its in-place version disallowmissing!.

julia> using DataFrames

help?> disallowmissing!
search: disallowmissing! disallowmissing

  disallowmissing!(df::DataFrame, cols=:; error::Bool=true)

  Convert columns cols of data frame df from element type Union{T, Missing} to T to drop support
  for missing values.

  cols can be any column selector (Symbol, string or integer; :, Cols, All, Between, Not, a
  regular expression, or a vector of Symbols, strings or integers).

  If cols is omitted all columns in the data frame are converted.

  If error=false then columns containing a missing value will be skipped instead of throwing an
  error.

  Metadata: this function preserves table-level and column-level :note-style metadata.

2 Likes

No libraries needed, you can rely on Julia narrowing type on broadcasting:

julia> A = Union{Int,Missing}[1, 2]
2-element Vector{Union{Missing, Int64}}:
 1
 2

julia> identity.(A)
2-element Vector{Int64}:
 1
 2
3 Likes

or

Float64.(v)

to be more explicit about the desired type

2 Likes

May I also suggest looking into coalesce() if you happen have a value you can assign to missing?

1 Like

also

float(v)

The end result is the same as some of the ideas above, but I feel like this one does a decent job of communicating (to humans) your intent—to set a new container type rather than operating on elements:

Float64[inv_nomiss;]
1 Like

There is no narrowing, the output remains unchanged: Vector{Union{Missing, Float64}}

1 Like

Just as you say. I hadn’t done a meaningful test

julia> v=[1,2,missing]
3-element Vector{Union{Missing, Int64}}:
 1
 2
  missing

julia> float(v)
3-element Vector{Union{Missing, Float64}}:
 1.0
 2.0
  missing

julia> filter!(!ismissing,v)
2-element Vector{Union{Missing, Int64}}:
 1
 2

julia> v
2-element Vector{Union{Missing, Int64}}:
 1
 2

julia> float(v)
2-element Vector{Union{Missing, Float64}}:
 1.0
 2.0

I had trusted the first meaning that comes to mind when reading the definition of the function

 Convert a number or array to a floating point data type.


collect(skipmissing(x)) is probably your best bet.

1 Like