Skip to content

BUG: unpicking panda data frame requires "latin1" encoding #34029

Closed
@DmitryLitvintsev

Description

@DmitryLitvintsev
  • [Y ] I have checked that this issue has not already been reported.

  • [ N] I have confirmed this bug exists on the latest version of pandas.

  • [ N] (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

# Your code here
#!/usr/bin/env python3

import pandas as pd 
import pickle


df = pd.read_csv("Factory_Entries_AWS.csv")

with open("Factory_Entries_AWS_p3.pkl","wb") as f:
    pickle.dump(df, f)

with open("Factory_Entries_AWS_p3.pkl","rb") as f:
    df1 = pickle.load(f)

pd.testing.assert_frame_equal(df.sort_index(axis=1), df1.sort_index(axis=1), check_names=True)

with open("Factory_Entries_AWS_p2.pkl","rb") as f:
    df2 = pickle.load(f, encoding='bytes')

pd.testing.assert_frame_equal(df.sort_index(axis=1), df2.sort_index(axis=1),check_names=True)

Problem description

The above code fails when unpickling data frame from pickle file created with python2 with the following error:

# python3 p3.py
Traceback (most recent call last):
  File "p3.py", line 18, in <module>
    df2 = pickle.load(f, encoding='bytes')
  File "/usr/local/lib/python3.6/site-packages/pandas-1.0.1-py3.6-linux-x86_64.egg/pandas/core/indexes/base.py", line 174, in _new_Index
    return cls.__new__(cls, **d)
TypeError: __new__() keywords must be strings

[this should explain why the current behavior is a problem and why the expected output is a better solution]

replacing 'bytes' with 'latin1' solves the problem. The prescribed method of unpicking python2 objects in python3 is to use 'bytes' encoding. Why does it fail here?

Expected Output

Output of pd.show_versions()

[paste the output of pd.show_versions() here leaving a blank line after the details tag]

pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.6.8.final.0
python-bits : 64
OS : Linux
OS-release : 3.10.0-1127.el7.x86_64
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.1
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 9.0.3
setuptools : 46.1.3
Cython : None
pytest : 5.3.5
hypothesis : None
sphinx : 3.0.3
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.8.5 (dt dec pq3 ext lo64)
jinja2 : 2.11.2
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.3.5
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : 0.8.3
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions