Skip to content

Commit a747939

Browse files
committed
Fix PandasColumn equality wrongly matching non-string values
1 parent 24afc50 commit a747939

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

Common/PandasMapper.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ def __new__(cls, key):
3737

3838
def __eq__(self, other):
3939
# We need this since Lean created data frames might contain Symbol objects in the indexes
40-
return super().__eq__(other) and type(other) is not Symbol
40+
if type(other) is Symbol:
41+
return False
42+
# For non-strings str.__eq__ returns NotImplemented, which Python resolves to False
43+
return super().__eq__(other)
4144

4245
def __hash__(self):
4346
return super().__hash__()

Tests/Python/PandasIndexingTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,17 @@ public void ExpectedException()
8585
Assert.IsTrue(exception.Contains("No key found for either mapped or original key.", StringComparison.InvariantCulture), exception);
8686
}
8787
}
88+
89+
[Test]
90+
public void ColumnEqualsOnlyMatchingString()
91+
{
92+
using (Py.GIL())
93+
{
94+
PyObject result = _pandasDataFrameTests.test_column_equals_only_matching_string();
95+
var test = result.As<bool>();
96+
97+
Assert.IsTrue(test);
98+
}
99+
}
88100
}
89101
}

Tests/Python/PandasTests/PandasIndexingTests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from AlgorithmImports import *
1515
from QuantConnect.Tests import *
1616
from QuantConnect.Tests.Python import *
17+
from PandasMapper import PandasColumn
1718

1819
# TODO: Rename to PandasResearchTests and keep this class for QB related tests; rename py module to PandasTests
1920
class PandasIndexingTests():
@@ -68,3 +69,8 @@ def test_contains_user_defined_columns_with_spaces(self, column_name):
6869
return True
6970
except:
7071
return False
72+
73+
def test_column_equals_only_matching_string(self):
74+
# A column label should only equal a matching string, never None/ints/floats
75+
column = PandasColumn("shares")
76+
return (not (column == None)) and (not (column == 0)) and (not (column == 123)) and (column == "shares")

0 commit comments

Comments
 (0)