Skip to content

BUG: set Index parameter to False in Index.to_series doesn't work as expected #34351

Open
@farhanreynaldo

Description

@farhanreynaldo
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

# Your code here
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)

Traceback

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

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):

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:

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

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

Problem description

You can't specify index parameter to False in pandas.Index.to_series to achieve the same index result with pandas.Index.to_frame. It would be more intuitive if we could also specify index to False in pandas.Index.to_series and get default index, to match its counterpart.

Expected Output

In [4]: idx.to_series(index=False)
Out[4]: 
0     Ant
1    Bear
2     Cow
Name: animal, dtype: object

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 6ea1855
python : 3.8.2.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.16299
machine : AMD64
processor : Intel64 Family 6 Model 61 Stepping 4, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : English_United States.1252

pandas : 1.1.0.dev0+1650.g6ea185548
numpy : 1.17.5
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 46.4.0.post20200518
Cython : 0.29.19
pytest : 5.4.2
hypothesis : 5.15.1
sphinx : 3.0.3
blosc : None
feather : None
xlsxwriter : 1.2.8
lxml.etree : 4.5.1
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.14.0
pandas_datareader: None
bs4 : 4.9.1
bottleneck : 1.3.2
fastparquet : 0.4.0
gcsfs : None
matplotlib : 3.2.1
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : 0.17.1
pytables : None
pyxlsb : None
s3fs : 0.4.2
scipy : 1.3.2
sqlalchemy : 1.3.17
tables : 3.6.1
tabulate : 0.8.7
xarray : 0.15.1
xlrd : 1.2.0
xlwt : 1.3.0
numba : 0.49.1

If possible, I would really love to work on this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    API - ConsistencyInternal Consistency of API/BehaviorEnhancementIndexRelated to the Index class or subclassesNeeds DiscussionRequires discussion from core team before further action

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions