Search code examples
c#deedle

Deedle frame save to csv file in millisecond


I am trying to save a Deedle frame which contains a DateTime column to csv file. But in CSV file, the DateTime column does not contain the milisecond. I would like to save it with milisecond in format [yyyy-mm-dd hh:mm:ss.000]. I tried converting DateTime to string before saving. It worked correctly but the performance is low. Is there any way to do it without converting to string?


Solution

  • You can indeed accomplish this without needing to manually convert your dates.

    The signature of the SaveCsv method allows you to specify as the last parameter a CultureInfo object. Internally, the Deedle code figures out if it's looking at a DateTime object when writing out to file, and if so, writes out that date by calling:

    dt.ToString(cultureInfo)
    

    where cultureInfo is either the object you pass in, or a default of CultureInfo.InvariantCulture.

    All that is to say you can specify the date formatting by passing in a CultureInfo object with modified formatting. It's pretty straightforward:

    CultureInfo culture = new CultureInfo("en-US");
    var dateformat = new DateTimeFormatInfo();  
    dateformat.ShortDatePattern = "yyyy-mm-dd";
    dateformat.LongTimePattern = "hh:mm:ss.fff";
    culture.DateTimeFormat = dateformat;
    
    myFrame.SaveCsv("SomePath.csv", new string[] { "rowKeyColumnName"}, default(char), culture);
    

    Why I am specifically setting LongTimePattern and ShortDatePattern (as opposed to one of the several other pattern properties)? If you look at the doc for DateTime.ToString(IFormatProvider), you'll see the following:

    The value of the current DateTime object is formatted using the general date and time format specifier ('G'), which formats output using the short date pattern and the long time pattern.