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

DataFrame: Add DateTime column type #6302

Merged
merged 9 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@
new TypeConfiguration("short", unsupportedMethods: new[] {"All", "Any"}),
new TypeConfiguration("uint", classPrefix:"UInt", unsupportedMethods: new[] {"UnaryMinus", "All", "Any"}),
new TypeConfiguration("ulong", classPrefix:"ULong", unsupportedMethods: new[] {"UnaryMinus", "All", "Any"}),
new TypeConfiguration("ushort", classPrefix:"UShort", unsupportedMethods: new[] {"UnaryMinus", "All", "Any"})
new TypeConfiguration("ushort", classPrefix:"UShort", unsupportedMethods: new[] {"UnaryMinus", "All", "Any"}),
new TypeConfiguration("DateTime", supportsBitwise: false, supportsNumeric: false, unsupportedMethods: new[] {"And", "Or", "Xor"})
};

public string GetBinaryShiftOperationReturnType(TypeConfiguration t1)
Expand Down
23 changes: 23 additions & 0 deletions src/Microsoft.Data.Analysis/DateTimeDataFrameColumn.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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.Text;

namespace Microsoft.Data.Analysis
{
public partial class DateTimeDataFrameColumn : PrimitiveDataFrameColumn<DateTime>
{
public DateTimeDataFrameColumn(string name, IEnumerable<DateTime?> values) : base(name, values) { }

public DateTimeDataFrameColumn(string name, IEnumerable<DateTime> values) : base(name, values) { }

public DateTimeDataFrameColumn(string name, long length = 0) : base(name, length) { }

public DateTimeDataFrameColumn(string name, ReadOnlyMemory<byte> buffer, ReadOnlyMemory<byte> nullBitMap, int length = 0, int nullCount = 0) : base(name, buffer, nullBitMap, length, nullCount) { }

internal DateTimeDataFrameColumn(string name, PrimitiveColumnContainer<DateTime> values) : base(name, values) { }
}
}
4 changes: 4 additions & 0 deletions src/Microsoft.Data.Analysis/IDataView.Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public static DataFrame ToDataFrame(this IDataView dataView, long maxRows, param
{
dataFrameColumns.Add(new BooleanDataFrameColumn(dataViewColumn.Name));
}
else if (type == DateTimeDataViewType.Instance)
{
dataFrameColumns.Add(new DateTimeDataFrameColumn(dataViewColumn.Name));
}
else if (type == NumberDataViewType.Byte)
{
dataFrameColumns.Add(new ByteDataFrameColumn(dataViewColumn.Name));
Expand Down
130 changes: 118 additions & 12 deletions src/Microsoft.Data.Analysis/PrimitiveDataFrameColumn.BinaryOperations.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -169,32 +169,32 @@ namespace Microsoft.Data.Analysis
switch (typeof(T))
{
<# foreach (TypeConfiguration type in typeConfiguration) { #>
<# if (type.TypeName == "bool") { #>
<# if (type.TypeName == "bool" || type.TypeName == "DateTime") { #>
case Type <#=type.TypeName#>Type when <#=type.TypeName#>Type == typeof(<#=type.TypeName#>):
<# if (method.IsNumeric == true) { #>
throw new NotSupportedException();
<# } else { #>
if (typeof(U) != typeof(bool))
if (typeof(U) != typeof(<#=type.TypeName#>))
{
throw new NotSupportedException();
}
<# if (method.MethodType == MethodType.ComparisonScalar || method.MethodType == MethodType.Comparison) { #>
PrimitiveDataFrameColumn<bool> retColumn = CloneAsBooleanColumn();
PrimitiveDataFrameColumn<bool> ret<#=type.TypeName#>Column = CloneAsBooleanColumn();
beccamc marked this conversation as resolved.
Show resolved Hide resolved
<# if (method.MethodType == MethodType.ComparisonScalar) { #>
(this as PrimitiveDataFrameColumn<bool>)._columnContainer.<#=method.MethodName#>(Unsafe.As<U, bool>(ref value), retColumn._columnContainer);
(this as PrimitiveDataFrameColumn<bool>)._columnContainer.<#=method.MethodName#>(Unsafe.As<U, bool>(ref value), ret<#=type.TypeName#>Column._columnContainer);
<# } else { #>
(this as PrimitiveDataFrameColumn<U>)._columnContainer.<#=method.MethodName#>(column._columnContainer, retColumn._columnContainer);
(this as PrimitiveDataFrameColumn<U>)._columnContainer.<#=method.MethodName#>(column._columnContainer, ret<#=type.TypeName#>Column._columnContainer);
<# } #>
<# } else if (method.MethodType == MethodType.BinaryScalar) {#>
PrimitiveDataFrameColumn<U> column = this as PrimitiveDataFrameColumn<U>;
PrimitiveDataFrameColumn<U> retColumn = <#=GenerateInPlaceStatement("column", "column.Clone()")#>;
retColumn._columnContainer.<#=method.MethodName#>(value);
PrimitiveDataFrameColumn<U> ret<#=type.TypeName#>Column = <#=GenerateInPlaceStatement("column", "column.Clone()")#>;
ret<#=type.TypeName#>Column._columnContainer.<#=method.MethodName#>(value);
<# } else { #>
PrimitiveDataFrameColumn<U> column = this as PrimitiveDataFrameColumn<U>;
PrimitiveDataFrameColumn<U> retColumn = <#=GenerateInPlaceStatement("column", "column.Clone()")#>;
retColumn._columnContainer.<#=method.MethodName#>(column._columnContainer);
PrimitiveDataFrameColumn<U> ret<#=type.TypeName#>Column = <#=GenerateInPlaceStatement("column", "column.Clone()")#>;
ret<#=type.TypeName#>Column._columnContainer.<#=method.MethodName#>(column._columnContainer);
<# } #>
return retColumn;
return ret<#=type.TypeName#>Column;
<# } #>
<# } else if (type.TypeName == "decimal") { #>
case Type <#=type.TypeName#>Type when <#=type.TypeName#>Type == typeof(<#=type.TypeName#>):
Expand Down Expand Up @@ -231,7 +231,7 @@ namespace Microsoft.Data.Analysis
return newColumn;
<# } #>
}
else
else
{
<# if (method.MethodType == MethodType.BinaryScalar) { #>
if (inPlace)
Expand Down Expand Up @@ -314,7 +314,7 @@ namespace Microsoft.Data.Analysis
return newColumn;
<# } #>
}
else
else
{
<# if (method.MethodType == MethodType.BinaryScalar) { #>
if (inPlace)
Expand Down
13 changes: 10 additions & 3 deletions src/Microsoft.Data.Analysis/PrimitiveDataFrameColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public override long NullCount
public override bool IsNumericColumn()
{
bool ret = true;
if (typeof(T) == typeof(char) || typeof(T) == typeof(bool))
if (typeof(T) == typeof(char) || typeof(T) == typeof(bool) || typeof(T) == typeof(DateTime))
ret = false;
return ret;
}
Expand Down Expand Up @@ -325,7 +325,10 @@ public override DataFrame ValueCounts()
}

/// <inheritdoc/>
public override bool HasDescription() => IsNumericColumn();
public override bool HasDescription()
{
return this.IsNumericColumn() || typeof(T) == typeof(DateTime);
}

/// <summary>
/// Returns a preview of the column contents as a formatted string.
Expand Down Expand Up @@ -701,6 +704,10 @@ private static DataViewType GetDataViewType()
{
return NumberDataViewType.Double;
}
else if (typeof(T) == typeof(DateTime))
{
return DateTimeDataViewType.Instance;
}
else if (typeof(T) == typeof(float))
{
return NumberDataViewType.Single;
Expand Down Expand Up @@ -744,7 +751,7 @@ private static DataViewType GetDataViewType()
return NumberDataViewType.Double;
}

throw new NotSupportedException();
throw new NotSupportedException("Type is " + typeof(T).Name);
}

protected internal override Delegate GetDataViewGetter(DataViewRowCursor cursor)
Expand Down
Loading