From 303ba62d10d25edc282ba811e2dbbc4a739f8fc4 Mon Sep 17 00:00:00 2001 From: Prashanth Govindarajan Date: Thu, 5 Dec 2019 16:14:18 -0800 Subject: [PATCH] Params constructor on DataFrame (#2800) * Params constructor on DataFrame * Delete redundant constructors --- src/Microsoft.Data.Analysis/DataFrame.cs | 7 ++++--- .../DataFrameColumnCollection.cs | 11 +++-------- tests/Microsoft.Data.Analysis.Tests/DataFrameTests.cs | 6 ++++++ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Microsoft.Data.Analysis/DataFrame.cs b/src/Microsoft.Data.Analysis/DataFrame.cs index 6bf50f46c9..d6e5b1bcc6 100644 --- a/src/Microsoft.Data.Analysis/DataFrame.cs +++ b/src/Microsoft.Data.Analysis/DataFrame.cs @@ -33,13 +33,14 @@ public partial class DataFrame { private readonly DataFrameColumnCollection _columnCollection; private readonly DataFrameRowCollection _rowCollection; - public DataFrame() + + public DataFrame(IEnumerable columns) { - _columnCollection = new DataFrameColumnCollection(OnColumnsChanged); + _columnCollection = new DataFrameColumnCollection(columns, OnColumnsChanged); _rowCollection = new DataFrameRowCollection(this); } - public DataFrame(IList columns) + public DataFrame(params DataFrameColumn[] columns) { _columnCollection = new DataFrameColumnCollection(columns, OnColumnsChanged); _rowCollection = new DataFrameRowCollection(this); diff --git a/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs b/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs index 68861f2f43..e362af2b15 100644 --- a/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs +++ b/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs @@ -21,18 +21,13 @@ public class DataFrameColumnCollection : Collection internal long RowCount { get; set; } - internal DataFrameColumnCollection(Action columnsChanged) : base() - { - ColumnsChanged = columnsChanged; - } - - internal DataFrameColumnCollection(IList columns, Action columnsChanged) : base() + internal DataFrameColumnCollection(IEnumerable 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); } } diff --git a/tests/Microsoft.Data.Analysis.Tests/DataFrameTests.cs b/tests/Microsoft.Data.Analysis.Tests/DataFrameTests.cs index 36bce6d754..b5ddefddd5 100644 --- a/tests/Microsoft.Data.Analysis.Tests/DataFrameTests.cs +++ b/tests/Microsoft.Data.Analysis.Tests/DataFrameTests.cs @@ -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]