Skip to content

API should empty ._get_bool_data have RangeIndex? #50862

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
MarcoGorelli opened this issue Jan 19, 2023 · 3 comments · Fixed by #59035
Closed

API should empty ._get_bool_data have RangeIndex? #50862

MarcoGorelli opened this issue Jan 19, 2023 · 3 comments · Fixed by #59035
Labels
API Design Index Related to the Index class or subclasses

Comments

@MarcoGorelli
Copy link
Member

This came up in #50636

In [9]: Series([], dtype='bool').index
Out[9]: RangeIndex(start=0, stop=0, step=1)

In [10]: Series([], dtype='bool')._get_bool_data().index
Out[10]: NumericIndex([], dtype='int64')

cc @topper-123

@MarcoGorelli MarcoGorelli added API Design Index Related to the Index class or subclasses labels Jan 19, 2023
@jbrockmendel
Copy link
Member

I'd expect it to always have the same index type as the original DataFrame

@topper-123
Copy link
Contributor

I'd expect it to always have the same index type as the original DataFrame

+1.

@topper-123
Copy link
Contributor

I've looked a bit into this. The underlying issue is that _get_bool_data() does an implicit slice and slicing a RangeIndex always returns a NumericIndex[dtype=int64].

>>> import pandas as pd
>>> ri = RangeIndex(0)
>>> ri[[]]
NumericIndex([], dtype='int64')  # soon Index([], dtype='int64')

I think the above is correct, i.e. slicing a RangeIndex should always return the same index type, i.e. a NumericIndex, even if its has length 0. SO IMO RangeIndex behaves correctly.

So the issue here seems to be in BaseBlockManager._combine where the use of slicing the index is forced, which does seems to be not needed in this case. So the question is maybe if slicing there should be not done in some case (especially where there is only 1 block).

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

Successfully merging a pull request may close this issue.

3 participants