Allow JSON encoder to handle ndarray #777
Merged
+18
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of changes:
The JSON encoder encodes a variety of data types via
json.dumps
, including primitives, lists, dicts, etc.However
json.dumps
doesn't work withndarray
s ; it won't serialize them.Conceptually, there isn't a strong reason to refuse to encode input as JSON where
ndarray
is used instead of lists, when one could simply transform even multi-dimensionalndarray
s to supported Python lists with.tolist()
.This change simply has the encoder call
.tolist()
on its arg if passed anndarray
, and the rest works as expected.This is actually relevant because the serialization of array type data from Spark to pandas will use
ndarray
s (being based on Arrow), and the result can't be passed todataframe_to_mds
even if the output type is given as'json'
. It's possible to workaround by passing a transformation of the pandas DF to this function. But, it seemed simple and natural to just support this directly.Merge Checklist:
General
Tests
pre-commit
on my change. (check out thepre-commit
section of prerequisites)