/// <summary> /// Saves data frame .NET object in a csv file. /// </summary> /// <param name="filePath">Full or relative file path.</param> /// <param name="dataFrame">Data frame to persist into file.</param> /// <returns>True if save successfully passed</returns> public static bool ToCsv(string filePath, DataFrame dataFrame, string dFormat) { if (dataFrame == null) { throw new ArgumentNullException(nameof(dataFrame)); } using (var strWr = File.CreateText(filePath)) { var csvWriter = new CsvWriter(strWr); //write header writeHeader(csvWriter, dataFrame.Columns); csvWriter.NextRecord(); //write values int lstIndex = 0; for (int i = 0; i < dataFrame.RowCount(); i++) { for (int j = 0; j < dataFrame.ColCount(); j++) { if (dataFrame._values[lstIndex] == DataFrame.NAN) { csvWriter.WriteField(""); lstIndex++; continue; } switch (dataFrame.ColTypes[j]) { case ColType.I2: var bv = Convert.ToBoolean(dataFrame._values[lstIndex]); csvWriter.WriteField(bv.ToString(CultureInfo.InvariantCulture)); break; case ColType.IN: csvWriter.WriteField(dataFrame._values[lstIndex].ToString()); break; case ColType.I32: var iv = Convert.ToInt32(dataFrame._values[lstIndex]); csvWriter.WriteField(iv.ToString(CultureInfo.InvariantCulture)); break; case ColType.I64: var lv = Convert.ToInt64(dataFrame._values[lstIndex]); csvWriter.WriteField(lv.ToString(CultureInfo.InvariantCulture)); break; case ColType.F32: var df = Convert.ToSingle(dataFrame._values[lstIndex]); csvWriter.WriteField(df.ToString(CultureInfo.InvariantCulture)); break; case ColType.DD: var dv = Convert.ToDouble(dataFrame._values[lstIndex]); csvWriter.WriteField(dv.ToString(CultureInfo.InvariantCulture)); break; case ColType.STR: csvWriter.WriteField(dataFrame._values[lstIndex].ToString()); break; case ColType.DT: var dt = Convert.ToDateTime(dataFrame._values[lstIndex]); if (dFormat != null) { csvWriter.WriteField(dt.ToString(dFormat)); } else { csvWriter.WriteField(dt.ToString()); } break; } // lstIndex++; } csvWriter.NextRecord(); } } return(true); }