Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: EAs with list-like values fail in to_csv (due to to_native_types) #28840

Closed
jorisvandenbossche opened this issue Oct 8, 2019 · 0 comments · Fixed by #28841
Closed

BUG: EAs with list-like values fail in to_csv (due to to_native_types) #28840

jorisvandenbossche opened this issue Oct 8, 2019 · 0 comments · Fixed by #28841
Labels
Bug ExtensionArray Extending pandas with custom dtypes or arrays.
Milestone

Comments

@jorisvandenbossche
Copy link
Member

From a report at GeoPandas: geopandas/geopandas#1145

Explanation: assume you have an ExtensionArray with list-like values (eg with an object numpy array with lists under the hood). When calling, to_csv, which goes through Block.to_native_types, we convert this to a numpy array and call astype(str):

values = values.astype("<U{size}".format(size=itemsize))

But, in numpy, if you have a numpy array with list like values, doing astype(str) does not work:

In [2]: a = np.array([[1, 2], [3]], dtype=object) 

In [3]: a.astype(str)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-db38302b5f9a> in <module>
----> 1 a.astype(str)

ValueError: setting an array element with a sequence

This could be considered a numpy issue, but, we should also avoid converting to a numpy array while we can directly call astype on the ExtensionArray.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug ExtensionArray Extending pandas with custom dtypes or arrays.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants