Python DataFrame column lookup is nondeterministic when a Symbol column and its ticker string column coexist
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 42/100
Research direction
Start by tracing how Lean’s Python Symbol objects compare and hash when used as pandas DataFrame column labels; the report provides a reproduction using AlgorithmImports, history, and pd.DataFrame. Check existing tests for Symbol equality, hashing, and DataFrame column lookup. Done when df['open'] consistently selects the intended column when a Symbol ticker and matching string column coexist, with a regression test covering the reported case.
Written by the indexing model from the issue text.
Description
Expected Behavior
In Python, a DataFrame that has both a Symbol column and a string column named like that Symbol's ticker returns the same result for df['open'] in every run.
Actual Behavior
With the ticker cached, Symbol == 'open' is True while hash(symbol) != hash('open'). A frame with columns [OPEN Symbol, 'open'] then returns different results from run to run for df['open']: usually a Series, but in some processes a DataFrame with both columns. Python randomizes string hashes per process, which is likely why the outcome changes between processes. Code that builds a mask from it, such as history.loc[lambda d: other['open'] > 0, 'close'], then fails intermittently with ValueError: Cannot index with multidimensional key.
Potential Solution
A suggestion only: Python requires that objects that compare equal have the same hash, which Symbol vs. ticker string doesn't meet. Either the equality or the hash would need to change for these cases.
Reproducing the Problem
from AlgorithmImports import *
class SymbolTickerColumnAmbiguity(QCAlgorithm):
def initialize(self):
spy = self.add_equity("SPY", Resolution.DAILY).symbol
opened = self.add_equity("OPEN", Resolution.DAILY).symbol
history = self.history(spy, 10, Resolution.DAILY)
other = pd.DataFrame({opened: 1.0, 'open': -1.0}, index=history.index)
self.log(f"{opened == 'open'} {hash(opened) == hash('open')} {type(other['open']).__name__}")
The first two values are always True False. The type varies between processes: in ten runs of the same steps as a unit test, other['open'] was a DataFrame once and a Series nine times. The same steps with LOW/low and HIGH/high gave a Series in all ten runs.
System Information
pandas 2.3.3, Python 3.11, current master.
Checklist
- I have completely filled out this template
- I have confirmed that this issue exists on the current
masterbranch - I have confirmed that this is not a duplicate issue by searching issues
- Dominant language
- C#
- Stars
- 22.6k
- Forks
- 5.4k
- Avg merge
- 11h 39m
- Merged PRs (30d)
- 104
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from QuantConnect/Lean
-
priority:p2
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
QuantConnect/Lean#10216 ·
Maintainers usually reply within 1 day
-
priority:p2
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
QuantConnect/Lean#10206 ·
Maintainers usually reply within 1 day
-
priority:p2
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
QuantConnect/Lean#10199 ·
Maintainers usually reply within 1 day
-
priority:p2
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
QuantConnect/Lean#10198 ·
Maintainers usually reply within 1 day
-
priority:p2
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
QuantConnect/Lean#10197 ·
Maintainers usually reply within 1 day
All issues in QuantConnect/Lean
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
dotnet-outdated/dotnet-outdated#871 ·
Maintainers usually reply within 1 day
-
triage/untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
unoplatform/uno#26676 ·
Maintainers usually reply within 1 day
-
.NET bug
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
jhaizhou-ops/S2Arm#2177 · 3 comments ·
Maintainers usually reply within 1 day
-
area-CodeGen-coreclr untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
dotnet/runtime#138181 · 1 comment ·
Maintainers usually reply within 1 day