如何处理C#中的deedle系列中的null(缺失)值? [英] How to deal with null (missing) values in a deedle series in C#?

查看:262
本文介绍了如何处理C#中的deedle系列中的null(缺失)值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该如何处理一系列序列中的缺失值?

How should I deal with missing values in a deedle series?

例如,我有一个包含字段 Name BirthDate ,其中 BirthDate 最初是 DateTime?,我需要转换 BirthDate String

For example, I have a series with fields Name and BirthDate, where BirthDate is initially DateTime? and I need to convert BirthDate to String.

var newDOB = df.GetColumn<DateTime?>("DOB").Select(x => x.Value.Value != null ? x.Value.Value.ToString("dd/MM/yyyy") : " ");
df.ReplaceColumn("DOB", newDOB);

这是我尝试的方法,它不起作用。
对我而言,什么是将丢失的 DateTime c值转换为 string 的最佳方法?
那么在C#中处理Deedle系列和Deedle数据框中的缺失值的最佳一般方法是什么?

This is what I tried and it does not work. What is the best way to convert a missing DateTime? value to string for me? And what is the best way in general to deal with missing values in Deedle series and Deedle dataframes in C#?

推荐答案

在创建Deedle系列时,Deedle会检测无效值并将其自动视为缺失-因此,在创建系列时使用 NaN null ,这些值会自动转换为缺失值(这也适用于可为null的变量)。

When you are creating a Deedle series, Deedle detects invalid values and treats them as missing automatically - so when you create a series with NaN or null, those are automatically turned into missing values (and this also works for nullables).

此外, Select 方法会跳过所有缺少的值。例如,考虑以下系列:

Furthermore, the Select method skips over all missing values. For example, consider this series:

Series<int, DateTime?> ds = Enumerable.Range(0, 100).Select(i => 
  new KeyValuePair<int, DateTime?>(i, i%5==0 ? (DateTime?)null : DateTime.Now.AddHours(i))
 ).ToSeries();
ds.Print();

在这里,Deedle认识到每五分之一的值都丢失了。当您调用 Select 时,它仅将操作应用于有效值,而每五个值仍保留为缺失值:

Here, Deedle recognizes that every fifth value is missing. When you call Select, it applies the operation only to valid values and every fifth value remains as a missing value:

  ds.Select(kvp => kvp.Value.Value.ToString("D")).Print();

如果要对缺失的值进行处理,可以使用 FillMissing (以指定的字符串填充它们或从系列中的上一个项目复制值)或 DropMissing 从系列中丢弃它们。您还可以使用 SelectOptional ,该函数通过 OptionalValue< V> 调用函数,因此可以实现自己的自定义逻辑缺失值。

If you want to do something with the missing values, you could use FillMissing (to fill them with a specified string or to copy the value from previous item in the series) or DropMissing to discard them from the series. You can also use SelectOptional that calls your function with OptionalValue<V> and so you can implement your own custom logic for missing values.

这也意味着,如果您拥有 Series< K,DateTime?> ,它确实不是很有用,因为 null 值均由Deedle处理-因此,您可以使用以下命令将其转换为 Series< K,DateTime> Select(kvp => kvp.Value.Value),让Deedle为您处理丢失的值。

This also means that if you have Series<K, DateTime?>, it is really not very useful, because the null values are all handled by Deedle - so you can turn it into Series<K, DateTime> using Select(kvp => kvp.Value.Value) and let Deedle handle missing values for you.

这篇关于如何处理C#中的deedle系列中的null(缺失)值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