Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting DataFrame String column containing missing values to Date in Julia

I'm trying to convert a DataFrame String column to Date format in Julia, but if the column contains missing values an error is produced:

ERROR: MethodError: no method matching Int64(::Missing)

The code I've tried to run (which works for columns with no missing data) is:

df_pp[:tod] = Date.(df_pp[:tod], DateFormat("d/m/y"));

Other lines of code I have tried are:

df_pp[:tod] = Date.(passmissing(df_pp[:tod]), DateFormat("d/m/y"));
df_pp[.!ismissing.(df_pp[:tod]), :tod] = Date.(df_pp[:tod], DateFormat("d/m/y"));

The code relates to a column named tod in a data frame named df_pp. Both the DataFrames & Dates packages have been loaded prior to attempting this.

like image 827
Feakster Avatar asked Oct 16 '22 14:10

Feakster


1 Answers

The passmissing way is

df_pp.tod = passmissing(x->Date(x, DateFormat("d/m/y"))).(df_pp.tod)

What happens here is this: passmissing takes a function, and returns a new function that handles missings (by returning missing). Inside the bracket, in x->Date(x, DateFormat("d/m/y")) I define a new, anonymous function, that calls the Date function with the appropriate DateFormat. Finally, I use the function returned by passmissing immediately on df_pp.tod, using a . to broadcast along the column. It's easier to see the syntax if I split it up:

myDate(x) = Date(x, DateFormat("d/m/y"))
Date_accepting_missing = passmissing(myDate)
df_pp[:tod] = Date_accepting_missing.(df_pp[:tod])
like image 119
Michael K. Borregaard Avatar answered Oct 21 '22 03:10

Michael K. Borregaard