Skip to content

setitem on empty DataFrame / Series casts to RangeIndex #17101

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
TomAugspurger opened this issue Jul 27, 2017 · 2 comments · Fixed by #26471
Closed

setitem on empty DataFrame / Series casts to RangeIndex #17101

TomAugspurger opened this issue Jul 27, 2017 · 2 comments · Fixed by #26471
Labels
Dtype Conversions Unexpected or buggy dtype conversions good first issue Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@TomAugspurger
Copy link
Contributor

I thought we had an issue for this, but couldn't find it.

In [41]: df = pd.DataFrame(columns=['A'])

In [42]: df.index.dtype
Out[42]: dtype('O')

In [43]: df['A'] = []

In [44]: df.index.dtype
Out[44]: dtype('int64')

In [45]: df.index
Out[45]: RangeIndex(start=0, stop=0, step=1)

I this should still be empty an empty Index (though see #16961 which proposes change pd.Series() to have that use a range index. Not sure if that's a good idea or not).

xref dask/dask#2564

@gfyoung gfyoung added Dtype Conversions Unexpected or buggy dtype conversions Indexing Related to indexing on series/frames, not to indexes themselves labels Jul 27, 2017
@TomAugspurger
Copy link
Contributor Author

It's possible that adding an and len(value) to the if statement in

pandas/pandas/core/frame.py

Lines 3386 to 3389 in ee6b131

# GH5632, make sure that we are a Series convertible
if not len(self.index) and is_list_like(value):
try:
value = Series(value)

will fix things. That plus a test would make a good first PR.

@SvoONs
Copy link

SvoONs commented May 17, 2019

I would like to fix this as my first PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions good first issue Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants