Skip to content

to_msgpack converts tuples of length 1 to str iff no object(str) columns are present. #25837

Closed
@TyberiusPrime

Description

@TyberiusPrime

Code Sample, a copy-pastable example if possible

import pandas as pd
from pandas.testing import assert_frame_equal

for name, df in [
    ("mixed len tuples", pd.DataFrame({"x": [("one",), ("two", "three")]})),
    ("len(1) tuples", pd.DataFrame({"x": [("one",), ("two",)]})),
    (
        "len(1) tuples+str_column",
        pd.DataFrame({"x": [("one",), ("two",)], "y": ["a", "b"]}),
    ),
    (
        "len(1) tuples+categorical",
        pd.DataFrame({"x": [("one",), ("two",)], "y": pd.Categorical(["a", "b"])}),
    ),

]:
    df.to_msgpack("example")
    df2 = pd.read_msgpack("example")
    if df.equals(df2):
        print("%s -> ok" % name)
    else:
        print("%s -> different" % name)
        print(df2.x)

Problem description

DataFrame.to_msgpack() handles tuples - except when they are of length 1 and no string column is present.
This is unexpected, the round tripping shoudl neither depend on the tuples being longer nor on the dtypes of other columns.

Expected Output

mixed len tuples -> ok
len(1) tuples -> ok
len(1) tuples+str_column -> ok
len(1) tuples+categorical -> ok
len(mixed) tuples+int -> ok

###actual output
mixed len tuples -> ok
len(1) tuples -> different
0 one
1 two
Name: x, dtype: object
len(1) tuples+str_column -> ok
len(1) tuples+categorical -> different
0 one
1 two
Name: x, dtype: object

Output of pd.show_versions()

This is on todays master.
INSTALLED VERSIONS

commit: None
python: 3.7.2.final.0
python-bits: 64
OS: Linux
OS-release: 4.15.0-43-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: None
LOCALE: en_US.UTF-8

pandas: 0.25.0.dev0+301.gf2bcb9f00
pytest: 3.10.1
pip: 19.0.1
setuptools: 40.8.0
Cython: 0.29.4
numpy: 1.16.1
scipy: 1.2.0
pyarrow: 0.12.0
xarray: None
IPython: 7.3.0
sphinx: None
patsy: 0.5.1
dateutil: 2.8.0
pytz: 2018.9
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: 0.4.0
matplotlib: 3.0.2
openpyxl: None
xlrd: 1.2.0
xlwt: 1.3.0
xlsxwriter: None
lxml.etree: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None
None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions