-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataFrameExtensions.cs
More file actions
116 lines (102 loc) · 3.85 KB
/
Copy pathDataFrameExtensions.cs
File metadata and controls
116 lines (102 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Linq;
using System.Numerics;
using Microsoft.Data.Analysis;
namespace Dimension.DataFrameExtensions;
/// <summary>
/// Methods for adding calculations extension methods to make Microsoft's DataFrame a little more user-friendly.
/// </summary>
public static class DataFrameExtensionsCalculations
{
public static PrimitiveDataFrameColumn<T>? Diff<T>(this DataFrameColumn? column, string name = "", T? seed = default)
where T : unmanaged, INumber<T>
{
if (column is null)
{
return null;
}
// Cast to typed column
if (column is not PrimitiveDataFrameColumn<T> typedColumn)
{
throw new ArgumentException($"Column must be of type PrimitiveDataFrameColumn<{typeof(T).Name}>", nameof(column));
}
var newName = string.IsNullOrEmpty(name) ? column.Name + "_Diff" : name;
var (values, isValid) = DataFrameBulk.Read(typedColumn);
var n = values.Length;
var outValues = new T[n];
var outValid = new bool[n];
if (n > 0)
{
// Row 0 carries the seed (which may itself be null).
outValues[0] = seed.GetValueOrDefault();
outValid[0] = seed.HasValue;
}
for (var i = 1; i < n; i++)
{
if (isValid[i] && isValid[i - 1])
{
outValues[i] = values[i] - values[i - 1];
outValid[i] = true;
}
}
return DataFrameBulk.Create(newName, outValues, outValid);
}
/// <summary>
/// Applies operation to every non-null value, preserving nulls, and renaming the result column.
/// Named NullableApply, not Apply, because PrimitiveDataFrameColumn<T> already declares an instance
/// Apply(Func<T,T>, bool isInPlace = true) that permanently shadows an extension method of that name.
/// </summary>
public static PrimitiveDataFrameColumn<T> NullableApply<T>(this PrimitiveDataFrameColumn<T> column, Func<T, T> operation, string name = "")
where T : unmanaged, INumber<T>
{
if (operation is null)
{
throw new ArgumentNullException(nameof(operation));
}
if (string.IsNullOrEmpty(name))
{
name = column.Name + "_Applied";
}
var newColumn = new PrimitiveDataFrameColumn<T>(name, column.Length);
for (var i = 0; i < column.Length; i++)
{
var rawValue = column[i];
if (rawValue != null)
{
var castedValue = (T) rawValue;
newColumn[i] = operation(castedValue);
}
else
{
newColumn[i] = null;
}
}
return newColumn;
}
public static PrimitiveDataFrameColumn<T> Pow<T>(this PrimitiveDataFrameColumn<T> column, double power, string name = "")
where T : unmanaged, INumber<T>
{
var result = new T[column.Length];
for (var i = 0; i < column.Length; i++)
{
var value = column[i];
if (value.HasValue)
{
// Convert to double, apply power, and then try to convert back to T.
// This approach has limitations and might not work for all INumber<T> types, especially those that cannot be accurately represented as double.
var poweredValue = Math.Pow(Convert.ToDouble(value.GetValueOrDefault()), power);
result[i] = T.CreateChecked(poweredValue);
}
else
{
// Use default(T) to represent missing values, as NaN is not universally applicable.
result[i] = default;
}
}
if (string.IsNullOrEmpty(name))
{
name = $"{column.Name}_Pow{power}";
}
return new PrimitiveDataFrameColumn<T>(name, result);
}
}