Skip to content

Convert to native datatypes for Series.tolist() #13050

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions doc/source/whatsnew/v0.18.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,33 @@ API changes
- Non-convertible dates in an excel date column will be returned without conversion and the column will be ``object`` dtype, rather than raising an exception (:issue:`10001`)


Series.tolist() will now return Python types
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Series.tolist() was updated to return Python types, mimicking NumPy tolist() behaviour (:issue:`10904`)


.. ipython:: python

s = pd.Series([1,2,3])
type(s.tolist()[0])

Previous Behavior:

.. code-block:: ipython

In [6]: s = pandas.Series([1,2,3])
In [7]: type(s.tolist()[0])
Out[7]:
<class 'numpy.int64'>


New Behavior:

.. ipython:: python

s = pd.Series([1,2,3])
type(s.tolist()[0])



Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ def keys(self):

def tolist(self):
""" Convert Series to a nested list """
return list(self)
return list(self.asobject)

def to_dict(self):
"""
Expand Down
48 changes: 37 additions & 11 deletions pandas/tests/series/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put all of the *tolist methods in a new class TestSeriesToList or like.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's some more tolist tests, move those as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't move those as they depend on TestData

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move the other _tolist (and have the new class mixin TestData)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved

from pandas import Series, DataFrame

from pandas.compat import StringIO, u
from pandas.compat import StringIO, u, long
from pandas.util.testing import (assert_series_equal, assert_almost_equal,
assert_frame_equal, ensure_clean)
import pandas.util.testing as tm
Expand Down Expand Up @@ -82,16 +82,6 @@ def test_to_csv_unicode_index(self):

assert_series_equal(s, s2)

def test_tolist(self):
rs = self.ts.tolist()
xp = self.ts.values.tolist()
assert_almost_equal(rs, xp)

# datetime64
s = Series(self.ts.index)
rs = s.tolist()
self.assertEqual(self.ts.index[0], rs[0])

def test_to_frame(self):
self.ts.name = None
rs = self.ts.to_frame()
Expand Down Expand Up @@ -174,3 +164,39 @@ class SubclassedFrame(DataFrame):
self.assertTrue(isinstance(result, SubclassedFrame))
expected = SubclassedFrame({'X': [1, 2, 3]})
assert_frame_equal(result, expected)


class TestSeriesToList(TestData, tm.TestCase):

_multiprocess_can_split_ = True

def test_tolist(self):
rs = self.ts.tolist()
xp = self.ts.values.tolist()
assert_almost_equal(rs, xp)

# datetime64
s = Series(self.ts.index)
rs = s.tolist()
self.assertEqual(self.ts.index[0], rs[0])

def test_tolist_np_int(self):
# GH10904
for t in ['int8', 'int16', 'int32', 'int64']:
s = pd.Series([1], dtype=t)
self.assertIsInstance(s.tolist()[0], int)

def test_tolist_np_uint(self):
# GH10904
for t in ['uint8', 'uint16']:
s = pd.Series([1], dtype=t)
self.assertIsInstance(s.tolist()[0], int)
for t in ['uint32', 'uint64']:
s = pd.Series([1], dtype=t)
self.assertIsInstance(s.tolist()[0], long)

def test_tolist_np_float(self):
# GH10904
for t in ['float16', 'float32', 'float64']:
s = pd.Series([1], dtype=t)
self.assertIsInstance(s.tolist()[0], float)