-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
IDataView to DataFrame #5712
Merged
Merged
IDataView to DataFrame #5712
Changes from 8 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 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 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 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,126 @@ | ||
// 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.Data.Analysis; | ||
using Microsoft.ML.Data; | ||
|
||
namespace Microsoft.ML | ||
{ | ||
public static class IDataViewExtensions | ||
{ | ||
private const int defaultMaxRows = 100; | ||
|
||
public static DataFrame ToDataFrame(this IDataView dataView, long maxRows = defaultMaxRows) | ||
{ | ||
return ToDataFrame(dataView, maxRows, null); | ||
} | ||
|
||
public static DataFrame ToDataFrame(this IDataView dataView, params string[] selectColumns) | ||
{ | ||
return ToDataFrame(dataView, defaultMaxRows, 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) | ||
{ | ||
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)); | ||
} | ||
else if (type == NumberDataViewType.Byte) | ||
{ | ||
columns.Add(new ByteDataFrameColumn(column.Name)); | ||
} | ||
else if (type == NumberDataViewType.Double) | ||
{ | ||
columns.Add(new DoubleDataFrameColumn(column.Name)); | ||
} | ||
else if (type == NumberDataViewType.Single) | ||
{ | ||
columns.Add(new SingleDataFrameColumn(column.Name)); | ||
} | ||
else if (type == NumberDataViewType.Int32) | ||
{ | ||
columns.Add(new Int32DataFrameColumn(column.Name)); | ||
} | ||
else if (type == NumberDataViewType.Int64) | ||
{ | ||
columns.Add(new Int64DataFrameColumn(column.Name)); | ||
} | ||
else if (type == NumberDataViewType.SByte) | ||
{ | ||
columns.Add(new SByteDataFrameColumn(column.Name)); | ||
} | ||
else if (type == NumberDataViewType.Int16) | ||
{ | ||
columns.Add(new Int16DataFrameColumn(column.Name)); | ||
} | ||
else if (type == NumberDataViewType.UInt32) | ||
{ | ||
columns.Add(new UInt32DataFrameColumn(column.Name)); | ||
} | ||
else if (type == NumberDataViewType.UInt64) | ||
{ | ||
columns.Add(new UInt64DataFrameColumn(column.Name)); | ||
} | ||
else if (type == NumberDataViewType.UInt16) | ||
{ | ||
columns.Add(new UInt16DataFrameColumn(column.Name)); | ||
} | ||
else if (type == TextDataViewType.Instance) | ||
{ | ||
columns.Add(new StringDataFrameColumn(column.Name)); | ||
} | ||
else | ||
{ | ||
throw new NotSupportedException(String.Format(Microsoft.Data.Strings.NotSupportedColumnType, type.RawType.Name)); | ||
} | ||
} | ||
|
||
using (DataViewRowCursor cursor = dataView.GetRowCursor(activeColumns)) | ||
{ | ||
Delegate[] activeColumnDelegates = new Delegate[activeColumns.Count]; | ||
int columnIndex = 0; | ||
foreach (DataViewSchema.Column column in activeColumns) | ||
{ | ||
Delegate valueGetter = columns[columnIndex].GetValueGetterUsingCursor(cursor, column); | ||
activeColumnDelegates[columnIndex] = valueGetter; | ||
columnIndex++; | ||
} | ||
while (cursor.MoveNext() && cursor.Position < maxRows) | ||
pgovind marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
columnIndex = 0; | ||
foreach (DataViewSchema.Column column in activeColumns) | ||
pgovind marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
columns[columnIndex].AddValueUsingCursor(cursor, column, activeColumnDelegates[columnIndex]); | ||
columnIndex++; | ||
} | ||
} | ||
} | ||
|
||
return new DataFrame(columns); | ||
} | ||
} | ||
|
||
} |
This file contains 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 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 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will cause a problem for vector types in IDataView I think. We'd need to add support for vector columns in DataFrame to fix this. I'll open a bug
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#5721