Skip to content

Commit ef61981

Browse files
committed
Added support for xUnit. Implemented test for arange updated test and console to .net 7.0 updated console to support command switches for extensibility.
1 parent 964b9e7 commit ef61981

13 files changed

+257
-15
lines changed

Pandas.NET.sln

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pandas.NET.Test", "test\Pan
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pandas.NET", "src\Pandas.NET\Pandas.NET.csproj", "{41658850-1B21-4409-BC35-D0E8A7B73661}"
99
EndProject
10-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pandas.Console", "test\Pandas.Console\Pandas.Console.csproj", "{16298C82-7219-4F52-ACD1-E0AFE650F207}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PandasConsole", "test\PandasConsole\PandasConsole.csproj", "{16298C82-7219-4F52-ACD1-E0AFE650F207}"
1111
EndProject
1212
Global
1313
GlobalSection(SolutionConfigurationPlatforms) = preSolution

src/Pandas.NET/DataFrameApi.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public DataFrame from_dict(string data)
1515
var json = (JObject)JsonConvert.DeserializeObject(data);
1616

1717
var cols = new List<Series>();
18-
foreach(var col in json)
18+
foreach (var col in json)
1919
{
2020
var column = new Column
2121
{
@@ -26,12 +26,14 @@ public DataFrame from_dict(string data)
2626
if (type == JTokenType.Integer)
2727
{
2828
var array = GetArray<int>(col.Value);
29+
column.DType = typeof(int);
2930
var series = new Series(array, column);
3031
cols.Add(series);
3132
}
3233
else if (type == JTokenType.String)
3334
{
3435
var array = GetArray<string>(col.Value);
36+
column.DType = typeof(string);
3537
var series = new Series(array, column);
3638
cols.Add(series);
3739
};

src/Pandas.NET/Methods/Pandas.arange.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace PandasNet
55
{
66
public partial class Pandas
77
{
8-
public T[] arange<T>(int start, int stop, int step = 1)
8+
public T[] arange<T>(int start = 0, int stop = 0, int step = 1)
99
{
1010
return typeof(T).Name switch
1111
{

src/Pandas.NET/Methods/Pandas.convert.cs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Text;
44
using System.Linq;
55
using System.Globalization;
6+
using OneOf.Types;
67

78
namespace PandasNet
89
{

test/Pandas.NET.Test/IndexSliceTest.cs

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
using Microsoft.VisualStudio.TestTools.UnitTesting;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
7-
using static PandasNet.PandasApi;
1+
using static PandasNet.PandasApi;
82

93
namespace Pandas.Test
104
{
11-
[TestClass]
125
public class IndexSliceTest
136
{
14-
[TestMethod]
7+
[Fact]
158
public void Test()
169
{
1710
var df = pd.DataFrame.from_dict("{'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using static PandasNet.PandasApi;
3+
4+
namespace Pandas.Test;
5+
6+
public partial class PandasTest
7+
{
8+
9+
/// <summary>
10+
/// Test arange method for int
11+
/// </summary>
12+
[Fact]
13+
public void test_arange_int()
14+
{
15+
//create an array of 10 elements from 0 to 9
16+
var arange = pd.arange<int>(0, 10, 1);
17+
Assert.Equal(10, arange.Length);
18+
for (int i = 0; i < arange.Length; i++)
19+
{
20+
Assert.Equal(i, arange[i]);
21+
}
22+
}
23+
24+
/// <summary>
25+
/// Test arange method for float
26+
/// </summary>
27+
[Fact]
28+
public void test_arange_float()
29+
{
30+
// Only int is supported
31+
Assert.Throws<NotImplementedException>(() => pd.arange<float>(0, 10, 1));
32+
}
33+
}

test/Pandas.NET.Test/Pandas.NET.Test.csproj

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net7.0</TargetFramework>
55

66
<IsPackable>false</IsPackable>
77

88
<RootNamespace>Pandas.Test</RootNamespace>
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
13-
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
13+
<PackageReference Include="xunit" Version="2.4.2" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16+
<PrivateAssets>all</PrivateAssets>
17+
</PackageReference>
18+
<PackageReference Include="coverlet.collector" Version="3.2.0">
19+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
20+
<PrivateAssets>all</PrivateAssets>
21+
</PackageReference>
1422
</ItemGroup>
1523

1624
<ItemGroup>

test/Pandas.NET.Test/Usings.cs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global using Xunit;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using PandasNet;
2+
using static PandasNet.PandasApi;
3+
4+
namespace PandasConsole.Methods
5+
{
6+
public class PandasConsoleConvert
7+
{
8+
public DataFrame GetSampleDataFrame()
9+
{
10+
return pd.DataFrame.from_dict("{'col_1': [3, 2, 1, 0], 'col_2': ['3', '2', '1', '0']}");
11+
}
12+
}
13+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
<RootNamespace>PandasTest</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\..\src\Pandas.NET\Pandas.NET.csproj" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="CommandLineParser" Version="2.9.1" />
15+
<PackageReference Include="System.Console" Version="4.3.1" />
16+
</ItemGroup>
17+
18+
</Project>

test/PandasConsole/Program.cs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using CommandLine;
6+
using PandasConsole.Methods;
7+
using Tensorflow;
8+
using static PandasNet.PandasApi;
9+
10+
namespace PandasConsole
11+
{
12+
class Program
13+
{
14+
public class Options
15+
{
16+
[Option("info", Required = false, HelpText = "Print the info a DataFrame")]
17+
public bool ConvertSample { get; set; }
18+
}
19+
20+
21+
static void Main(string[] args)
22+
{
23+
Parser.Default.ParseArguments<Options>(args)
24+
.WithParsed<Options>(o =>
25+
{
26+
if (o.ConvertSample)
27+
{
28+
CommandRunners.RunInfoSample();
29+
}
30+
})
31+
.WithNotParsed(CommandRunners.HandleParseError);
32+
33+
var df = pd.DataFrame.from_dict("{'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}");
34+
var df1 = df[new[] { "col_1" }];
35+
}
36+
37+
public static class CommandRunners
38+
{
39+
public static void HandleParseError(IEnumerable<Error> errs)
40+
{
41+
Console.WriteLine("An Error Occurred");
42+
}
43+
public static void RunInfoSample()
44+
{
45+
var converter = new PandasConsoleConvert();
46+
var df = converter.GetSampleDataFrame();
47+
Utils.PrintDataFrameInfo(df);
48+
}
49+
}
50+
}
51+
}

test/PandasConsole/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# PandasConsole
2+
3+
## Usage
4+
5+
### DotNet CLI
6+
7+
- Build the Pandas.NET solution
8+
- Run the project (from project root) `dotnet run --project .\test\PandasConsole --help`
9+
10+
## Implemented Options
11+
12+
### Info
13+
14+
> Prints a sample information table similar to calling df.info() in Python but instead rendering as markdown
15+
16+
| # | Column | Non-Null Count | DType |
17+
| --- | ------ | -------------- | ------------- |
18+
| 0 | col_1 | 4 | System.Int32 |
19+
| 1 | col_2 | 4 | System.String |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
using System.Text;
3+
using PandasNet;
4+
using System.Linq;
5+
using System.Data;
6+
using System.Collections.Generic;
7+
8+
namespace PandasConsole;
9+
public static class Utils
10+
{
11+
public static void PrintDataFrameInfo(DataFrame df)
12+
{
13+
int indexLength = df.shape[0];
14+
float minIndex = df.index.min();
15+
float maxIndex = df.index.max();
16+
int columnCount = df.columns.Count;
17+
18+
StringBuilder sb = new();
19+
sb.AppendLine("**Dataframe Info**");
20+
sb.AppendLine($"Range Index: {indexLength} entries, {minIndex} to {maxIndex}");
21+
sb.AppendLine($"Data Columns: (total {columnCount} columns)");
22+
Console.WriteLine(sb.ToString());
23+
24+
25+
DataTable table = new DataTable();
26+
table.Columns.Add("#", typeof(int));
27+
table.Columns.Add("Column", typeof(string));
28+
table.Columns.Add("Non-Null Count", typeof(int));
29+
table.Columns.Add("DType", typeof(string));
30+
31+
for (int i = 0; i < df.columns.Count; i++)
32+
{
33+
table.Rows.Add(i, df.columns[i].Name, df[df.columns[i].Name].count(), df[df.columns[i].Name].dtype ?? typeof(String));
34+
}
35+
Console.Write(RenderDataTable(table));
36+
}
37+
38+
/// <summary>
39+
/// Renders a DataTable to a markdown table
40+
/// </summary>
41+
/// <remarks>Code from StackOverflow contributor david-liebeherr</remarks>
42+
/// <param name="table"></param>
43+
/// <returns></returns>
44+
public static string RenderDataTable(DataTable table)
45+
{
46+
String GetCellValueAsString(DataRow row, DataColumn column)
47+
{
48+
var cellValue = row[column];
49+
var cellValueAsString = cellValue is null or DBNull ? "{{null}}" : cellValue.ToString();
50+
51+
return cellValueAsString;
52+
}
53+
54+
var columnWidths = new Dictionary<DataColumn, Int32>();
55+
56+
foreach (DataColumn column in table.Columns)
57+
{
58+
columnWidths.Add(column, column.ColumnName.Length);
59+
}
60+
61+
foreach (DataRow row in table.Rows)
62+
{
63+
foreach (DataColumn column in table.Columns)
64+
{
65+
columnWidths[column] = Math.Max(columnWidths[column], GetCellValueAsString(row, column).Length);
66+
}
67+
}
68+
69+
var resultBuilder = new StringBuilder();
70+
71+
resultBuilder.Append("| ");
72+
73+
foreach (DataColumn column in table.Columns)
74+
{
75+
resultBuilder.Append(column.ColumnName.PadRight(columnWidths[column]));
76+
resultBuilder.Append(" | ");
77+
}
78+
79+
resultBuilder.AppendLine();
80+
resultBuilder.Append("| ");
81+
foreach (DataColumn column in table.Columns)
82+
{
83+
resultBuilder.Append("-".PadRight(columnWidths[column], '-'));
84+
resultBuilder.Append(" | ");
85+
}
86+
resultBuilder.AppendLine();
87+
88+
foreach (DataRow row in table.Rows)
89+
{
90+
resultBuilder.Append("| ");
91+
92+
foreach (DataColumn column in table.Columns)
93+
{
94+
resultBuilder.Append(GetCellValueAsString(row, column).PadRight(columnWidths[column]));
95+
resultBuilder.Append(" | ");
96+
}
97+
98+
resultBuilder.AppendLine();
99+
}
100+
101+
return resultBuilder.ToString();
102+
}
103+
}

0 commit comments

Comments
 (0)