Skip to content

Commit 18da9fe

Browse files
authored
Fix Logical binary operations not supported exception (#7093)
1 parent d507a1e commit 18da9fe

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/Microsoft.Data.Analysis/PrimitiveDataFrameColumn.cs

+7
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,13 @@ internal DataFrameColumn HandleOperationImplementation<U>(BinaryOperation operat
892892
switch (typeof(T))
893893
{
894894
case Type boolType when boolType == typeof(bool):
895+
if (typeof(U) == typeof(bool))
896+
{
897+
PrimitiveDataFrameColumn<U> primitiveColumn = this as PrimitiveDataFrameColumn<U>;
898+
var newColumn = inPlace ? primitiveColumn : primitiveColumn.Clone();
899+
newColumn._columnContainer.HandleOperation(operation, column._columnContainer);
900+
return newColumn;
901+
}
895902
throw new NotSupportedException();
896903
case Type decimalType when decimalType == typeof(decimal):
897904
if (typeof(U) == typeof(bool))

test/Microsoft.Data.Analysis.Tests/DataFrameTests.Computations.cs

+34
Original file line numberDiff line numberDiff line change
@@ -473,5 +473,39 @@ public void TestComputations_WithNegativeNumbers_MaxMin_Calculated(int startingF
473473
// We need to iterate over all range with conversion to ushort due to negative numbers issue
474474
Assert.Equal((ushort)ushortColumn.Min(), range.Select(x => (ushort)x).Min());
475475
}
476+
477+
[Fact]
478+
public void Test_Logical_Computation_And()
479+
{
480+
var col1 = new BooleanDataFrameColumn("col1", new Boolean[] { true, false, true });
481+
var col2 = new BooleanDataFrameColumn("col2", new Boolean[] { false, true, true });
482+
var dfTest = new DataFrame(col1, col2);
483+
var col3 = dfTest["col1"].And(dfTest["col2"]);
484+
var col4 = col1.And(col2);
485+
486+
for (int i = 0; i < col1.Length; i++)
487+
{
488+
var exprectedValue = col1[i] & col2[i];
489+
Assert.Equal(exprectedValue, col3[i]);
490+
Assert.Equal(exprectedValue, col4[i]);
491+
}
492+
}
493+
494+
[Fact]
495+
public void Test_Logical_Computation_Or()
496+
{
497+
var col1 = new BooleanDataFrameColumn("col1", new Boolean[] { true, false, true });
498+
var col2 = new BooleanDataFrameColumn("col2", new Boolean[] { false, true, true });
499+
var dfTest = new DataFrame(col1, col2);
500+
var col3 = dfTest["col1"].Or(dfTest["col2"]);
501+
var col4 = col1.Or(col2);
502+
503+
for (int i = 0; i < col1.Length; i++)
504+
{
505+
var exprectedValue = col1[i] | col2[i];
506+
Assert.Equal(exprectedValue, col3[i]);
507+
Assert.Equal(exprectedValue, col4[i]);
508+
}
509+
}
476510
}
477511
}

0 commit comments

Comments
 (0)