pandas.Series.tolist should convert Series contents from numpy datatypes to native Python datatypes · Issue #10904 · pandas-dev/pandas (original) (raw)

Noticed this in Pandas v0.16.1 when JSON serializing a large nested dictionary containing mixes of arrays, numpy arrays, and pandas series. Calling tolist() on numpy arrays allows the standard library JSON serializer to serialize them, but this is not the case for pandas.

numpy.array.tolist converts all of the array's contents to native Python datatypes. For compatibility with numpy, pandas.Series.tolist should also convert its contents to native Python datatypes. Currently, the list returned by pandas contains elements which have types like numpy.int64 or numpy.float64.

For example:

>>> from pandas
>>> import numpy
>>> np_array = numpy.array([1, 2, 3])
>>> pd_series = pandas.Series([1,2,3])
>>> np_list = np_array.tolist()
>>> pd_list = pd_series.tolist()
>>> type(np_list[0])
<class 'int'>
>>> type(pd_list[0])
<class 'numpy.int64'>