Skip to content

Commit

Permalink
ENH: to_list as alias for tolist (#23398)
Browse files Browse the repository at this point in the history
  • Loading branch information
ScientiaEtVeritas authored and jreback committed Dec 13, 2018
1 parent 33ca356 commit 250dd5f
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 16 deletions.
4 changes: 2 additions & 2 deletions doc/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ Conversion
Series.bool
Series.to_period
Series.to_timestamp
Series.tolist
Series.to_list
Series.get_values


Expand Down Expand Up @@ -1541,7 +1541,7 @@ Conversion
Index.item
Index.map
Index.ravel
Index.tolist
Index.to_list
Index.to_native_types
Index.to_series
Index.to_frame
Expand Down
10 changes: 5 additions & 5 deletions doc/source/timedeltas.rst
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,11 @@ Finally, the combination of ``TimedeltaIndex`` with ``DatetimeIndex`` allow cert
.. ipython:: python
tdi = pd.TimedeltaIndex(['1 days', pd.NaT, '2 days'])
tdi.tolist()
tdi.to_list()
dti = pd.date_range('20130101', periods=3)
dti.tolist()
(dti + tdi).tolist()
(dti - tdi).tolist()
dti.to_list()
(dti + tdi).to_list()
(dti - tdi).to_list()
Conversions
~~~~~~~~~~~
Expand All @@ -461,7 +461,7 @@ Scalars type ops work as well. These can potentially return a *different* type o
# subtraction of a date and a timedelta -> datelike
# note that trying to subtract a date from a Timedelta will raise an exception
(pd.Timestamp('20130101') - tdi).tolist()
(pd.Timestamp('20130101') - tdi).to_list()
# timedelta + timedelta -> timedelta
tdi + pd.Timedelta('10 days')
Expand Down
6 changes: 3 additions & 3 deletions doc/source/timeseries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2338,7 +2338,7 @@ Infer the ambiguous times
.. ipython:: python
rng_hourly_eastern = rng_hourly.tz_localize('US/Eastern', ambiguous='infer')
rng_hourly_eastern.tolist()
rng_hourly_eastern.to_list()
In addition to 'infer', there are several other arguments supported. Passing
an array-like of bools or 0s/1s where True represents a DST hour and False a
Expand All @@ -2351,8 +2351,8 @@ constructor as well as ``tz_localize``.
.. ipython:: python
rng_hourly_dst = np.array([1, 1, 0, 0, 0])
rng_hourly.tz_localize('US/Eastern', ambiguous=rng_hourly_dst).tolist()
rng_hourly.tz_localize('US/Eastern', ambiguous='NaT').tolist()
rng_hourly.tz_localize('US/Eastern', ambiguous=rng_hourly_dst).to_list()
rng_hourly.tz_localize('US/Eastern', ambiguous='NaT').to_list()
didx = pd.DatetimeIndex(start='2014-08-01 09:00', freq='H',
periods=10, tz='US/Eastern')
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,7 @@ Other API Changes
- :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`).
- 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`)
- :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`).
- :func:`Series.to_list` and :func:`Index.to_list` are now aliases of ``Series.tolist`` respectively ``Index.tolist`` (:issue:`8826`)

.. _whatsnew_0240.deprecations:

Expand Down
5 changes: 4 additions & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ class Categorical(ExtensionArray, PandasObject):
# ops, which raise
__array_priority__ = 1000
_dtype = CategoricalDtype(ordered=False)
_deprecations = frozenset(['labels'])
# tolist is not actually deprecated, just suppressed in the __dir__
_deprecations = frozenset(['labels', 'tolist'])
_typ = 'categorical'

def __init__(self, values, categories=None, ordered=None, dtype=None,
Expand Down Expand Up @@ -567,6 +568,8 @@ def tolist(self):
"""
return list(self)

to_list = tolist

@property
def base(self):
"""
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,8 @@ def tolist(self):
else:
return self._values.tolist()

to_list = tolist

def __iter__(self):
"""
Return an iterator of the values.
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from pandas.core.dtypes.missing import array_equivalent, isna

from pandas.core import ops
from pandas.core.accessor import CachedAccessor
from pandas.core.accessor import CachedAccessor, DirNamesMixin
import pandas.core.algorithms as algos
from pandas.core.arrays import ExtensionArray
from pandas.core.base import IndexOpsMixin, PandasObject
Expand Down Expand Up @@ -202,6 +202,9 @@ class Index(IndexOpsMixin, PandasObject):
>>> pd.Index(list('abc'))
Index(['a', 'b', 'c'], dtype='object')
"""
# tolist is not actually deprecated, just suppressed in the __dir__
_deprecations = DirNamesMixin._deprecations | frozenset(['tolist'])

# To hand over control to subclasses
_join_precedence = 1

Expand Down
3 changes: 2 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
"""
_metadata = ['name']
_accessors = {'dt', 'cat', 'str', 'sparse'}
# tolist is not actually deprecated, just suppressed in the __dir__
_deprecations = generic.NDFrame._deprecations | frozenset(
['asobject', 'reshape', 'get_value', 'set_value',
'from_csv', 'valid'])
'from_csv', 'valid', 'tolist'])

# Override cache_readonly bc Series is mutable
hasnans = property(base.IndexOpsMixin.hasnans.func,
Expand Down
9 changes: 6 additions & 3 deletions pandas/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1099,9 +1099,10 @@ class TestToIterable(object):
'method',
[
lambda x: x.tolist(),
lambda x: x.to_list(),
lambda x: list(x),
lambda x: list(x.__iter__()),
], ids=['tolist', 'list', 'iter'])
], ids=['tolist', 'to_list', 'list', 'iter'])
@pytest.mark.parametrize('typ', [Series, Index])
def test_iterable(self, typ, method, dtype, rdtype):
# gh-10904
Expand All @@ -1122,9 +1123,10 @@ def test_iterable(self, typ, method, dtype, rdtype):
'method',
[
lambda x: x.tolist(),
lambda x: x.to_list(),
lambda x: list(x),
lambda x: list(x.__iter__()),
], ids=['tolist', 'list', 'iter'])
], ids=['tolist', 'to_list', 'list', 'iter'])
@pytest.mark.parametrize('typ', [Series, Index])
def test_iterable_object_and_category(self, typ, method,
dtype, rdtype, obj):
Expand Down Expand Up @@ -1167,9 +1169,10 @@ def test_iterable_map(self, typ, dtype, rdtype):
'method',
[
lambda x: x.tolist(),
lambda x: x.to_list(),
lambda x: list(x),
lambda x: list(x.__iter__()),
], ids=['tolist', 'list', 'iter'])
], ids=['tolist', 'to_list', 'list', 'iter'])
def test_categorial_datetimelike(self, method):
i = CategoricalIndex([Timestamp('1999-12-31'),
Timestamp('2000-12-31')])
Expand Down

0 comments on commit 250dd5f

Please sign in to comment.