Skip to content

Commit b2009e6

Browse files
gliptakjreback
authored andcommitted
COMPAT: Convert to native datatypes for Series.tolist()
closes #10904 Author: Gábor Lipták <[email protected]> Closes #13050 from gliptak/npcompat1 and squashes the following commits: 4da83ac [Gábor Lipták] Convert to native datatypes for Series.tolist()
1 parent 1296ab3 commit b2009e6

File tree

3 files changed

+88
-32
lines changed

3 files changed

+88
-32
lines changed

doc/source/whatsnew/v0.18.2.txt

+26
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,32 @@ 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+
.. _whatsnew_0182.api.tolist:
45+
46+
``Series.tolist()`` will now return Python types
47+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48+
49+
``Series.tolist()`` will now return Python types in the output, mimicking NumPy ``.tolist()`` behaviour (:issue:`10904`)
50+
51+
52+
.. ipython:: python
53+
54+
s = pd.Series([1,2,3])
55+
type(s.tolist()[0])
56+
57+
Previous Behavior:
58+
59+
.. code-block:: ipython
60+
61+
In [7]: type(s.tolist()[0])
62+
Out[7]:
63+
<class 'numpy.int64'>
64+
65+
New Behavior:
66+
67+
.. ipython:: python
68+
69+
type(s.tolist()[0])
4470

4571

4672

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

+61-31
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
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
1515

1616
from .common import TestData
1717

1818

19-
class TestSeriesIO(TestData, tm.TestCase):
19+
class TestSeriesToCSV(TestData, tm.TestCase):
2020

2121
_multiprocess_can_split_ = True
2222

@@ -82,35 +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-
95-
def test_to_frame(self):
96-
self.ts.name = None
97-
rs = self.ts.to_frame()
98-
xp = pd.DataFrame(self.ts.values, index=self.ts.index)
99-
assert_frame_equal(rs, xp)
100-
101-
self.ts.name = 'testname'
102-
rs = self.ts.to_frame()
103-
xp = pd.DataFrame(dict(testname=self.ts.values), index=self.ts.index)
104-
assert_frame_equal(rs, xp)
105-
106-
rs = self.ts.to_frame(name='testdifferent')
107-
xp = pd.DataFrame(
108-
dict(testdifferent=self.ts.values), index=self.ts.index)
109-
assert_frame_equal(rs, xp)
110-
111-
def test_to_dict(self):
112-
self.assert_numpy_array_equal(Series(self.ts.to_dict()), self.ts)
113-
11485
def test_to_csv_float_format(self):
11586

11687
with ensure_clean() as filename:
@@ -137,6 +108,29 @@ def test_to_csv_path_is_none(self):
137108
csv_str = s.to_csv(path=None)
138109
self.assertIsInstance(csv_str, str)
139110

111+
class TestSeriesIO(TestData, tm.TestCase):
112+
113+
_multiprocess_can_split_ = True
114+
115+
def test_to_frame(self):
116+
self.ts.name = None
117+
rs = self.ts.to_frame()
118+
xp = pd.DataFrame(self.ts.values, index=self.ts.index)
119+
assert_frame_equal(rs, xp)
120+
121+
self.ts.name = 'testname'
122+
rs = self.ts.to_frame()
123+
xp = pd.DataFrame(dict(testname=self.ts.values), index=self.ts.index)
124+
assert_frame_equal(rs, xp)
125+
126+
rs = self.ts.to_frame(name='testdifferent')
127+
xp = pd.DataFrame(
128+
dict(testdifferent=self.ts.values), index=self.ts.index)
129+
assert_frame_equal(rs, xp)
130+
131+
def test_to_dict(self):
132+
self.assert_numpy_array_equal(Series(self.ts.to_dict()), self.ts)
133+
140134
def test_timeseries_periodindex(self):
141135
# GH2891
142136
from pandas import period_range
@@ -174,3 +168,39 @@ class SubclassedFrame(DataFrame):
174168
self.assertTrue(isinstance(result, SubclassedFrame))
175169
expected = SubclassedFrame({'X': [1, 2, 3]})
176170
assert_frame_equal(result, expected)
171+
172+
173+
class TestSeriesToList(TestData, tm.TestCase):
174+
175+
_multiprocess_can_split_ = True
176+
177+
def test_tolist(self):
178+
rs = self.ts.tolist()
179+
xp = self.ts.values.tolist()
180+
assert_almost_equal(rs, xp)
181+
182+
# datetime64
183+
s = Series(self.ts.index)
184+
rs = s.tolist()
185+
self.assertEqual(self.ts.index[0], rs[0])
186+
187+
def test_tolist_np_int(self):
188+
# GH10904
189+
for t in ['int8', 'int16', 'int32', 'int64']:
190+
s = pd.Series([1], dtype=t)
191+
self.assertIsInstance(s.tolist()[0], int)
192+
193+
def test_tolist_np_uint(self):
194+
# GH10904
195+
for t in ['uint8', 'uint16']:
196+
s = pd.Series([1], dtype=t)
197+
self.assertIsInstance(s.tolist()[0], int)
198+
for t in ['uint32', 'uint64']:
199+
s = pd.Series([1], dtype=t)
200+
self.assertIsInstance(s.tolist()[0], long)
201+
202+
def test_tolist_np_float(self):
203+
# GH10904
204+
for t in ['float16', 'float32', 'float64']:
205+
s = pd.Series([1], dtype=t)
206+
self.assertIsInstance(s.tolist()[0], float)

0 commit comments

Comments
 (0)