diff --git a/src/Microsoft.Data.Analysis/DataFrame.cs b/src/Microsoft.Data.Analysis/DataFrame.cs index e62f49a2e..3f7d0dd13 100644 --- a/src/Microsoft.Data.Analysis/DataFrame.cs +++ b/src/Microsoft.Data.Analysis/DataFrame.cs @@ -147,6 +147,18 @@ namespace Microsoft.Data.Analysis } } + /// + /// An indexer based on + /// + /// The name of a + /// A if it exists. + /// Throws if is not present in this + public DataFrameColumn this[string columnName] + { + get => Columns[columnName]; + set => Columns[columnName] = value; + } + /// /// Returns the first rows /// diff --git a/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs b/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs index 90779cb85..2c3b72b28 100644 --- a/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs +++ b/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs @@ -133,7 +133,7 @@ namespace Microsoft.Data.Analysis /// public int IndexOf(string columnName) { - if (_columnNameToIndexDictionary.TryGetValue(columnName, out int columnIndex)) + if (columnName != null && _columnNameToIndexDictionary.TryGetValue(columnName, out int columnIndex)) { return columnIndex; } diff --git a/tests/Microsoft.Data.Analysis.Tests/DataFrameTests.cs b/tests/Microsoft.Data.Analysis.Tests/DataFrameTests.cs index d40b13f23..f32edd64d 100644 --- a/tests/Microsoft.Data.Analysis.Tests/DataFrameTests.cs +++ b/tests/Microsoft.Data.Analysis.Tests/DataFrameTests.cs @@ -200,10 +200,11 @@ namespace Microsoft.Data.Analysis.Tests var row = dataFrame.Rows[4]; Assert.Equal(14, (int)row[1]); - var column = dataFrame.Columns["Int2"] as Int32DataFrameColumn; + var column = dataFrame["Int2"] as Int32DataFrameColumn; Assert.Equal(1000, (int)column[2]); - Assert.Throws(() => dataFrame.Columns["Int5"]); + Assert.Throws(() => dataFrame["Int5"]); + Assert.Throws(() => dataFrame[(string)null]); } [Fact] @@ -645,7 +646,7 @@ namespace Microsoft.Data.Analysis.Tests { DataFrame df = MakeDataFrameWithTwoColumns(10); - df.Columns["Int3"] = df.Columns["Int1"] * 2 + df.Columns["Int2"]; + df["Int3"] = df.Columns["Int1"] * 2 + df.Columns["Int2"]; Assert.Equal(16, df.Columns["Int3"][2]); } @@ -653,7 +654,7 @@ namespace Microsoft.Data.Analysis.Tests public void TestComputations() { DataFrame df = MakeDataFrameWithAllMutableColumnTypes(10); - df.Columns["Int"][0] = -10; + df["Int"][0] = -10; Assert.Equal(-10, df.Columns["Int"][0]); DataFrameColumn absColumn = df.Columns["Int"].Abs();