Skip to content

Commit

Permalink
Params constructor on DataFrame (dotnet#2800)
Browse files Browse the repository at this point in the history
* Params constructor on DataFrame

* Delete redundant constructors
  • Loading branch information
Prashanth Govindarajan committed Dec 6, 2019
1 parent e64cbad commit 303ba62
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/Microsoft.Data.Analysis/DataFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ public partial class DataFrame
{
private readonly DataFrameColumnCollection _columnCollection;
private readonly DataFrameRowCollection _rowCollection;
public DataFrame()

public DataFrame(IEnumerable<DataFrameColumn> columns)
{
_columnCollection = new DataFrameColumnCollection(OnColumnsChanged);
_columnCollection = new DataFrameColumnCollection(columns, OnColumnsChanged);
_rowCollection = new DataFrameRowCollection(this);
}

public DataFrame(IList<DataFrameColumn> columns)
public DataFrame(params DataFrameColumn[] columns)
{
_columnCollection = new DataFrameColumnCollection(columns, OnColumnsChanged);
_rowCollection = new DataFrameRowCollection(this);
Expand Down
11 changes: 3 additions & 8 deletions src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,13 @@ public class DataFrameColumnCollection : Collection<DataFrameColumn>

internal long RowCount { get; set; }

internal DataFrameColumnCollection(Action columnsChanged) : base()
{
ColumnsChanged = columnsChanged;
}

internal DataFrameColumnCollection(IList<DataFrameColumn> columns, Action columnsChanged) : base()
internal DataFrameColumnCollection(IEnumerable<DataFrameColumn> columns, Action columnsChanged) : base()
{
columns = columns ?? throw new ArgumentNullException(nameof(columns));
ColumnsChanged = columnsChanged;
for (int i = 0; i < columns.Count; i++)
foreach (DataFrameColumn column in columns)
{
Add(columns[i]);
Add(column);
}
}

Expand Down
6 changes: 6 additions & 0 deletions tests/Microsoft.Data.Analysis.Tests/DataFrameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ public void ColumnAndTableCreationTest()
dataFrame.Columns.RemoveAt(1);
Assert.Single(dataFrame.Columns);
Assert.True(ReferenceEquals(intColumn, dataFrame.Columns[0]));

// Test the params constructor
DataFrame dataFrame1 = new DataFrame(intColumn, floatColumn);
Assert.Equal(2, dataFrame1.Columns.Count);
Assert.Equal(intColumn, dataFrame1.Columns[0]);
Assert.Equal(floatColumn, dataFrame1.Columns[1]);
}

[Fact]
Expand Down

0 comments on commit 303ba62

Please sign in to comment.