在毫秒内将 Deedle 帧保存到 csv 文件
Deedle frame save to csv file in millisecond
我正在尝试将包含 DateTime 列的 Deedle 帧保存到 csv 文件。但在 CSV 文件中,DateTime 列不包含毫秒。我想以毫秒格式保存它 [yyyy-mm-dd hh:mm:ss.000]。我尝试在保存之前将 DateTime 转换为字符串。它工作正常,但性能很低。
有什么方法可以不转换为字符串吗?
您确实可以做到这一点,而无需手动转换您的日期。
SaveCsv
方法的签名允许您将 CultureInfo
对象指定为最后一个参数。在内部,Deedle 代码在写入文件时确定它是否正在查看 DateTime
对象,如果是,则通过调用写出该日期:
dt.ToString(cultureInfo)
其中 cultureInfo
是您传入的对象,或者默认值 CultureInfo.InvariantCulture
。
也就是说,您可以通过传入具有修改格式的 CultureInfo
对象来指定日期格式。非常简单:
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);
为什么我要专门设置 LongTimePattern
和 ShortDatePattern
(与其他几个模式属性之一相对)?如果您查看 DateTime.ToString(IFormatProvider)
的文档,您将看到以下内容:
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.
我正在尝试将包含 DateTime 列的 Deedle 帧保存到 csv 文件。但在 CSV 文件中,DateTime 列不包含毫秒。我想以毫秒格式保存它 [yyyy-mm-dd hh:mm:ss.000]。我尝试在保存之前将 DateTime 转换为字符串。它工作正常,但性能很低。 有什么方法可以不转换为字符串吗?
您确实可以做到这一点,而无需手动转换您的日期。
SaveCsv
方法的签名允许您将 CultureInfo
对象指定为最后一个参数。在内部,Deedle 代码在写入文件时确定它是否正在查看 DateTime
对象,如果是,则通过调用写出该日期:
dt.ToString(cultureInfo)
其中 cultureInfo
是您传入的对象,或者默认值 CultureInfo.InvariantCulture
。
也就是说,您可以通过传入具有修改格式的 CultureInfo
对象来指定日期格式。非常简单:
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);
为什么我要专门设置 LongTimePattern
和 ShortDatePattern
(与其他几个模式属性之一相对)?如果您查看 DateTime.ToString(IFormatProvider)
的文档,您将看到以下内容:
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.