-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Conversation
There was a problem hiding this 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.
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 |
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. |
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. |
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 |
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff
Related to #27977.
output of
python scripts/validate_docstrings.py pandas.Index.to_series
:Notes:
While working with the documentation, I noticed that index parameter in
pandas.Index.to_series
has different behaviour withpandas.Index.to_frame
. You can't specify index parameter to False inpandas.Index.to_series
to achieve the same index result withpandas.Index.to_frame
. Is this intentional?