-
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 4 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
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,32 @@ | ||
| using System; | ||
|
pgovind marked this conversation as resolved.
Outdated
|
||
| using System.Collections; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
|
|
||
| namespace Microsoft.Data.Analysis | ||
| { | ||
| public class Grouping<TKey> : IGrouping<TKey, DataFrameRow> | ||
|
pgovind marked this conversation as resolved.
Outdated
|
||
| { | ||
| private readonly TKey _key; | ||
| private readonly ICollection<DataFrameRow> _rows; | ||
|
|
||
| public Grouping(TKey key, ICollection<DataFrameRow> rows) | ||
| { | ||
| _key = key; | ||
| _rows = rows; | ||
| } | ||
|
|
||
| public TKey Key => _key; | ||
|
|
||
| public IEnumerator<DataFrameRow> GetEnumerator() | ||
| { | ||
| return _rows.GetEnumerator(); | ||
| } | ||
|
|
||
| IEnumerator IEnumerable.GetEnumerator() | ||
| { | ||
| return _rows.GetEnumerator(); | ||
| } | ||
| } | ||
| } | ||
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
107 changes: 107 additions & 0 deletions
107
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,107 @@ | ||
| 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() | ||
| { | ||
| int lenght = 11; | ||
|
pgovind marked this conversation as resolved.
Outdated
|
||
|
|
||
| //Create test dataframe (numbers starting from 0 up to lenght) | ||
| DataFrame df = MakeTestDataFrame(lenght); | ||
|
|
||
| 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(lenght/2, oddGroup.Count()); | ||
|
|
||
| var evenGroup = grouping.Where(gr => gr.Key == "even").FirstOrDefault(); | ||
| Assert.NotNull(evenGroup); | ||
| Assert.Equal(lenght / 2 + lenght % 2, evenGroup.Count()); | ||
|
|
||
| //Check corner cases | ||
| lenght = 0; | ||
| df = MakeTestDataFrame(lenght); | ||
| grouping = df.GroupBy<string>("Parity").Groupings; | ||
| Assert.Empty(grouping); | ||
|
|
||
| lenght = 1; | ||
| df = MakeTestDataFrame(lenght); | ||
| grouping = df.GroupBy<string>("Parity").Groupings; | ||
| Assert.Single(grouping); | ||
| Assert.Equal("even", grouping.First().Key); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TestGroupingWithTKeyPrimitiveType() | ||
| { | ||
| const int lenght = 55; | ||
|
|
||
| //Create test dataframe (numbers starting from 0 up to lenght) | ||
| DataFrame df = MakeTestDataFrame(lenght); | ||
|
|
||
| //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 < lenght / 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(lenght % 10, groupings[lenght / 10].Count()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TestGroupingWithTKeyOfWrongType() | ||
| { | ||
| const int lenght = 5; | ||
|
|
||
| var message = string.Empty; | ||
|
|
||
| //Create test dataframe (numbers starting from 0 up to lenght) | ||
| DataFrame df = MakeTestDataFrame(lenght); | ||
|
|
||
| //Use wrong type for grouping | ||
| Assert.Throws<InvalidCastException>(() => df.GroupBy<double>("Tens")); | ||
| } | ||
|
|
||
|
|
||
| private DataFrame MakeTestDataFrame(int length) | ||
|
asmirnov82 marked this conversation as resolved.
Outdated
|
||
| { | ||
| 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.