Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas dataframe index: to_list() vs tolist()

Tags:

python

pandas

I recently wrote a python script for someone, where I converted a pandas dataframe's index into a list using to_list(). However, this does not work for them, as they get: AttributeError: 'Index' object has no attribute 'to_list' with their python interpretter.

I did some searching and found that there is also tolist() that seems to do the same as to_list(): searching on Pandas documentation finds both, with word-for-word identical description. On the other hand, the documentation of Index mentions only to_list().

So I wonder whether there is a difference between the two

  • in functionality
  • in popularity and/or "officiality"/endorsement
  • in support in different version of pandas
like image 568
Michal Kaut Avatar asked Sep 09 '19 12:09

Michal Kaut


2 Answers

If you check the source code, you will see that right after tolist() code there is line to_list = tolist, so to_list is just alias for tolist

EDIT: to_list() was added in ver. 0.24.0, see issue#8826

like image 147
buran Avatar answered Sep 22 '22 11:09

buran


Expanding on buran's answer (too much text to fit into a comment): Using git blameon the pointed-out source code, one can see that to_list() was indeed added in December 2018 as an alias to tolist(). It was commited as an enhancement to resolve issue 8826, i.e., to make it more inline with to_period() and to_timestamp().

Moreover, changes and comments in pandas/core/series.py show that the original tolist() is "semi-deprecated":

# 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'])

Interesting to see how the system works...

like image 27
Michal Kaut Avatar answered Sep 25 '22 11:09

Michal Kaut