Skip to content

Commit 4da83ac

Browse files
committed
Convert to native datatypes for Series.tolist()
1 parent 1296ab3 commit 4da83ac

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed

doc/source/whatsnew/v0.18.2.txt

+27
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,33 @@ API changes
4141
- 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`)
4242

4343

44+
Series.tolist() will now return Python types
45+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
46+
47+
Series.tolist() was updated to return Python types, mimicking NumPy tolist() behaviour (:issue:`10904`)
48+
49+
50+
.. ipython:: python
51+
52+
s = pd.Series([1,2,3])
53+
type(s.tolist()[0])
54+
55+
Previous Behavior:
56+
57+
.. code-block:: ipython
58+
59+
In [6]: s = pandas.Series([1,2,3])
60+
In [7]: type(s.tolist()[0])
61+
Out[7]:
62+
<class 'numpy.int64'>
63+
64+
65+
New Behavior:
66+
67+
.. ipython:: python
68+
69+
s = pd.Series([1,2,3])
70+
type(s.tolist()[0])
4471

4572

4673

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ def keys(self):
10851085

10861086
def tolist(self):
10871087
""" Convert Series to a nested list """
1088-
return list(self)
1088+
return list(self.asobject)
10891089

10901090
def to_dict(self):
10911091
"""

pandas/tests/series/test_io.py

+37-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from pandas import Series, DataFrame
1010

11-
from pandas.compat import StringIO, u
11+
from pandas.compat import StringIO, u, long
1212
from pandas.util.testing import (assert_series_equal, assert_almost_equal,
1313
assert_frame_equal, ensure_clean)
1414
import pandas.util.testing as tm
@@ -82,16 +82,6 @@ def test_to_csv_unicode_index(self):
8282

8383
assert_series_equal(s, s2)
8484

85-
def test_tolist(self):
86-
rs = self.ts.tolist()
87-
xp = self.ts.values.tolist()
88-
assert_almost_equal(rs, xp)
89-
90-
# datetime64
91-
s = Series(self.ts.index)
92-
rs = s.tolist()
93-
self.assertEqual(self.ts.index[0], rs[0])
94-
9585
def test_to_frame(self):
9686
self.ts.name = None
9787
rs = self.ts.to_frame()
@@ -174,3 +164,39 @@ class SubclassedFrame(DataFrame):
174164
self.assertTrue(isinstance(result, SubclassedFrame))
175165
expected = SubclassedFrame({'X': [1, 2, 3]})
176166
assert_frame_equal(result, expected)
167+
168+
169+
class TestSeriesToList(TestData, tm.TestCase):
170+
171+
_multiprocess_can_split_ = True
172+
173+
def test_tolist(self):
174+
rs = self.ts.tolist()
175+
xp = self.ts.values.tolist()
176+
assert_almost_equal(rs, xp)
177+
178+
# datetime64
179+
s = Series(self.ts.index)
180+
rs = s.tolist()
181+
self.assertEqual(self.ts.index[0], rs[0])
182+
183+
def test_tolist_np_int(self):
184+
# GH10904
185+
for t in ['int8', 'int16', 'int32', 'int64']:
186+
s = pd.Series([1], dtype=t)
187+
self.assertIsInstance(s.tolist()[0], int)
188+
189+
def test_tolist_np_uint(self):
190+
# GH10904
191+
for t in ['uint8', 'uint16']:
192+
s = pd.Series([1], dtype=t)
193+
self.assertIsInstance(s.tolist()[0], int)
194+
for t in ['uint32', 'uint64']:
195+
s = pd.Series([1], dtype=t)
196+
self.assertIsInstance(s.tolist()[0], long)
197+
198+
def test_tolist_np_float(self):
199+
# GH10904
200+
for t in ['float16', 'float32', 'float64']:
201+
s = pd.Series([1], dtype=t)
202+
self.assertIsInstance(s.tolist()[0], float)

0 commit comments

Comments
 (0)