Skip to content

Commit 250dd5f

Browse files
retkowskijreback
authored andcommitted
ENH: to_list as alias for tolist (#23398)
1 parent 33ca356 commit 250dd5f

File tree

9 files changed

+29
-16
lines changed

9 files changed

+29
-16
lines changed

doc/source/api.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ Conversion
330330
Series.bool
331331
Series.to_period
332332
Series.to_timestamp
333-
Series.tolist
333+
Series.to_list
334334
Series.get_values
335335

336336

@@ -1541,7 +1541,7 @@ Conversion
15411541
Index.item
15421542
Index.map
15431543
Index.ravel
1544-
Index.tolist
1544+
Index.to_list
15451545
Index.to_native_types
15461546
Index.to_series
15471547
Index.to_frame

doc/source/timedeltas.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,11 @@ Finally, the combination of ``TimedeltaIndex`` with ``DatetimeIndex`` allow cert
436436
.. ipython:: python
437437
438438
tdi = pd.TimedeltaIndex(['1 days', pd.NaT, '2 days'])
439-
tdi.tolist()
439+
tdi.to_list()
440440
dti = pd.date_range('20130101', periods=3)
441-
dti.tolist()
442-
(dti + tdi).tolist()
443-
(dti - tdi).tolist()
441+
dti.to_list()
442+
(dti + tdi).to_list()
443+
(dti - tdi).to_list()
444444
445445
Conversions
446446
~~~~~~~~~~~
@@ -461,7 +461,7 @@ Scalars type ops work as well. These can potentially return a *different* type o
461461
462462
# subtraction of a date and a timedelta -> datelike
463463
# note that trying to subtract a date from a Timedelta will raise an exception
464-
(pd.Timestamp('20130101') - tdi).tolist()
464+
(pd.Timestamp('20130101') - tdi).to_list()
465465
466466
# timedelta + timedelta -> timedelta
467467
tdi + pd.Timedelta('10 days')

doc/source/timeseries.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -2338,7 +2338,7 @@ Infer the ambiguous times
23382338
.. ipython:: python
23392339
23402340
rng_hourly_eastern = rng_hourly.tz_localize('US/Eastern', ambiguous='infer')
2341-
rng_hourly_eastern.tolist()
2341+
rng_hourly_eastern.to_list()
23422342
23432343
In addition to 'infer', there are several other arguments supported. Passing
23442344
an array-like of bools or 0s/1s where True represents a DST hour and False a
@@ -2351,8 +2351,8 @@ constructor as well as ``tz_localize``.
23512351
.. ipython:: python
23522352
23532353
rng_hourly_dst = np.array([1, 1, 0, 0, 0])
2354-
rng_hourly.tz_localize('US/Eastern', ambiguous=rng_hourly_dst).tolist()
2355-
rng_hourly.tz_localize('US/Eastern', ambiguous='NaT').tolist()
2354+
rng_hourly.tz_localize('US/Eastern', ambiguous=rng_hourly_dst).to_list()
2355+
rng_hourly.tz_localize('US/Eastern', ambiguous='NaT').to_list()
23562356
23572357
didx = pd.DatetimeIndex(start='2014-08-01 09:00', freq='H',
23582358
periods=10, tz='US/Eastern')

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,7 @@ Other API Changes
10871087
- :meth:`Index.hasnans` and :meth:`Series.hasnans` now always return a python boolean. Previously, a python or a numpy boolean could be returned, depending on circumstances (:issue:`23294`).
10881088
- The order of the arguments of :func:`DataFrame.to_html` and :func:`DataFrame.to_string` is rearranged to be consistent with each other. (:issue:`23614`)
10891089
- :meth:`CategoricalIndex.reindex` now raises a ``ValueError`` if the target index is non-unique and not equal to the current index. It previously only raised if the target index was not of a categorical dtype (:issue:`23963`).
1090+
- :func:`Series.to_list` and :func:`Index.to_list` are now aliases of ``Series.tolist`` respectively ``Index.tolist`` (:issue:`8826`)
10901091

10911092
.. _whatsnew_0240.deprecations:
10921093

pandas/core/arrays/categorical.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,8 @@ class Categorical(ExtensionArray, PandasObject):
309309
# ops, which raise
310310
__array_priority__ = 1000
311311
_dtype = CategoricalDtype(ordered=False)
312-
_deprecations = frozenset(['labels'])
312+
# tolist is not actually deprecated, just suppressed in the __dir__
313+
_deprecations = frozenset(['labels', 'tolist'])
313314
_typ = 'categorical'
314315

315316
def __init__(self, values, categories=None, ordered=None, dtype=None,
@@ -567,6 +568,8 @@ def tolist(self):
567568
"""
568569
return list(self)
569570

571+
to_list = tolist
572+
570573
@property
571574
def base(self):
572575
"""

pandas/core/base.py

+2
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,8 @@ def tolist(self):
10251025
else:
10261026
return self._values.tolist()
10271027

1028+
to_list = tolist
1029+
10281030
def __iter__(self):
10291031
"""
10301032
Return an iterator of the values.

pandas/core/indexes/base.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from pandas.core.dtypes.missing import array_equivalent, isna
3232

3333
from pandas.core import ops
34-
from pandas.core.accessor import CachedAccessor
34+
from pandas.core.accessor import CachedAccessor, DirNamesMixin
3535
import pandas.core.algorithms as algos
3636
from pandas.core.arrays import ExtensionArray
3737
from pandas.core.base import IndexOpsMixin, PandasObject
@@ -202,6 +202,9 @@ class Index(IndexOpsMixin, PandasObject):
202202
>>> pd.Index(list('abc'))
203203
Index(['a', 'b', 'c'], dtype='object')
204204
"""
205+
# tolist is not actually deprecated, just suppressed in the __dir__
206+
_deprecations = DirNamesMixin._deprecations | frozenset(['tolist'])
207+
205208
# To hand over control to subclasses
206209
_join_precedence = 1
207210

pandas/core/series.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,10 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
134134
"""
135135
_metadata = ['name']
136136
_accessors = {'dt', 'cat', 'str', 'sparse'}
137+
# tolist is not actually deprecated, just suppressed in the __dir__
137138
_deprecations = generic.NDFrame._deprecations | frozenset(
138139
['asobject', 'reshape', 'get_value', 'set_value',
139-
'from_csv', 'valid'])
140+
'from_csv', 'valid', 'tolist'])
140141

141142
# Override cache_readonly bc Series is mutable
142143
hasnans = property(base.IndexOpsMixin.hasnans.func,

pandas/tests/test_base.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1099,9 +1099,10 @@ class TestToIterable(object):
10991099
'method',
11001100
[
11011101
lambda x: x.tolist(),
1102+
lambda x: x.to_list(),
11021103
lambda x: list(x),
11031104
lambda x: list(x.__iter__()),
1104-
], ids=['tolist', 'list', 'iter'])
1105+
], ids=['tolist', 'to_list', 'list', 'iter'])
11051106
@pytest.mark.parametrize('typ', [Series, Index])
11061107
def test_iterable(self, typ, method, dtype, rdtype):
11071108
# gh-10904
@@ -1122,9 +1123,10 @@ def test_iterable(self, typ, method, dtype, rdtype):
11221123
'method',
11231124
[
11241125
lambda x: x.tolist(),
1126+
lambda x: x.to_list(),
11251127
lambda x: list(x),
11261128
lambda x: list(x.__iter__()),
1127-
], ids=['tolist', 'list', 'iter'])
1129+
], ids=['tolist', 'to_list', 'list', 'iter'])
11281130
@pytest.mark.parametrize('typ', [Series, Index])
11291131
def test_iterable_object_and_category(self, typ, method,
11301132
dtype, rdtype, obj):
@@ -1167,9 +1169,10 @@ def test_iterable_map(self, typ, dtype, rdtype):
11671169
'method',
11681170
[
11691171
lambda x: x.tolist(),
1172+
lambda x: x.to_list(),
11701173
lambda x: list(x),
11711174
lambda x: list(x.__iter__()),
1172-
], ids=['tolist', 'list', 'iter'])
1175+
], ids=['tolist', 'to_list', 'list', 'iter'])
11731176
def test_categorial_datetimelike(self, method):
11741177
i = CategoricalIndex([Timestamp('1999-12-31'),
11751178
Timestamp('2000-12-31')])

0 commit comments

Comments
 (0)