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

问题描述

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

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

例如,我有一个包含字段 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(缺失)值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 12:54