Skip to content
This repository was archived by the owner on Aug 2, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Microsoft.Data.Analysis/DataFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ public DataFrame this[IEnumerable<bool> rowFilter]
}
}

/// <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 => Columns[columnName];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The case that the column doesn't exist is handled and tested.
What's about the case of columnName = null?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it turns out, we have too many overloads of this indexer, so the only way to call this is to do df[(string)null]. I fixed the null case just in case though :)

set => Columns[columnName] = value;
}

/// <summary>
/// Returns the first <paramref name="numberOfRows"/> rows
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void Remove(string columnName)
/// <param name="columnName"></param>
public int IndexOf(string columnName)
{
if (_columnNameToIndexDictionary.TryGetValue(columnName, out int columnIndex))
if (columnName != null && _columnNameToIndexDictionary.TryGetValue(columnName, out int columnIndex))
{
return columnIndex;
}
Expand Down
9 changes: 5 additions & 4 deletions tests/Microsoft.Data.Analysis.Tests/DataFrameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,11 @@ public void TestIndexer()
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<ArgumentException>(() => dataFrame.Columns["Int5"]);
Assert.Throws<ArgumentException>(() => dataFrame["Int5"]);
Assert.Throws<ArgumentException>(() => dataFrame[(string)null]);
}

[Fact]
Expand Down Expand Up @@ -645,15 +646,15 @@ public void TestProjectionAndAppend()
{
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]);
}

[Fact]
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();
Expand Down