Skip to content

Commit

Permalink
Merge pull request #515 from zyzhu/issue-514
Browse files Browse the repository at this point in the history
Remove duplicated sum function in FrameExtensions
  • Loading branch information
zyzhu authored Sep 16, 2020
2 parents 5cd6aad + c5f113c commit 608c005
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 101 deletions.
3 changes: 0 additions & 3 deletions src/Deedle/FrameExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,9 +1047,6 @@ type FrameExtensions =
[<Extension>]
static member Print(frame:Frame<'K, 'V>, printTypes:bool) = Console.WriteLine(frame.Format(printTypes));

[<Extension>]
static member Sum(frame:Frame<'R, 'C>) = Stats.sum frame

[<Extension>]
static member Window(frame:Frame<'R, 'C>, size) = Frame.window size frame

Expand Down
213 changes: 115 additions & 98 deletions tests/Deedle.CSharp.Tests/Frame.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using NUnit.Framework;
using Deedle;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -48,116 +45,136 @@ public static void CanSubtractNumericalValuesStream()
}
}

/* ----------------------------------------------------------------------------------
* Creating frames and getting frame data
* --------------------------------------------------------------------------------*/
/* ----------------------------------------------------------------------------------
* Creating frames and getting frame data
* --------------------------------------------------------------------------------*/

public class PublicFields
{
public readonly int A;
public readonly string B;
public PublicFields(int a, string b)
public class PublicFields
{
this.A = a;
this.B = b;
public readonly int A;
public readonly string B;
public PublicFields(int a, string b)
{
this.A = a;
this.B = b;
}
}
}

