Skip to content
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
17 changes: 12 additions & 5 deletions src/Microsoft.Data.Analysis/DataFrame.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

Expand Down Expand Up @@ -483,6 +483,7 @@ private void ResizeByOneAndAppend(DataFrameColumn column, object value)
/// </summary>
/// <remarks>If an input column's value doesn't match a DataFrameColumn's data type, a conversion will be attempted</remarks>
/// <remarks>If a <seealso cref="DataFrameRow"/> in <paramref name="rows"/> is null, a null value is appended to each column</remarks>
/// <remarks> Values are appended based on the column names</remarks>
/// <param name="rows">The rows to be appended to this DataFrame </param>
/// <param name="inPlace">If set, appends <paramref name="rows"/> in place. Otherwise, a new DataFrame is returned with the <paramref name="rows"/> appended</param>
/// <param name="cultureInfo">culture info for formatting values</param>
Expand All @@ -491,7 +492,7 @@ public DataFrame Append(IEnumerable<DataFrameRow> rows, bool inPlace = false, Cu
DataFrame ret = inPlace ? this : Clone();
foreach (DataFrameRow row in rows)
{
ret.Append(row, inPlace: true, cultureInfo: cultureInfo);
ret.Append(row.GetValues(), inPlace: true, cultureInfo: cultureInfo);
}
return ret;
}
Expand All @@ -503,7 +504,7 @@ public DataFrame Append(IEnumerable<DataFrameRow> rows, bool inPlace = false, Cu
/// <remarks>If <paramref name="row"/> is null, a null value is appended to each column</remarks>
/// <param name="row"></param>
/// <param name="inPlace">If set, appends a <paramref name="row"/> in place. Otherwise, a new DataFrame is returned with an appended <paramref name="row"/> </param>
/// <param name="cultureInfo">culture info for formatting values</param>
/// <param name="cultureInfo">Culture info for formatting values</param>
public DataFrame Append(IEnumerable<object> row = null, bool inPlace = false, CultureInfo cultureInfo = null)
{
if (cultureInfo == null)
Expand Down Expand Up @@ -586,8 +587,14 @@ public DataFrame Append(IEnumerable<object> row = null, bool inPlace = false, Cu
/// <remarks>If a column's value doesn't match its column's data type, a conversion will be attempted</remarks>
/// <param name="row">An enumeration of column name and value to be appended</param>
/// <param name="inPlace">If set, appends <paramref name="row"/> in place. Otherwise, a new DataFrame is returned with an appended <paramref name="row"/> </param>
public DataFrame Append(IEnumerable<KeyValuePair<string, object>> row, bool inPlace = false)
/// <param name="cultureInfo">Culture info for formatting values</param>
public DataFrame Append(IEnumerable<KeyValuePair<string, object>> row, bool inPlace = false, CultureInfo cultureInfo = null)
{
if (cultureInfo == null)
{
cultureInfo = CultureInfo.CurrentCulture;
}

DataFrame ret = inPlace ? this : Clone();
if (row == null)
{
Expand All @@ -608,7 +615,7 @@ public DataFrame Append(IEnumerable<KeyValuePair<string, object>> row, bool inPl
object value = columnAndValue.Value;
if (value != null)
{
value = Convert.ChangeType(value, column.DataType);
value = Convert.ChangeType(value, column.DataType, cultureInfo);
if (value is null)
{
throw new ArgumentException(string.Format(Strings.MismatchedValueType, column.DataType), column.Name);
Expand Down
7 changes: 7 additions & 0 deletions src/Microsoft.Data.Analysis/DataFrameRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace Microsoft.Data.Analysis
Expand All @@ -17,6 +18,7 @@ public class DataFrameRow : IEnumerable<object>
{
private readonly DataFrame _dataFrame;
private readonly long _rowIndex;

internal DataFrameRow(DataFrame df, long rowIndex)
{
Debug.Assert(rowIndex < df.Columns.RowCount);
Expand All @@ -35,6 +37,11 @@ public IEnumerator<object> GetEnumerator()
}
}

public IEnumerable<KeyValuePair<string, object>> GetValues()
{
return _dataFrame.Columns.Select(col => new KeyValuePair<string, object>(col.Name, col[_rowIndex]));
}

/// <summary>
/// An indexer to return the value at <paramref name="index"/>.
/// </summary>
Expand Down
29 changes: 29 additions & 0 deletions test/Microsoft.Data.Analysis.Tests/DataFrameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3206,6 +3206,35 @@ void Verify(DataFrame ret, DataFrame check1, DataFrame check2)
Verify(df, dfClone, df2);
}

[Fact]
public void TestAppendRowsIfColumnAreOutOfOrder()
{
var dataFrame = new DataFrame(
new StringDataFrameColumn("ColumnA", new string[] { "a", "b", "c" }),
new Int32DataFrameColumn("ColumnB", new int[] { 1, 2, 3 }),
new Int32DataFrameColumn("ColumnC", new int[] { 10, 20, 30 }));

//ColumnC and ColumnB are swaped
var dataFrame2 = new DataFrame(
new StringDataFrameColumn("ColumnA", new string[] { "d", "e", "f" }),
new Int32DataFrameColumn("ColumnC", new int[] { 40, 50, 60 }),
new Int32DataFrameColumn("ColumnB", new int[] { 4, 5, 6 }));

var resultDataFrame = dataFrame.Append(dataFrame2.Rows);

Assert.Equal(3, resultDataFrame.Columns.Count);
Assert.Equal(6, resultDataFrame.Rows.Count);

Assert.Equal("c", resultDataFrame["ColumnA"][2]);
Assert.Equal("d", resultDataFrame["ColumnA"][3]);

Assert.Equal(3, resultDataFrame["ColumnB"][2]);
Assert.Equal(4, resultDataFrame["ColumnB"][3]);

Assert.Equal(30, resultDataFrame["ColumnC"][2]);
Assert.Equal(40, resultDataFrame["ColumnC"][3]);
}

[Fact]
public void TestAppendRow()
{
Expand Down