forked from dotnet/machinelearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataFrameColumnCollection.cs
More file actions
496 lines (435 loc) · 21.2 KB
/
DataFrameColumnCollection.cs
File metadata and controls
496 lines (435 loc) · 21.2 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Microsoft.Data.Analysis
{
/// <summary>
/// A DataFrameColumnCollection is just a container that holds a number of DataFrameColumn instances.
/// </summary>
public class DataFrameColumnCollection : Collection<DataFrameColumn>
{
private readonly Action ColumnsChanged;
private readonly Dictionary<string, int> _columnNameToIndexDictionary = new Dictionary<string, int>(StringComparer.Ordinal);
internal long RowCount { get; set; }
internal DataFrameColumnCollection(IEnumerable<DataFrameColumn> columns, Action columnsChanged) : base()
{
columns = columns ?? throw new ArgumentNullException(nameof(columns));
ColumnsChanged = columnsChanged;
foreach (DataFrameColumn column in columns)
{
Add(column);
}
}
internal IReadOnlyList<string> GetColumnNames()
{
var ret = new List<string>(Count);
for (int i = 0; i < Count; i++)
{
ret.Add(this[i].Name);
}
return ret;
}
public void RenameColumn(string currentName, string newName)
{
var column = this[currentName];
column.SetName(newName);
}
[Obsolete]
public void SetColumnName(DataFrameColumn column, string newName)
{
column.SetName(newName);
}
//Updates column's metadata (is used as a callback from Column class)
internal void UpdateColumnNameMetadata(DataFrameColumn column, string newName)
{
string currentName = column.Name;
int currentIndex = _columnNameToIndexDictionary[currentName];
_columnNameToIndexDictionary.Remove(currentName);
_columnNameToIndexDictionary.Add(newName, currentIndex);
ColumnsChanged?.Invoke();
}
public void Insert<T>(int columnIndex, IEnumerable<T> column, string columnName)
where T : unmanaged
{
DataFrameColumn newColumn = new PrimitiveDataFrameColumn<T>(columnName, column);
Insert(columnIndex, newColumn); // calls InsertItem internally
}
protected override void InsertItem(int columnIndex, DataFrameColumn column)
{
column = column ?? throw new ArgumentNullException(nameof(column));
if (Count == 0)
{
//change RowCount on inserting first row to dataframe
RowCount = column.Length;
}
else if (column.Length != RowCount)
{
//check all columns in the dataframe have the same lenght (amount of rows)
throw new ArgumentException(Strings.MismatchedColumnLengths, nameof(column));
}
if (_columnNameToIndexDictionary.ContainsKey(column.Name))
{
throw new ArgumentException(string.Format(Strings.DuplicateColumnName, column.Name), nameof(column));
}
column.AddOwner(this);
_columnNameToIndexDictionary[column.Name] = columnIndex;
for (int i = columnIndex + 1; i < Count; i++)
{
_columnNameToIndexDictionary[this[i].Name]++;
}
base.InsertItem(columnIndex, column);
ColumnsChanged?.Invoke();
}
protected override void SetItem(int columnIndex, DataFrameColumn column)
{
column = column ?? throw new ArgumentNullException(nameof(column));
if (RowCount > 0 && column.Length != RowCount)
{
throw new ArgumentException(Strings.MismatchedColumnLengths, nameof(column));
}
bool existingColumn = _columnNameToIndexDictionary.TryGetValue(column.Name, out int existingColumnIndex);
if (existingColumn && existingColumnIndex != columnIndex)
{
throw new ArgumentException(string.Format(Strings.DuplicateColumnName, column.Name), nameof(column));
}
_columnNameToIndexDictionary.Remove(this[columnIndex].Name);
_columnNameToIndexDictionary[column.Name] = columnIndex;
this[columnIndex].RemoveOwner(this);
base.SetItem(columnIndex, column);
ColumnsChanged?.Invoke();
}
protected override void RemoveItem(int columnIndex)
{
_columnNameToIndexDictionary.Remove(this[columnIndex].Name);
for (int i = columnIndex + 1; i < Count; i++)
{
_columnNameToIndexDictionary[this[i].Name]--;
}
this[columnIndex].RemoveOwner(this);
base.RemoveItem(columnIndex);
//Reset RowCount if the last column was removed and dataframe is empty
if (Count == 0)
RowCount = 0;
ColumnsChanged?.Invoke();
}
public void Remove(string columnName)
{
int columnIndex = IndexOf(columnName);
if (columnIndex != -1)
{
RemoveAt(columnIndex); // calls RemoveItem internally
}
}
/// <summary>
/// Searches for a <see cref="DataFrameColumn"/> with the specified <paramref name="columnName"/> and returns the zero-based index of the first occurrence if found. Returns -1 otherwise
/// </summary>
/// <param name="columnName"></param>
public int IndexOf(string columnName)
{
if (columnName != null && _columnNameToIndexDictionary.TryGetValue(columnName, out int columnIndex))
{
return columnIndex;
}
return -1;
}
protected override void ClearItems()
{
base.ClearItems();
ColumnsChanged?.Invoke();
_columnNameToIndexDictionary.Clear();
//reset RowCount as DataFrame is now empty
RowCount = 0;
}
/// <summary>
/// An indexer based on <see cref="DataFrameColumn.Name"/>
/// </summary>
/// <param name="columnName">The name of a <see cref="DataFrameColumn"/></param>
/// <returns>A <see cref="DataFrameColumn"/> if it exists.</returns>
/// <exception cref="ArgumentException">Throws if <paramref name="columnName"/> is not present in this <see cref="DataFrame"/></exception>
public DataFrameColumn this[string columnName]
{
get
{
int columnIndex = IndexOf(columnName);
if (columnIndex == -1)
{
throw new ArgumentException(String.Format(Strings.InvalidColumnName, columnName), nameof(columnName));
}
return this[columnIndex];
}
set
{
int columnIndex = IndexOf(columnName);
DataFrameColumn newColumn = value;
newColumn.SetName(columnName);
if (columnIndex == -1)
{
Insert(Count, newColumn);
}
else
{
this[columnIndex] = newColumn;
}
}
}
/// <summary>
/// Gets the <see cref="PrimitiveDataFrameColumn{T}"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="PrimitiveDataFrameColumn{T}"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public PrimitiveDataFrameColumn<T> GetPrimitiveColumn<T>(string name)
where T : unmanaged
{
DataFrameColumn column = this[name];
if (column is PrimitiveDataFrameColumn<T> ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(T)), nameof(T));
}
/// <summary>
/// Gets the <see cref="PrimitiveDataFrameColumn{T}"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="PrimitiveDataFrameColumn{T}"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public PrimitiveDataFrameColumn<DateTime> GetDateTimeColumn(string name)
{
DataFrameColumn column = this[name];
if (column is PrimitiveDataFrameColumn<DateTime> ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(DateTime)));
}
/// <summary>
/// Gets the <see cref="ArrowStringDataFrameColumn"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="ArrowStringDataFrameColumn"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public ArrowStringDataFrameColumn GetArrowStringColumn(string name)
{
DataFrameColumn column = this[name];
if (column is ArrowStringDataFrameColumn ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(string)));
}
/// <summary>
/// Gets the <see cref="StringDataFrameColumn"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="StringDataFrameColumn"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public StringDataFrameColumn GetStringColumn(string name)
{
DataFrameColumn column = this[name];
if (column is StringDataFrameColumn ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(string)));
}
/// <summary>
/// Gets the <see cref="PrimitiveDataFrameColumn{T}"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="PrimitiveDataFrameColumn{T}"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public PrimitiveDataFrameColumn<bool> GetBooleanColumn(string name)
{
DataFrameColumn column = this[name];
if (column is PrimitiveDataFrameColumn<bool> ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Boolean)));
}
/// <summary>
/// Gets the <see cref="PrimitiveDataFrameColumn{T}"/> with the specified <paramref name="name"/> and attempts to return it as an <see cref="PrimitiveDataFrameColumn{T}"/>. If <see cref="DataFrameColumn.DataType"/> is not of type <see cref="Byte"/>, an exception is thrown.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="PrimitiveDataFrameColumn{T}"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public PrimitiveDataFrameColumn<byte> GetByteColumn(string name)
{
DataFrameColumn column = this[name];
if (column is PrimitiveDataFrameColumn<byte> ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Byte)));
}
/// <summary>
/// Gets the <see cref="PrimitiveDataFrameColumn{T}"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="PrimitiveDataFrameColumn{T}"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public PrimitiveDataFrameColumn<char> GetCharColumn(string name)
{
DataFrameColumn column = this[name];
if (column is PrimitiveDataFrameColumn<char> ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Char)));
}
/// <summary>
/// Gets the <see cref="PrimitiveDataFrameColumn{T}"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="PrimitiveDataFrameColumn{T}"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public PrimitiveDataFrameColumn<double> GetDoubleColumn(string name)
{
DataFrameColumn column = this[name];
if (column is PrimitiveDataFrameColumn<double> ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Double)));
}
/// <summary>
/// Gets the <see cref="PrimitiveDataFrameColumn{T}"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="PrimitiveDataFrameColumn{T}"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public PrimitiveDataFrameColumn<decimal> GetDecimalColumn(string name)
{
DataFrameColumn column = this[name];
if (column is PrimitiveDataFrameColumn<decimal> ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Decimal)));
}
/// <summary>
/// Gets the <see cref="PrimitiveDataFrameColumn{T}"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="PrimitiveDataFrameColumn{T}"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public PrimitiveDataFrameColumn<float> GetSingleColumn(string name)
{
DataFrameColumn column = this[name];
if (column is PrimitiveDataFrameColumn<float> ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Single)));
}
/// <summary>
/// Gets the <see cref="PrimitiveDataFrameColumn{T}"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="PrimitiveDataFrameColumn{T}"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public PrimitiveDataFrameColumn<int> GetInt32Column(string name)
{
DataFrameColumn column = this[name];
if (column is PrimitiveDataFrameColumn<int> ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Int32)));
}
/// <summary>
/// Gets the <see cref="PrimitiveDataFrameColumn{T}"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="PrimitiveDataFrameColumn{T}"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public PrimitiveDataFrameColumn<long> GetInt64Column(string name)
{
DataFrameColumn column = this[name];
if (column is PrimitiveDataFrameColumn<long> ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Int64)));
}
/// <summary>
/// Gets the <see cref="PrimitiveDataFrameColumn{T}"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="PrimitiveDataFrameColumn{T}"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public PrimitiveDataFrameColumn<sbyte> GetSByteColumn(string name)
{
DataFrameColumn column = this[name];
if (column is PrimitiveDataFrameColumn<sbyte> ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(SByte)));
}
/// <summary>
/// Gets the <see cref="PrimitiveDataFrameColumn{T}"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="PrimitiveDataFrameColumn{T}"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public PrimitiveDataFrameColumn<short> GetInt16Column(string name)
{
DataFrameColumn column = this[name];
if (column is PrimitiveDataFrameColumn<short> ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Int16)));
}
/// <summary>
/// Gets the <see cref="PrimitiveDataFrameColumn{T}"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="PrimitiveDataFrameColumn{T}"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public PrimitiveDataFrameColumn<uint> GetUInt32Column(string name)
{
DataFrameColumn column = this[name];
if (column is PrimitiveDataFrameColumn<uint> ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(string)));
}
/// <summary>
/// Gets the <see cref="PrimitiveDataFrameColumn{T}"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="PrimitiveDataFrameColumn{T}"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public PrimitiveDataFrameColumn<ulong> GetUInt64Column(string name)
{
DataFrameColumn column = this[name];
if (column is PrimitiveDataFrameColumn<ulong> ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(UInt64)));
}
/// <summary>
/// Gets the <see cref="PrimitiveDataFrameColumn{T}"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="PrimitiveDataFrameColumn{T}"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public PrimitiveDataFrameColumn<ushort> GetUInt16Column(string name)
{
DataFrameColumn column = this[name];
if (column is PrimitiveDataFrameColumn<ushort> ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(UInt16)));
}
}
}