Skip to content

[Python] MapArray pandas round trip is broken #31965

Open

Description

@asfimport

pyarrow.MapArray when converted to pandas cannot be successfully converted back.

The following snipper does not work:

 

import pyarrow as pa

data = [[('x', 1), ('y', 0)], [('a', 2), ('b', 45)]]
ty = pa.map_(pa.string(), pa.int64())
map_col = pa.array(data, type=ty)
pa.MapArray.from_pandas(map_col.to_pandas())

Uncaught exception: ArrowTypeError: Expected bytes, got a 'int' object (java.lang.RuntimeException)

Reporter: Robbie Gruener / @rgruener

Note: This issue was originally created as ARROW-16611. Please see the migration documentation for further details.

Activity

  1. asfimport commented on May 18, 2022

    @asfimport
    Author

    Antoine Pitrou / @pitrou:
    Hmm, where does the "java.lang.RuntimeException" come from?

  2. asfimport commented on May 18, 2022

    @asfimport
    Author

    Antoine Pitrou / @pitrou:
    Also, did it work with previous versions of PyArrow? I don't think so.

  3. asfimport commented on May 31, 2022

    @asfimport
    Author

    Joris Van den Bossche / @jorisvandenbossche:
    If you specify the type again, then it works:

    pa.MapArray.from_pandas(map_col.to_pandas(), type=ty)

    Also small note that the from_pandas method is a generic Array class method, and not specific to MapArray. So what is actually being done under the hood is pa.array(map_col.to_pandas(), from_pandas=True) (which also illustrates why you need to specify the type again).

    In general it is difficult to infer a map type (i.e. inferring the type from the data when not specifying an exact type). A list of tuples can for example also be the representation for a struct array.

  4. thisisnic commented on Oct 26, 2026

    @thisisnic
    Member

    @jorisvandenbossche I'm going through old Type: bug issues. You explained here that MapArray.from_pandas() is just the generic Array.from_pandas(), so a map type cannot be inferred from a list of tuples and passing type=pa.map_(pa.string(), pa.int64()) makes the round trip work; the reporter never followed up. Are you happy for me to close this as answered?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions