diff --git a/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs b/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs index 5488d8ca93..2f73c4d547 100644 --- a/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs +++ b/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs @@ -175,5 +175,278 @@ public DataFrameColumn this[string columnName] } } + /// + /// Gets the with the specified . + /// + /// The name of the column + /// . + /// A column named cannot be found, or if the column's type doesn't match. + public PrimitiveDataFrameColumn GetPrimitiveColumn(string name) + where T : unmanaged + { + DataFrameColumn column = this[name]; + if (column is PrimitiveDataFrameColumn ret) + { + return ret; + } + + throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(T)), nameof(T)); + } + + /// + /// Gets the with the specified . + /// + /// The name of the column + /// . + /// A column named cannot be found, or if the column's type doesn't match. + 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))); + } + + /// + /// Gets the with the specified . + /// + /// The name of the column + /// . + /// A column named cannot be found, or if the column's type doesn't match. + 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))); + } + + /// + /// Gets the with the specified . + /// + /// The name of the column + /// . + /// A column named cannot be found, or if the column's type doesn't match. + public BooleanDataFrameColumn GetBooleanColumn(string name) + { + DataFrameColumn column = this[name]; + if (column is BooleanDataFrameColumn ret) + { + return ret; + } + + throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Boolean))); + } + + /// + /// Gets the with the specified and attempts to return it as an . If is not of type , an exception is thrown. + /// + /// The name of the column + /// . + /// A column named cannot be found, or if the column's type doesn't match. + public ByteDataFrameColumn GetByteColumn(string name) + { + DataFrameColumn column = this[name]; + if (column is ByteDataFrameColumn ret) + { + return ret; + } + + throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Byte))); + } + + /// + /// Gets the with the specified . + /// + /// The name of the column + /// . + /// A column named cannot be found, or if the column's type doesn't match. + public CharDataFrameColumn GetCharColumn(string name) + { + DataFrameColumn column = this[name]; + if (column is CharDataFrameColumn ret) + { + return ret; + } + + throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Char))); + } + + /// + /// Gets the with the specified . + /// + /// The name of the column + /// . + /// A column named cannot be found, or if the column's type doesn't match. + public DoubleDataFrameColumn GetDoubleColumn(string name) + { + DataFrameColumn column = this[name]; + if (column is DoubleDataFrameColumn ret) + { + return ret; + } + + throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Double))); + } + + /// + /// Gets the with the specified . + /// + /// The name of the column + /// . + /// A column named cannot be found, or if the column's type doesn't match. + public DecimalDataFrameColumn GetDecimalColumn(string name) + { + DataFrameColumn column = this[name]; + if (column is DecimalDataFrameColumn ret) + { + return ret; + } + + throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Decimal))); + } + + /// + /// Gets the with the specified . + /// + /// The name of the column + /// . + /// A column named cannot be found, or if the column's type doesn't match. + public SingleDataFrameColumn GetSingleColumn(string name) + { + DataFrameColumn column = this[name]; + if (column is SingleDataFrameColumn ret) + { + return ret; + } + + throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Single))); + } + + /// + /// Gets the with the specified . + /// + /// The name of the column + /// . + /// A column named cannot be found, or if the column's type doesn't match. + public Int32DataFrameColumn GetInt32Column(string name) + { + DataFrameColumn column = this[name]; + if (column is Int32DataFrameColumn ret) + { + return ret; + } + + throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Int32))); + } + + /// + /// Gets the with the specified . + /// + /// The name of the column + /// . + /// A column named cannot be found, or if the column's type doesn't match. + public Int64DataFrameColumn GetInt64Column(string name) + { + DataFrameColumn column = this[name]; + if (column is Int64DataFrameColumn ret) + { + return ret; + } + + throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Int64))); + } + + /// + /// Gets the with the specified . + /// + /// The name of the column + /// . + /// A column named cannot be found, or if the column's type doesn't match. + public SByteDataFrameColumn GetSByteColumn(string name) + { + DataFrameColumn column = this[name]; + if (column is SByteDataFrameColumn ret) + { + return ret; + } + + throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(SByte))); + } + + /// + /// Gets the with the specified . + /// + /// The name of the column + /// . + /// A column named cannot be found, or if the column's type doesn't match. + public Int16DataFrameColumn GetInt16Column(string name) + { + DataFrameColumn column = this[name]; + if (column is Int16DataFrameColumn ret) + { + return ret; + } + + throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(Int16))); + } + + /// + /// Gets the with the specified . + /// + /// The name of the column + /// . + /// A column named cannot be found, or if the column's type doesn't match. + public UInt32DataFrameColumn GetUInt32Column(string name) + { + DataFrameColumn column = this[name]; + if (column is UInt32DataFrameColumn ret) + { + return ret; + } + + throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(string))); + } + + /// + /// Gets the with the specified . + /// + /// The name of the column + /// . + /// A column named cannot be found, or if the column's type doesn't match. + public UInt64DataFrameColumn GetUInt64Column(string name) + { + DataFrameColumn column = this[name]; + if (column is UInt64DataFrameColumn ret) + { + return ret; + } + + throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(UInt64))); + } + + /// + /// Gets the with the specified . + /// + /// The name of the column + /// . + /// A column named cannot be found, or if the column's type doesn't match. + public UInt16DataFrameColumn GetUInt16Column(string name) + { + DataFrameColumn column = this[name]; + if (column is UInt16DataFrameColumn ret) + { + return ret; + } + + throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(UInt16))); + } + } } diff --git a/src/Microsoft.Data.Analysis/strings.Designer.cs b/src/Microsoft.Data.Analysis/strings.Designer.cs index f7bf691b47..6196e234a3 100644 --- a/src/Microsoft.Data.Analysis/strings.Designer.cs +++ b/src/Microsoft.Data.Analysis/strings.Designer.cs @@ -60,6 +60,15 @@ internal Strings() { } } + /// + /// Looks up a localized string similar to Cannot cast column holding {0} values to type {1}. + /// + internal static string BadColumnCast { + get { + return ResourceManager.GetString("BadColumnCast", resourceCulture); + } + } + /// /// Looks up a localized string similar to Cannot resize down. /// diff --git a/src/Microsoft.Data.Analysis/strings.resx b/src/Microsoft.Data.Analysis/strings.resx index 7e9d040bc6..446b3b3f37 100644 --- a/src/Microsoft.Data.Analysis/strings.resx +++ b/src/Microsoft.Data.Analysis/strings.resx @@ -117,6 +117,9 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + Cannot cast column holding {0} values to type {1} + Cannot resize down diff --git a/tests/Microsoft.Data.Analysis.Tests/DataFrameTests.cs b/tests/Microsoft.Data.Analysis.Tests/DataFrameTests.cs index 0df08fb746..44636ffd72 100644 --- a/tests/Microsoft.Data.Analysis.Tests/DataFrameTests.cs +++ b/tests/Microsoft.Data.Analysis.Tests/DataFrameTests.cs @@ -2179,5 +2179,75 @@ public void TestBinaryOperationsOnExplodedNumericColumns() Assert.True(reverseInPlace.ElementwiseEquals(ints).All()); Assert.False(reverseInPlace.ElementwiseEquals(reverse).All()); } + + [Fact] + public void GetColumnTests() + { + DataFrame dataFrame = MakeDataFrameWithAllColumnTypes(10); + PrimitiveDataFrameColumn primitiveInts = dataFrame.Columns.GetPrimitiveColumn("Int"); + Assert.NotNull(primitiveInts); + Assert.Throws(() => dataFrame.Columns.GetPrimitiveColumn("Int")); + + StringDataFrameColumn strings = dataFrame.Columns.GetStringColumn("String"); + Assert.NotNull(strings); + Assert.Throws(() => dataFrame.Columns.GetStringColumn("ArrowString")); + + ArrowStringDataFrameColumn arrowStrings = dataFrame.Columns.GetArrowStringColumn("ArrowString"); + Assert.NotNull(arrowStrings); + Assert.Throws(() => dataFrame.Columns.GetArrowStringColumn("String")); + + ByteDataFrameColumn bytes = dataFrame.Columns.GetByteColumn("Byte"); + Assert.NotNull(bytes); + Assert.Throws(() => dataFrame.Columns.GetSingleColumn("Byte")); + + Int32DataFrameColumn ints = dataFrame.Columns.GetInt32Column("Int"); + Assert.NotNull(ints); + Assert.Throws(() => dataFrame.Columns.GetSingleColumn("Int")); + + BooleanDataFrameColumn bools = dataFrame.Columns.GetBooleanColumn("Bool"); + Assert.NotNull(bools); + Assert.Throws(() => dataFrame.Columns.GetSingleColumn("Bool")); + + CharDataFrameColumn chars = dataFrame.Columns.GetCharColumn("Char"); + Assert.NotNull(chars); + Assert.Throws(() => dataFrame.Columns.GetSingleColumn("Char")); + + DecimalDataFrameColumn decimals = dataFrame.Columns.GetDecimalColumn("Decimal"); + Assert.NotNull(decimals); + Assert.Throws(() => dataFrame.Columns.GetSingleColumn("Decimal")); + + DoubleDataFrameColumn doubles = dataFrame.Columns.GetDoubleColumn("Double"); + Assert.NotNull(doubles); + Assert.Throws(() => dataFrame.Columns.GetSingleColumn("Double")); + + SingleDataFrameColumn singles = dataFrame.Columns.GetSingleColumn("Float"); + Assert.NotNull(singles); + Assert.Throws(() => dataFrame.Columns.GetDoubleColumn("Float")); + + Int64DataFrameColumn longs = dataFrame.Columns.GetInt64Column("Long"); + Assert.NotNull(longs); + Assert.Throws(() => dataFrame.Columns.GetSingleColumn("Long")); + + SByteDataFrameColumn sbytes = dataFrame.Columns.GetSByteColumn("Sbyte"); + Assert.NotNull(sbytes); + Assert.Throws(() => dataFrame.Columns.GetSingleColumn("Sbyte")); + + Int16DataFrameColumn shorts = dataFrame.Columns.GetInt16Column("Short"); + Assert.NotNull(shorts); + Assert.Throws(() => dataFrame.Columns.GetSingleColumn("Short")); + + UInt32DataFrameColumn uints = dataFrame.Columns.GetUInt32Column("Uint"); + Assert.NotNull(uints); + Assert.Throws(() => dataFrame.Columns.GetSingleColumn("Uint")); + + UInt64DataFrameColumn ulongs = dataFrame.Columns.GetUInt64Column("Ulong"); + Assert.NotNull(ulongs); + Assert.Throws(() => dataFrame.Columns.GetSingleColumn("Ulong")); + + UInt16DataFrameColumn ushorts = dataFrame.Columns.GetUInt16Column("Ushort"); + Assert.NotNull(ushorts); + Assert.Throws(() => dataFrame.Columns.GetSingleColumn("Ushort")); + + } } }