-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Feature/5820 dataframe extend groupby #5821
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
Merged
pgovind
merged 8 commits into
dotnet:main
from
asmirnov82:feature/5820_dataframe_extend_groupby
Jun 3, 2021
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c527c21
fix #5767 issue with DataFrame Merge method
a38fdf5
Merge pull request #1 from dotnet/main
e5f5eeb
Merge pull request #2 from dotnet/main
0580710
#5820 Extend DataFrame GroupBy operations
f7658b2
#5820 fix code review findings
2c170dc
#5820 fix code review findings
4afefd0
Update src/Microsoft.Data.Analysis/GroupBy.cs
c4d3ad2
Fix remaining comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
test/Microsoft.Data.Analysis.Tests/DataFrameGroupByTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| // 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. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Data.Analysis.Tests | ||
| { | ||
| public class DataFrameGroupByTests | ||
| { | ||
| [Fact] | ||
| public void TestGroupingWithTKeyTypeofString() | ||
| { | ||
| const int length = 11; | ||
|
|
||
| //Create test dataframe (numbers starting from 0 up to lenght) | ||
| DataFrame df = MakeTestDataFrameWithParityAndTensColumns(length); | ||
|
|
||
| var grouping = df.GroupBy<string>("Parity").Groupings; | ||
|
|
||
| //Check groups count | ||
| Assert.Equal(2, grouping.Count()); | ||
|
|
||
| //Check number of elements in each group | ||
| var oddGroup = grouping.Where(gr => gr.Key == "odd").FirstOrDefault(); | ||
| Assert.NotNull(oddGroup); | ||
| Assert.Equal(length/2, oddGroup.Count()); | ||
|
|
||
| var evenGroup = grouping.Where(gr => gr.Key == "even").FirstOrDefault(); | ||
| Assert.NotNull(evenGroup); | ||
| Assert.Equal(length / 2 + length % 2, evenGroup.Count()); | ||
|
|
||
|
|
||
| } | ||
|
|
||
| [Fact] | ||
| public void TestGroupingWithTKey_CornerCases() | ||
| { | ||
| //Check corner cases | ||
| var df = MakeTestDataFrameWithParityAndTensColumns(0); | ||
| var grouping = df.GroupBy<string>("Parity").Groupings; | ||
| Assert.Empty(grouping); | ||
|
|
||
|
|
||
| df = MakeTestDataFrameWithParityAndTensColumns(1); | ||
| grouping = df.GroupBy<string>("Parity").Groupings; | ||
| Assert.Single(grouping); | ||
| Assert.Equal("even", grouping.First().Key); | ||
| } | ||
|
|
||
|
|
||
| [Fact] | ||
| public void TestGroupingWithTKeyPrimitiveType() | ||
| { | ||
| const int length = 55; | ||
|
|
||
| //Create test dataframe (numbers starting from 0 up to lenght) | ||
| DataFrame df = MakeTestDataFrameWithParityAndTensColumns(length); | ||
|
|
||
| //Group elements by int column, that contain the amount of full tens in each int | ||
| var groupings = df.GroupBy<int>("Tens").Groupings.ToDictionary(g => g.Key, g => g.ToList()); | ||
|
|
||
| //Get the amount of all number based columns | ||
| int numberColumnsCount = df.Columns.Count - 2; //except "Parity" and "Tens" columns | ||
|
|
||
| //Check each group | ||
| for (int i = 0; i < length / 10; i++) | ||
| { | ||
| Assert.Equal(10, groupings[i].Count()); | ||
|
|
||
| var rows = groupings[i]; | ||
| for (int colIndex = 0; colIndex < numberColumnsCount; colIndex++) | ||
| { | ||
| var values = rows.Select(row => Convert.ToInt32(row[colIndex])); | ||
|
|
||
| for (int j = 0; j < 10; j++) | ||
| { | ||
| Assert.Contains(i * 10 + j, values); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| //Last group should contain smaller amount of items | ||
| Assert.Equal(length % 10, groupings[length / 10].Count()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TestGroupingWithTKeyOfWrongType() | ||
| { | ||
|
|
||
| var message = string.Empty; | ||
|
|
||
| //Create test dataframe (numbers starting from 0 up to lenght) | ||
| DataFrame df = MakeTestDataFrameWithParityAndTensColumns(1); | ||
|
|
||
| //Use wrong type for grouping | ||
| Assert.Throws<InvalidCastException>(() => df.GroupBy<double>("Tens")); | ||
| } | ||
|
|
||
|
|
||
| private DataFrame MakeTestDataFrameWithParityAndTensColumns(int length) | ||
| { | ||
| DataFrame df = DataFrameTests.MakeDataFrameWithNumericColumns(length, false); | ||
| DataFrameColumn parityColumn = new StringDataFrameColumn("Parity", Enumerable.Range(0, length).Select(x => x % 2 == 0 ? "even" : "odd")); | ||
| DataFrameColumn tensColumn = new Int32DataFrameColumn("Tens", Enumerable.Range(0, length).Select(x => x / 10)); | ||
| df.Columns.Insert(df.Columns.Count, parityColumn); | ||
| df.Columns.Insert(df.Columns.Count, tensColumn); | ||
|
|
||
| return df; | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.