Skip to content

DOC: Fix error in pandas.Index.to_series #34349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2020

Conversation

farhanreynaldo
Copy link
Contributor

  • closes #xxxx
  • tests added / passed
  • passes black pandas
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff
  • whatsnew entry

Related to #27977.

output of python scripts/validate_docstrings.py pandas.Index.to_series:

################################################################################
################################## Validation ##################################
################################################################################

Notes:
While working with the documentation, I noticed that index parameter in pandas.Index.to_series has different behaviour with pandas.Index.to_frame. You can't specify index parameter to False in pandas.Index.to_series to achieve the same index result with pandas.Index.to_frame. Is this intentional?

Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks! Examples are always helpful, IMO - as it says in the docstring guide,

As often, people understand concepts better with examples, than with accurate explanations.

@farhanreynaldo
Copy link
Contributor Author

Nice, thanks! Examples are always helpful, IMO - as it says in the docstring guide,

As often, people understand concepts better with examples, than with accurate explanations.

Sure, here's the minimal example

In [1]: import pandas as pd

In [2]: idx = pd.Index(['Ant', 'Bear', 'Cow'], name='animal')

In [3]: idx.to_frame(index=False)
Out[3]: 
  animal
0    Ant
1   Bear
2    Cow

In [4]: idx.to_series(index=False)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-bc071128e429> in <module>
----> 1 idx.to_series(index=False)

E:\farhan-reynaldo\pandas-farhanreynaldo\pandas\core\indexes\base.py in to_series(self, index, name)
   1053             name = self.name
   1054
-> 1055         return Series(self.values.copy(), index=index, name=name)
   1056
   1057     def to_frame(self, index: bool = True, name=None):

E:\farhan-reynaldo\pandas-farhanreynaldo\pandas\core\series.py in __init__(self, data, index, dtype, name, copy, fastpath)
    239
    240             if index is not None:
--> 241                 index = ensure_index(index)
    242
    243             if data is None:

E:\farhan-reynaldo\pandas-farhanreynaldo\pandas\core\indexes\base.py in ensure_index(index_like, copy)
   5557             index_like = copy_func(index_like)
   5558
-> 5559     return Index(index_like)
   5560
   5561

E:\farhan-reynaldo\pandas-farhanreynaldo\pandas\core\indexes\base.py in __new__(cls, data, dtype, copy, name, tupleize_cols, **kwargs)
    403
    404         elif data is None or is_scalar(data):
--> 405             raise cls._scalar_data_error(data)
    406         elif hasattr(data, "__array__"):
    407             return Index(np.asarray(data), dtype=dtype, copy=copy, name=name, **kwargs)

TypeError: Index(...) must be called with a collection of some kind, False was passed

I expect the pandas.Index.to_series would also produce default index like in pandas.Index.to_frame.

@MarcoGorelli
Copy link
Member

Sorry, I might have been unclear - what I meant was, "I like your PR because you've added examples" :)

@farhanreynaldo
Copy link
Contributor Author

Sorry, I might have been unclear - what I meant was, "I like your PR because you've added examples" :)

Oops sorry for the misunderstanding haha, I really appreciate the kind words, @MarcoGorelli.

@MarcoGorelli
Copy link
Member

I expect the pandas.Index.to_series would also produce default index like in pandas.Index.to_frame.

I hadn't come across this before, but I think I would have expected similar behaviour - could you please open a new issue (if it hasn't been posted before) with the code you just gave?

@farhanreynaldo
Copy link
Contributor Author

I expect the pandas.Index.to_series would also produce default index like in pandas.Index.to_frame.

I hadn't come across this before, but I think I would have expected similar behaviour - could you please open a new issue (if it hasn't been posted before) with the code you just gave?

Sure, I'll open a new issue if it hasn't been posted.

@jreback jreback added Docs Index Related to the Index class or subclasses labels May 25, 2020
@jreback jreback added this to the 1.1 milestone May 25, 2020
@jreback jreback merged commit 0e5c9d8 into pandas-dev:master May 25, 2020
@jreback
Copy link
Contributor

jreback commented May 25, 2020

thanks @farhanreynaldo

you can open a new issue for these differences in behavior on Series/DataFrame, reference this issue.

@MarcoGorelli
Copy link
Member

thanks @farhanreynaldo

you can open a new issue for these differences in behavior on Series/DataFrame, reference this issue.

just for reference, they opened it here #34351

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Index Related to the Index class or subclasses
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants