Skip to content

BUG: Empty dataframe created with Index, not RangeIndex #40077

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

Closed
2 of 3 tasks
connesy opened this issue Feb 26, 2021 · 2 comments · Fixed by #49637
Closed
2 of 3 tasks

BUG: Empty dataframe created with Index, not RangeIndex #40077

connesy opened this issue Feb 26, 2021 · 2 comments · Fixed by #49637
Labels
API - Consistency Internal Consistency of API/Behavior Bug Constructors Series/DataFrame/Index/pd.array Constructors Index Related to the Index class or subclasses

Comments

@connesy
Copy link
Contributor

connesy commented Feb 26, 2021

  • 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
>>> import pandas as pd
>>> df = pd.DataFrame(columns=['a', 'b'])
>>> print(df.index)
Index([], dtype='object')

>>> df2 = df.append(df, ignore_index=True)
>>> print(df2.index)
RangeIndex(start=0, stop=0, step=1)

>>> pd.testing.assert_frame_equal(df, df2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/stefan/miniconda3/envs/pandas/lib/python3.9/site-packages/pandas/_testing.py", line 1657, in assert_frame_equal
    assert_index_equal(
  File "/home/stefan/miniconda3/envs/pandas/lib/python3.9/site-packages/pandas/_testing.py", line 773, in assert_index_equal
    _check_types(left, right, obj=obj)
  File "/home/stefan/miniconda3/envs/pandas/lib/python3.9/site-packages/pandas/_testing.py", line 740, in _check_types
    assert_class_equal(left, right, exact=exact, obj=obj)
  File "/home/stefan/miniconda3/envs/pandas/lib/python3.9/site-packages/pandas/_testing.py", line 868, in assert_class_equal
    raise_assert_detail(obj, msg, repr_class(left), repr_class(right))
  File "/home/stefan/miniconda3/envs/pandas/lib/python3.9/site-packages/pandas/_testing.py", line 1073, in raise_assert_detail
    raise AssertionError(msg)
AssertionError: DataFrame.index are different

DataFrame.index classes are not equivalent
[left]:  Index([], dtype='object')
[right]: RangeIndex(start=0, stop=0, step=1)

Problem description

When creating an empty dataframe it is created with Index, and not with a RangeIndex as specified in the docs. This is a problem, e.g. in the example shown above, where I would not expect the index to change when appending the empty dataframe to itself. However, pd.testing.assert_frame_equals raises AssertionError since the indeces are not equivalent.

Expected Output

>>> df = pd.DataFrame(columns=['a', 'b'])
>>> print(df.index)
RangeIndex(start=0, stop=0, step=1)

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 7d32926
python : 3.9.2.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-59-generic
Version : #65-Ubuntu SMP Thu Dec 10 12:01:51 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.2
numpy : 1.20.1
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 49.6.0.post20210108
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@connesy connesy added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 26, 2021
@alexprincel
Copy link
Contributor

alexprincel commented Feb 27, 2021

The following code in pandas.core.internals.construction seems to be the culprit. I do not think I have enough maturity with the Pandas API to understand the code-breaking potential of editing the following from Index to RangeIndex or to reorganize the code without the length check:

def extract_index(data) -> Index:
    """
    Try to infer an Index from the passed data, raise ValueError on failure.
    """
    index = None
    if len(data) == 0:
        index = Index([])
    elif len(data) > 0:
        # ...

    return ensure_index(index)

@mzeitlin11
Copy link
Member

Persists on current master, contributions welcome to fix this!

@mzeitlin11 mzeitlin11 added API - Consistency Internal Consistency of API/Behavior Constructors Series/DataFrame/Index/pd.array Constructors Index Related to the Index class or subclasses and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 1, 2021
@mzeitlin11 mzeitlin11 added this to the Contributions Welcome milestone Jul 1, 2021
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API - Consistency Internal Consistency of API/Behavior Bug Constructors Series/DataFrame/Index/pd.array Constructors Index Related to the Index class or subclasses
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants