ajkl
ajkl

Reputation: 1036

Date formatting a data frame string column

Is there a way to do something like this (this is in R)

df$dataCol <- as.Date(df$dataCol, format="%Y%m%d")

where the dataCol is of the format "20151009".

  1. Is there a way to change the column type to date in julia ?
  2. I didnt find a way to do this with the Date.jl package.

Upvotes: 5

Views: 895

Answers (2)

ajkl
ajkl

Reputation: 1036

Is this the best way to do the first part of my question

using Dates
dateReported = map((x) -> string(x), df[:DateReported])
df[:DateOccurred] = map((x) -> if match(r"^((19|20)\d\d)(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])", x)!=nothing Date(x, DateFormat("yyyymmdd")) end, dateOccurred)

Upvotes: 0

Vincent Zoonekynd
Vincent Zoonekynd

Reputation: 32401

There is a Date constructor with an argument for the format, but the syntax is slightly different.

using Dates
Date( "20141123", DateFormat("yyyymmdd") )

Upvotes: 3

Related Questions