Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Microsoft.Data.Analysis.DataFrame C# Samples to use Plotly.NET… #3380

Merged
merged 2 commits into from
Jan 5, 2024
Merged
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
134 changes: 39 additions & 95 deletions samples/notebooks/csharp/Samples/DataFrame-Getting Started.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,59 +18,15 @@
"languageId": "dotnet-interactive.csharp"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div><div><strong>Restore sources</strong><ul><li><span>https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json</span></li><li><span>https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json</span></li></ul></div><div></div><div><strong>Installed Packages</strong><ul><li><span>Microsoft.Data.Analysis, 0.19.0</span></li><li><span>XPlot.Plotly.Interactive, 4.0.6</span></li></ul></div></div>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/markdown": [
"Loading extensions from `XPlot.Plotly.Interactive.dll`"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/markdown": [
"Configuring PowerShell Kernel for XPlot.Plotly integration."
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/markdown": [
"Installed support for XPlot.Plotly."
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/markdown": [
"Loading extensions from `Microsoft.Data.Analysis.Interactive.dll`"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"outputs": [],
"source": [
"#i \"nuget:https://api.nuget.org/v3/index.json\" \n",
"#i \"nuget:https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json\" \n",
"#i \"nuget:https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json\" \n",
"\n",
"#r \"nuget:Microsoft.Data.Analysis,0.19.0\"\n",
"#r \"nuget:XPlot.Plotly.Interactive, 4.0.6\"\n",
"#r \"nuget:Microsoft.Data.Analysis, 0.21.0\"\n",
"#r \"nuget: Plotly.NET.Interactive, 4.2.0\"\n",
"#r \"nuget: Plotly.Net, 4.2.0\"\n",
"\n",
"using Microsoft.Data.Analysis;"
]
Expand All @@ -88,9 +44,9 @@
},
"outputs": [],
"source": [
"PrimitiveDataFrameColumn<DateTime> dateTimes = new PrimitiveDataFrameColumn<DateTime>(\"DateTimes\"); // Default length is 0.\n",
"PrimitiveDataFrameColumn<int> ints = new PrimitiveDataFrameColumn<int>(\"Ints\", 3); // Makes a column of length 3. Filled with nulls initially\n",
"StringDataFrameColumn strings = new StringDataFrameColumn(\"Strings\", 3); // Makes a column of length 3. Filled with nulls initially"
"DateTimeDataFrameColumn dateTimes = new DateTimeDataFrameColumn(\"DateTimes\"); // Default length is 0.\n",
"Int32DataFrameColumn ints = new Int32DataFrameColumn(\"Ints\", 6); // Makes a column of length 3. Filled with nulls initially\n",
"StringDataFrameColumn strings = new StringDataFrameColumn(\"Strings\", 6); // Makes a column of length 3. Filled with nulls initially"
]
},
{
Expand All @@ -106,10 +62,13 @@
},
"outputs": [],
"source": [
"// Append 3 values to dateTimes\n",
"// Append 6 values to dateTimes\n",
"dateTimes.Append(DateTime.Parse(\"2019/01/01\"));\n",
"dateTimes.Append(DateTime.Parse(\"2019/01/01\"));\n",
"dateTimes.Append(DateTime.Parse(\"2019/01/02\"));"
"dateTimes.Append(DateTime.Parse(\"2019/01/02\"));\n",
"dateTimes.Append(DateTime.Parse(\"2019/02/02\"));\n",
"dateTimes.Append(DateTime.Parse(\"2019/02/02\"));\n",
"dateTimes.Append(DateTime.Parse(\"2019/03/02\"));"
]
},
{
Expand Down Expand Up @@ -176,7 +135,7 @@
"outputs": [],
"source": [
"// Modify ints and strings columns by indexing\n",
"ints[1] = 100;\n",
"ints[1] = 24;\n",
"strings[1] = \"Foo!\";\n",
"df"
]
Expand Down Expand Up @@ -232,7 +191,7 @@
"outputs": [],
"source": [
"// We can also use binary operators. Binary operators produce a copy, so assign it back to our Ints column \n",
"df[\"Ints\"] = (ints / 5) * 100;\n",
"df[\"Ints\"] = (ints / 5) * 20;\n",
"df"
]
},
Expand All @@ -250,7 +209,7 @@
"outputs": [],
"source": [
"// Fill nulls in our columns, if any. Ints[2], Strings[0] and Strings[1] are null\n",
"df[\"Ints\"].FillNulls(-1, inPlace: true);\n",
"df[\"Ints\"].FillNulls(100, inPlace: true);\n",
"df[\"Strings\"].FillNulls(\"Bar\", inPlace: true);\n",
"df"
]
Expand Down Expand Up @@ -339,11 +298,12 @@
},
"outputs": [],
"source": [
"// GroupBy \n",
"GroupBy groupBy = df.GroupBy(\"DateTimes\");\n",
"// Count of values in each group\n",
"DataFrame groupCounts = groupBy.Count();\n",
"groupCounts"
"//Clone dataframe\n",
"var newDf = df.Clone();\n",
"\n",
"//Add new column\n",
"StringDataFrameColumn newColumn = new StringDataFrameColumn(\"Month\", ((DateTimeDataFrameColumn)df[\"DateTimes\"]).Select(x => x.Value.ToString(\"MMMM\")));\n",
"newDf.Columns.Add(newColumn);"
]
},
{
Expand All @@ -359,9 +319,12 @@
},
"outputs": [],
"source": [
"// Alternatively find the sum of the values in each group in Ints\n",
"DataFrame intsGroupSum = groupBy.Sum(\"Ints\");\n",
"intsGroupSum"
"// GroupBy month\n",
"GroupBy groupBy = newDf.GroupBy(\"Month\");\n",
"\n",
"//Show grouped data\n",
"DataFrame groupedDf = groupBy.Head(10);\n",
"groupedDf"
]
},
{
Expand All @@ -377,8 +340,9 @@
},
"outputs": [],
"source": [
"using XPlot.Plotly;\n",
"using System.Linq;"
"// Count of values in each group\n",
"DataFrame groupCounts = groupBy.Count();\n",
"groupCounts"
]
},
{
Expand All @@ -394,7 +358,9 @@
},
"outputs": [],
"source": [
"#r \"nuget:MathNet.Numerics,4.9.0\""
"// Alternatively find the sum of the values in each group in Ints\n",
"DataFrame intsGroupSum = groupBy.Sum(\"Ints\");\n",
"intsGroupSum"
]
},
{
Expand All @@ -410,11 +376,9 @@
},
"outputs": [],
"source": [
"using MathNet.Numerics.Distributions;\n",
"double mean = 0;\n",
"double stdDev = 0.1;\n",
"\n",
"MathNet.Numerics.Distributions.Normal normalDist = new Normal(mean, stdDev);"
"using Plotly.NET;\n",
"using System.Linq;\n",
"using Microsoft.FSharp.Core;"
]
},
{
Expand All @@ -430,29 +394,9 @@
},
"outputs": [],
"source": [
"PrimitiveDataFrameColumn<double> doubles = new PrimitiveDataFrameColumn<double>(\"Normal Distribution\", normalDist.Samples().Take(1000));\n",
"display(Chart.Plot(\n",
" new Histogram()\n",
" {\n",
" x = doubles,\n",
" nbinsx = 30\n",
" }\n",
"));"
"display(Chart2D.Chart.Column<int, string, string, string, string>(\n",
" ((Int32DataFrameColumn)intsGroupSum[\"Ints\"]).Select(x => x ?? default), new FSharpOption<IEnumerable<string>>(((StringDataFrameColumn)intsGroupSum[\"Month\"]))));"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"vscode": {
"languageId": "dotnet-interactive.csharp"
}
},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down