-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataFrameExtensions.cs
More file actions
121 lines (106 loc) · 4.1 KB
/
DataFrameExtensions.cs
File metadata and controls
121 lines (106 loc) · 4.1 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
117
118
119
120
121
<<<<<<< HEAD
// ####################################################################################################################
// An open-source set of extensions to bulk-out the capabilities of the Dataframe in Microsoft.Data.Analysis.
// Copyright (c) 2024 Harrow Ventures Limited (HVL)
// Issued under the MIT License.
// The accompany copyright notice and licence shall be included in all copies or substantial portions of the software.
// ####################################################################################################################
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Data.Analysis;
=======
using System;
using System.Linq;
using System.Numerics;
using Microsoft.Data.Analysis;
namespace Dimension.DataFrame.Extensions;
/// <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 newColumn = new PrimitiveDataFrameColumn<T>(newName, Enumerable.Repeat(seed, (int) column.Length));
for (var i = 1; i < column.Length; i++)
{
var currentValue = typedColumn[i];
var previousValue = typedColumn[i - 1];
if (currentValue.HasValue && previousValue.HasValue)
{
newColumn[i] = currentValue.Value - previousValue.Value;
}
else
{
newColumn[i] = null;
}
}
return newColumn;
}
public static PrimitiveDataFrameColumn<T> Apply<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);
}
}
>>>>>>> 8c6160fa77adf5c233ef7ac5350a310532bd0c0d