pythonpandaspyqtpyqt5qresource

How can I read pickle file containing pandas data frame from qrc resource file with pandas read_pickle?


I have simple UI app created in PyQt5. I would like to have all of my resource files in the qrc resources.

I am using pickle data structure to store previously created DataFrame. In my app I am reading the saved pickle with pandas. When I tried to do it from the qrc_resources (created with pyrcc5) Python module I get an error.

I used same approach as in this answer:

Create a pandas dataframe from a qrc resource file

Resources file:

<!DOCTYPE RCC><RCC version="1.0">
<qresource>
    <file alias="AA_data.pkl">resources/AA_data.pkl</file>
</qresource>
</RCC>

Python code:

import bisect, io
import pandas as pd

from PyQt5.QtGui import QImage
from PyQt5.QtCore import QFile, QIODevice
import qrc_resources       

file = QFile(':/AA_data.pkl')
if file.open(QIODevice.ReadOnly):
    f = io.BytesIO(file.readAll().data())
    AA_df = pd.read_pickle(f)

Error:
ValueError: Unrecognized compression type: infer

If I do similar with Excel file it works. But with pickle file format I get an error. I am not very familiar with the data serialization and I am not able to figure it out what am I doing wrong.


Solution

  • You must use None for compression:

    import io
    import pandas as pd
    from PyQt5.QtCore import QFile, QIODevice
    import qrc_resources
    
    file = QFile(':/AA_data.pkl')
    if file.open(QIODevice.ReadOnly):
        f = io.BytesIO(file.readAll().data())
        AA_df = pd.read_pickle(f, compression=None)
        print(AA_df)