diff --git a/src/Microsoft.Data.Analysis/DataFrameColumn.cs b/src/Microsoft.Data.Analysis/DataFrameColumn.cs index 579e8b8b1e..1795857070 100644 --- a/src/Microsoft.Data.Analysis/DataFrameColumn.cs +++ b/src/Microsoft.Data.Analysis/DataFrameColumn.cs @@ -24,6 +24,44 @@ public DataFrameColumn(string name, long length, Type type) DataType = type; } + /// + /// A static factory method to create a . + /// It allows you to take advantage of type inference based on the type of the values supplied. + /// + /// The type of the column to create. + /// The name of the column. + /// The initial values to populate in the column. + /// A populated with the provided data. + public static PrimitiveDataFrameColumn Create(string name, IEnumerable values) where T : unmanaged + { + return new PrimitiveDataFrameColumn(name, values); + } + + /// + /// A static factory method to create a . + /// It allows you to take advantage of type inference based on the type of the values supplied. + /// + /// The type of the column to create. + /// The name of the column. + /// The initial values to populate in the column. + /// A populated with the provided data. + public static PrimitiveDataFrameColumn Create(string name, IEnumerable values) where T : unmanaged + { + return new PrimitiveDataFrameColumn(name, values); + } + + /// + /// A static factory method to create a . + /// It allows you to take advantage of type inference based on the type of the values supplied. + /// + /// The name of the column. + /// The initial values to populate in the column. + /// A populated with the provided data. + public static StringDataFrameColumn Create(string name, IEnumerable values) + { + return new StringDataFrameColumn(name, values); + } + private long _length; public long Length { diff --git a/tests/Microsoft.Data.Analysis.Tests/DataFrameTests.cs b/tests/Microsoft.Data.Analysis.Tests/DataFrameTests.cs index 0de7e10e46..7c448cbe6f 100644 --- a/tests/Microsoft.Data.Analysis.Tests/DataFrameTests.cs +++ b/tests/Microsoft.Data.Analysis.Tests/DataFrameTests.cs @@ -104,14 +104,14 @@ public static DataFrame MakeDataFrameWithNumericAndBoolColumns(int length) public static DataFrame MakeDataFrameWithNumericAndStringColumns(int length, bool withNulls = true) { DataFrame df = MakeDataFrameWithNumericColumns(length, withNulls); - DataFrameColumn stringColumn = new StringDataFrameColumn("String", Enumerable.Range(0, length).Select(x => x.ToString())); + DataFrameColumn stringColumn = DataFrameColumn.Create("String", Enumerable.Range(0, length).Select(x => x.ToString())); df.Columns.Insert(df.Columns.Count, stringColumn); if (withNulls) { stringColumn[length / 2] = null; } - DataFrameColumn charColumn = new PrimitiveDataFrameColumn("Char", Enumerable.Range(0, length).Select(x => (char)(x + 65))); + DataFrameColumn charColumn = DataFrameColumn.Create("Char", Enumerable.Range(0, length).Select(x => (char)(x + 65))); df.Columns.Insert(df.Columns.Count, charColumn); if (withNulls) { @@ -122,17 +122,17 @@ public static DataFrame MakeDataFrameWithNumericAndStringColumns(int length, boo public static DataFrame MakeDataFrameWithNumericColumns(int length, bool withNulls = true) { - DataFrameColumn byteColumn = new PrimitiveDataFrameColumn("Byte", Enumerable.Range(0, length).Select(x => (byte)x)); - DataFrameColumn decimalColumn = new PrimitiveDataFrameColumn("Decimal", Enumerable.Range(0, length).Select(x => (decimal)x)); - DataFrameColumn doubleColumn = new PrimitiveDataFrameColumn("Double", Enumerable.Range(0, length).Select(x => (double)x)); - DataFrameColumn floatColumn = new PrimitiveDataFrameColumn("Float", Enumerable.Range(0, length).Select(x => (float)x)); - DataFrameColumn intColumn = new PrimitiveDataFrameColumn("Int", Enumerable.Range(0, length).Select(x => x)); - DataFrameColumn longColumn = new PrimitiveDataFrameColumn("Long", Enumerable.Range(0, length).Select(x => (long)x)); - DataFrameColumn sbyteColumn = new PrimitiveDataFrameColumn("Sbyte", Enumerable.Range(0, length).Select(x => (sbyte)x)); - DataFrameColumn shortColumn = new PrimitiveDataFrameColumn("Short", Enumerable.Range(0, length).Select(x => (short)x)); - DataFrameColumn uintColumn = new PrimitiveDataFrameColumn("Uint", Enumerable.Range(0, length).Select(x => (uint)x)); - DataFrameColumn ulongColumn = new PrimitiveDataFrameColumn("Ulong", Enumerable.Range(0, length).Select(x => (ulong)x)); - DataFrameColumn ushortColumn = new PrimitiveDataFrameColumn("Ushort", Enumerable.Range(0, length).Select(x => (ushort)x)); + DataFrameColumn byteColumn = DataFrameColumn.Create("Byte", Enumerable.Range(0, length).Select(x => (byte)x)); + DataFrameColumn decimalColumn = DataFrameColumn.Create("Decimal", Enumerable.Range(0, length).Select(x => (decimal)x)); + DataFrameColumn doubleColumn = DataFrameColumn.Create("Double", Enumerable.Range(0, length).Select(x => (double)x)); + DataFrameColumn floatColumn = DataFrameColumn.Create("Float", Enumerable.Range(0, length).Select(x => (float)x)); + DataFrameColumn intColumn = DataFrameColumn.Create("Int", Enumerable.Range(0, length).Select(x => x)); + DataFrameColumn longColumn = DataFrameColumn.Create("Long", Enumerable.Range(0, length).Select(x => (long)x)); + DataFrameColumn sbyteColumn = DataFrameColumn.Create("Sbyte", Enumerable.Range(0, length).Select(x => (sbyte)x)); + DataFrameColumn shortColumn = DataFrameColumn.Create("Short", Enumerable.Range(0, length).Select(x => (short)x)); + DataFrameColumn uintColumn = DataFrameColumn.Create("Uint", Enumerable.Range(0, length).Select(x => (uint)x)); + DataFrameColumn ulongColumn = DataFrameColumn.Create("Ulong", Enumerable.Range(0, length).Select(x => (ulong)x)); + DataFrameColumn ushortColumn = DataFrameColumn.Create("Ushort", Enumerable.Range(0, length).Select(x => (ushort)x)); DataFrame dataFrame = new DataFrame(new List { byteColumn, decimalColumn, doubleColumn, floatColumn, intColumn, longColumn, sbyteColumn, shortColumn, uintColumn, ulongColumn, ushortColumn }); @@ -150,8 +150,8 @@ public static DataFrame MakeDataFrame(int length, bool withNulls = true) where T1 : unmanaged where T2 : unmanaged { - DataFrameColumn baseColumn1 = new PrimitiveDataFrameColumn("Column1", Enumerable.Range(0, length).Select(x => (T1)Convert.ChangeType(x % 2 == 0 ? 0 : 1, typeof(T1)))); - DataFrameColumn baseColumn2 = new PrimitiveDataFrameColumn("Column2", Enumerable.Range(0, length).Select(x => (T2)Convert.ChangeType(x % 2 == 0 ? 0 : 1, typeof(T2)))); + DataFrameColumn baseColumn1 = DataFrameColumn.Create("Column1", Enumerable.Range(0, length).Select(x => (T1)Convert.ChangeType(x % 2 == 0 ? 0 : 1, typeof(T1)))); + DataFrameColumn baseColumn2 = DataFrameColumn.Create("Column2", Enumerable.Range(0, length).Select(x => (T2)Convert.ChangeType(x % 2 == 0 ? 0 : 1, typeof(T2)))); DataFrame dataFrame = new DataFrame(new List { baseColumn1, baseColumn2 }); if (withNulls)