Skip to content

BUG: A series/dataframe with np.nan values stored in a tuple does not pickle/unpickle as expected #45945

Open
@thehomebrewnerd

Description

@thehomebrewnerd

Pandas version checks

  • I have checked that this issue has not already been reported.

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np
import pickle

# Tuple of nan's doesn't work
series = pd.Series([(0, 1), (np.nan, np.nan)])
series_pkl = pickle.dumps(series)
series_unpickled = pickle.loads(series_pkl)
series_unpickled.equals(series) # False, but should be True
pd.testing.assert_series_equal(series, series_unpickled) # True

# Single nan value works as expected
series = pd.Series([(0, 1), np.nan])
series_pkl = pickle.dumps(series)
series_unpickled = pickle.loads(series_pkl)
series_unpickled.equals(series) # True as expected
pd.testing.assert_series_equal(series, series_unpickled) # True

Issue Description

If you pickle a series or dataframe that contains a tuple of nan values (np.nan, np.nan) using pickle.dumps and then unpickle with pickle.loads the dataframe returned from unpickling does not evaluate as equal to the original dataframe or series by using the .equals method. Using the testing functions pd.testing.assert_series_equal or pd.testing.assert_frame_equal indicate the original and unpickled versions are the equal.

The original and unpickled versions are equal if the tuple of nan values is replaced with a single nan value. Additionally, using pd.NA in a tuple also works as expected.

Expected Behavior

I expect that after unpickling the unpickled dataframe should be equal to the original dataframe as checked by both dataframe.equals and pd.testing.assert_frame_equal.

Installed Versions

INSTALLED VERSIONS

commit : bb1f651
python : 3.8.5.final.0
python-bits : 64
OS : Darwin
OS-release : 20.6.0
Version : Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:21 PDT 2021; root:xnu-7195.141.6~3/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.4.0
numpy : 1.22.2
pytz : 2021.3
dateutil : 2.8.2
pip : 22.0.3
setuptools : 47.1.0
Cython : None
pytest : 7.0.0
hypothesis : None
sphinx : 4.2.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 7.31.1
pandas_datareader: None
bs4 : 4.10.0
bottleneck : None
fastparquet : None
fsspec : 2022.01.0
gcsfs : None
matplotlib : 3.3.3
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 7.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.0
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIO Pickleread_pickle, to_pickleMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolateNested DataData where the values are collections (lists, sets, dicts, objects, etc.).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions