QuantConnect / QuantConnect/Lean

Dask serialization issues due to wrapper object for DataFrame

Open
#5,395 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
C#
Stars
21.7k
Forks
5.3k
Avg merge
2d 22h
Merged PRs (30d)
34

Description

Expected Behavior

The DataFrame returned by h = qb.History(qb.Securities.Keys, 10) should be a regular data frame or a conversion method should be available.

Actual Behavior

The DataFrame returned by the history call is some special wrapper object (https://github.com/QuantConnect/Lean/blob/4c085ff853b8a7e63aa4fc9dff7f03c354a75e7f/Common/Python/PandasData.cs#L571), causing various parallel processing libraries to fail during pickling. In the following stacktrace, it can bee seen that the module 'remapper' cannot be found, which is defined here: https://github.com/QuantConnect/Lean/blob/master/Common/Python/PandasData.cs

UserWarning: Distributing <class 'pandas.core.frame.DataFrame'> object. This may take some time.
distributed.core - ERROR - Exception while handling op scatter
Traceback (most recent call last):
  File "/opt/miniconda3/lib/python3.6/site-packages/distributed/core.py", line 500, in handle_comm
    result = await result
  File "/opt/miniconda3/lib/python3.6/site-packages/distributed/scheduler.py", line 4773, in scatter
    nthreads, data, rpc=self.rpc, report=False
  File "/opt/miniconda3/lib/python3.6/site-packages/distributed/utils_comm.py", line 149, in scatter_to_workers
    for address, v in d.items()
  File "/opt/miniconda3/lib/python3.6/site-packages/distributed/utils.py", line 229, in All
    result = await tasks.next()
  File "/opt/miniconda3/lib/python3.6/site-packages/distributed/core.py", line 861, in send_recv_from_rpc
    result = await send_recv(comm=comm, op=key, **kwargs)
  File "/opt/miniconda3/lib/python3.6/site-packages/distributed/core.py", line 662, in send_recv
    raise Exception(response["text"])
Exception: No module named 'remapper'
Potential Solution
  1. Allow conversion into a regular panda data frame. I have tried using the regular pandas DataFrame constructor, but the wrapper remains. I think this would be clearly the preferable way, as any downstream issues wrt pandas parallel processing will be avoided.
  2. Document how the PYTHONPATH has to be modified (in the notebook itself, prior to importing modin) when using the Research Docker image so that the Dask workers can locate the remapper module.
Reproducing the Problem
from clr import AddReference
from QuantConnect.Python import *
from QuantConnect.Brokerages import BrokerageName

import modin.pandas as pd
import pandas as pdo

qb = QuantBook()
qb.SetBrokerageModel(BrokerageName.Bitfinex, AccountType.Margin)
qb.SetStartDate(2021,1,18)
qb.AddCrypto("BTCUSD", Resolution.Minute)

h = qb.History(qb.Securities.Keys, 2 * 10)

# This shows that the remapper wrapper object is kinda 'persistent'
h_pandas = pdo.DataFrame(h)
h_modin = pd.DataFrame(h_pandas)
System Information

quantconnect/research:latest docker image, extended by installation of modin[dask]==0.6.3.

Checklist
  • I have completely filled out this template
  • I have confirmed that this issue exists on the current master branch
  • I have confirmed that this is not a duplicate issue by searching issues
  • I have provided detailed steps to reproduce the issue

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Common/Python/PandasData.cs, the QuantBook History entry point, and the supplied reproduction using pandas, Modin, and Dask. Run the example to confirm the wrapper survives pandas conversion, then define done as either a regular DataFrame conversion path or documentation that makes the remapper module available to Dask workers.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, pandas, python
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.