-
Notifications
You must be signed in to change notification settings - Fork 1.9k
IDataView to DataFrame #5712
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
Merged
IDataView to DataFrame #5712
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b982eed
IDataView -> DataFrame
a8f6ad7
More APIs and unit tests
cb3c28d
ANother unit test
90cc62c
Address feedback
1260e22
Last bit of feedback
96bb44a
Fix some stuff and unit tests
9d9f224
sq
1ce802b
Move RowCursor back
1203495
Remove unused param
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,115 @@ | ||
| // 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 Microsoft.ML; | ||
| using Microsoft.ML.Data; | ||
|
|
||
| namespace Microsoft.Data.Analysis | ||
| { | ||
| public static class IDataViewExtensions | ||
| { | ||
| public static DataFrame ToDataFrame(this IDataView dataView, long maxRows = -1) | ||
|
pgovind marked this conversation as resolved.
Outdated
|
||
| { | ||
| return ToDataFrame(dataView, maxRows, null); | ||
| } | ||
|
|
||
| public static DataFrame ToDataFrame(this IDataView dataView, params string[] selectColumns) | ||
| { | ||
| return ToDataFrame(dataView, -1, selectColumns); | ||
| } | ||
|
|
||
| public static DataFrame ToDataFrame(this IDataView dataView, long maxRows, params string[] selectColumns) | ||
| { | ||
| DataViewSchema schema = dataView.Schema; | ||
| List<DataFrameColumn> columns = new List<DataFrameColumn>(schema.Count); | ||
|
|
||
| HashSet<string> selectColumnsSet = null; | ||
| if (selectColumns != null && selectColumns.Length > 0) | ||
| { | ||
| selectColumnsSet = new HashSet<string>(selectColumns); | ||
| } | ||
|
|
||
| List<DataViewSchema.Column> activeColumns = new List<DataViewSchema.Column>(); | ||
| foreach (DataViewSchema.Column column in schema) | ||
| { | ||
| long length = maxRows >= 0 ? maxRows : long.MaxValue; | ||
| length = Math.Min(length, dataView.GetRowCount() ?? 0); | ||
|
pgovind marked this conversation as resolved.
Outdated
|
||
| if (column.IsHidden || (selectColumnsSet != null && !selectColumnsSet.Contains(column.Name))) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| activeColumns.Add(column); | ||
| DataViewType type = column.Type; | ||
| if (type == BooleanDataViewType.Instance) | ||
| { | ||
| columns.Add(new BooleanDataFrameColumn(column.Name, length)); | ||
| } | ||
| else if (type == NumberDataViewType.Byte) | ||
| { | ||
| columns.Add(new ByteDataFrameColumn(column.Name, length)); | ||
| } | ||
| else if (type == NumberDataViewType.Double) | ||
| { | ||
| columns.Add(new DoubleDataFrameColumn(column.Name, length)); | ||
| } | ||
| else if (type == NumberDataViewType.Single) | ||
| { | ||
| columns.Add(new SingleDataFrameColumn(column.Name, length)); | ||
| } | ||
| else if (type == NumberDataViewType.Int32) | ||
| { | ||
| columns.Add(new Int32DataFrameColumn(column.Name, length)); | ||
| } | ||
| else if (type == NumberDataViewType.Int64) | ||
| { | ||
| columns.Add(new Int64DataFrameColumn(column.Name, length)); | ||
| } | ||
| else if (type == NumberDataViewType.SByte) | ||
| { | ||
| columns.Add(new SByteDataFrameColumn(column.Name, length)); | ||
| } | ||
| else if (type == NumberDataViewType.Int16) | ||
| { | ||
| columns.Add(new Int16DataFrameColumn(column.Name, length)); | ||
| } | ||
| else if (type == NumberDataViewType.UInt32) | ||
| { | ||
| columns.Add(new UInt32DataFrameColumn(column.Name, length)); | ||
| } | ||
| else if (type == NumberDataViewType.UInt64) | ||
| { | ||
| columns.Add(new UInt64DataFrameColumn(column.Name, length)); | ||
| } | ||
| else if (type == NumberDataViewType.UInt16) | ||
| { | ||
| columns.Add(new UInt16DataFrameColumn(column.Name, length)); | ||
| } | ||
| else if (type == TextDataViewType.Instance) | ||
| { | ||
| columns.Add(new StringDataFrameColumn(column.Name, length)); | ||
| } | ||
| else | ||
| { | ||
| throw new NotSupportedException(nameof(type)); | ||
|
pgovind marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| DataFrame ret = new DataFrame(columns); | ||
| DataViewRowCursor cursor = dataView.GetRowCursor(activeColumns); | ||
|
pgovind marked this conversation as resolved.
Outdated
|
||
| while (cursor.MoveNext()) | ||
| { | ||
| foreach (DataViewSchema.Column column in activeColumns) | ||
| { | ||
| ret[column.Name].AddValueUsingCursor(cursor, column); | ||
|
pgovind marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
| } | ||
|
|
||
| } | ||
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
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.