public class FrameCreateAccessTests
{
[Test]
public static void CanCreateFrameFromRecords()
public class FrameCreateAccessTests
{
var df = Frame.FromRecords(new[] {
new { A = 1, B = "Test" },
new { A = 2, B = "Another"}
});
var firstRow = df.Rows[0].Values.ToArray();
Assert.AreEqual(new object[] { 1, "Test" }, firstRow);
Assert.AreEqual(new[] { "A", "B" }, df.ColumnKeys.ToArray());
}
[Test]
public static void CanCreateFrameFromRecords()
{
var df = Frame.FromRecords(new[] {
new { A = 1, B = "Test" },
new { A = 2, B = "Another"}
});
var firstRow = df.Rows[0].Values.ToArray();
Assert.AreEqual(new object[] { 1, "Test" }, firstRow);
Assert.AreEqual(new[] { "A", "B" }, df.ColumnKeys.ToArray());
}

[Test]
public static void CanCreateFrameFromFields()
{
var df = Frame.FromRecords(new[] {
new PublicFields(1, "Test"),
new PublicFields(2, "Another")
});
var firstRow = df.Rows[0].Values.ToArray();
Assert.AreEqual(new object[] { 1, "Test" }, firstRow);
Assert.AreEqual(new[] { "A", "B" }, df.ColumnKeys.ToArray());
}
[Test]
public static void CanCreateFrameFromFields()
{
var df = Frame.FromRecords(new[] {
new PublicFields(1, "Test"),
new PublicFields(2, "Another")
});
var firstRow = df.Rows[0].Values.ToArray();
Assert.AreEqual(new object[] { 1, "Test" }, firstRow);
Assert.AreEqual(new[] { "A", "B" }, df.ColumnKeys.ToArray());
}

[Test]
public static void CanRoundtripWithArray2D()
{
var arr = new double[200, 500];
for (var r = 0; r < 200; r++)
for (var c = 0; c < 500; c++)
arr[r, c] = (r + c == 10) ? Double.NaN : (r + c);
[Test]
public static void CanRoundtripWithArray2D()
{
var arr = new double[200, 500];
for (var r = 0; r < 200; r++)
for (var c = 0; c < 500; c++)
arr[r, c] = (r + c == 10) ? Double.NaN : (r + c);

var arr2 = Frame.FromArray2D(arr).ToArray2D<double>();
Assert.AreEqual(arr, arr2);
}
var arr2 = Frame.FromArray2D(arr).ToArray2D<double>();
Assert.AreEqual(arr, arr2);
}

[Test]
public static void CannotGetDefaultValueOfBoolean()
{
var df = Frame.FromColumns(new[] {
KeyValue.Create(1, (new SeriesBuilder<string>() { { "A", true } }).Series),
KeyValue.Create(2, (new SeriesBuilder<string>() { { "B", true } }).Series)
});
Assert.Throws<InvalidOperationException>(() =>
df.ToArray2D<bool>());
}
[Test]
public static void CannotGetDefaultValueOfBoolean()
{
var df = Frame.FromColumns(new[] {
KeyValue.Create(1, (new SeriesBuilder<string>() { { "A", true } }).Series),
KeyValue.Create(2, (new SeriesBuilder<string>() { { "B", true } }).Series)
});
Assert.Throws<InvalidOperationException>(() =>
df.ToArray2D<bool>());
}

[Test]
public static void CanGetDataAsBooleanWithDefault()
{
var df = Frame.FromColumns(new[] {
KeyValue.Create(1, (new SeriesBuilder<string>() { { "A", true } }).Series),
KeyValue.Create(2, (new SeriesBuilder<string>() { { "B", true } }).Series)
});
var data = df.ToArray2D<bool>(true);
var bools = data.OfType<bool>().ToArray();
Assert.AreEqual(new[] { true, true, true, true }, bools);
}
[Test]
public static void CanGetDataAsBooleanWithDefault()
{
var df = Frame.FromColumns(new[] {
KeyValue.Create(1, (new SeriesBuilder<string>() { { "A", true } }).Series),
KeyValue.Create(2, (new SeriesBuilder<string>() { { "B", true } }).Series)
});
var data = df.ToArray2D<bool>(true);
var bools = data.OfType<bool>().ToArray();
Assert.AreEqual(new[] { true, true, true, true }, bools);
}

[Test]
public static void FramesOrderedAfterBuild()
{
var builder = new FrameBuilder.Columns<int, string>();
builder.Add("column1", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
builder.Add("column2", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
builder.Add("column3", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
builder.Add("column4", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
var frame = builder.Frame;

Assert.AreEqual(
new[] { "column1", "column2", "column3", "column4" },
frame.ColumnKeys.ToArray()
);
[Test]
public static void FramesOrderedAfterBuild()
{
var builder = new FrameBuilder.Columns<int, string>();
builder.Add("column1", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
builder.Add("column2", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
builder.Add("column3", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
builder.Add("column4", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
var frame = builder.Frame;

Assert.AreEqual(
new[] { "column1", "column2", "column3", "column4" },
frame.ColumnKeys.ToArray()
);
}

[Test]
public static void DropSparseRowsFromRecords()
{
var f = Deedle.Frame.FromRecords(new[] {
new { a = "x", b = 1.0 },
new { a = "y", b = Double.NaN },
new { a = "z", b = 3.0 }
});
var b = Deedle.FrameModule.DropSparseRows(f).GetColumn<float>("b").GetAllValues().ToArray();
Assert.AreEqual(1.0, b[0].Value);
Assert.AreEqual(3.0, b[1].Value);
}
}

[Test]
public static void DropSparseRowsFromRecords()
/* ----------------------------------------------------------------------------------
* Stats functions on frame
* --------------------------------------------------------------------------------*/
public class FrameStatsTests
{
var f = Deedle.Frame.FromRecords(new[] {
new { a = "x", b = 1.0 },
new { a = "y", b = Double.NaN },
new { a = "z", b = 3.0 }
});
var b = Deedle.FrameModule.DropSparseRows(f).GetColumn<float>("b").GetAllValues().ToArray();
Assert.AreEqual(1.0, b[0].Value);
Assert.AreEqual(3.0, b[1].Value);
[Test]
public static void SumOfFrame()
{
var builder = new FrameBuilder.Columns<int, string>();
builder.Add("column1", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
builder.Add("column2", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
builder.Add("column3", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
builder.Add("column4", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
var df = builder.Frame;
var sum = df.Sum().GetAllValues().ToArray();
Assert.AreEqual(10.0, sum[0].Value);
Assert.AreEqual(10.0, sum[1].Value);
}
}
}

/* ----------------------------------------------------------------------------------
* Test data frame dynamic
* --------------------------------------------------------------------------------*/
public class DynamicFrameTests
/* ----------------------------------------------------------------------------------
* Test data frame dynamic
* --------------------------------------------------------------------------------*/
public class DynamicFrameTests
{
[Test]
public static void CanAddSeriesDynamically()
Expand Down

0 comments on commit 608c005

Please sign in to comment.