How to manage various date format in a csv file

Hi @steph_de_paris. Sorry for the late reply.

The cause of your problem

  • CSV.write() does not support dates being written with different formats in different columns.

Solution A

  • Write your own custom write_custom_csv(...) function.
  • You can use the Dates.format(::IO, some_date_object, FORMATSTR) function

Should be easy enough, just a bit annoying when you have to support multiple different table formats.

Solution B

Julia makes it possible to create wrapper types to get them to behave differently than the original.

In your case, you would want these types to use different show(::IO, ::MyWrapType) functions. Note that, in a similar fashion, you can create a more convenient constructor that applies the same date format used by show():

import DataFrames: DataFrame
import CSV
using Dates


#==Define date wrapper (parameterized type)
===================================================================#

#Wrapper/tagging system to create a specialized date format:
struct DateFormatted{FMT} #FMT should be a symbol like `:YMD`
	d::DateTime #Acutal data/date/etc
end
#Convenient string-based constructor:
DateFormatted{FMT}(datestr::AbstractString) where FMT =
	DateFormatted{FMT}(DateTime(datestr, DateFormat(DateFormatted{FMT}))) #Use DateFormat()... as specified below

#By default, dates are shown formatted by "user-specified" DateFormat() function
Base.show(io::IO, df::T) where T<:DateFormatted =
	Dates.format(io, df.d, Dates.DateFormat(T))


#==Define your CUSTOM date wrapper types:
===================================================================#
#Create convenient aliases (if you prefer):
DateAMJ = DateFormatted{:AMJ}
DateAMJHMS = DateFormatted{:AMJHMS}

#Overload `DateFormat` for your new wrapper type (to construct/show() your date types):
#(Convenient way for `show()` to figure out how to show)
Dates.DateFormat(::Type{DateAMJ}) = dateformat"yyyymmdd"
Dates.DateFormat(::Type{DateAMJHMS}) = dateformat"yyyymmddHHMMSS"


#==Use your new wrapped date types:
===================================================================#
@show d1 = DateAMJ("20190326")
@show DateAMJHMS("20190402161901")


#==Create your dataframe again:
===================================================================#
#Define your arrays externally for readability purposes:
hre_deb_calc = [DateAMJHMS("20190402161902")]
jour_prevu   = [DateAMJ("20190326")]

#Use `;` to avoid needing `keyname1=keyname1, keyname2=keyname2, ...`:
df_out = DataFrame(; hre_deb_calc, jour_prevu)
CSV.write("resultat.csv", df_out, delim=";")

PS

There are, of course, other solutions possible with Julia types. Some might even be more elegant than what I suggested.

3 Likes