Skip to content
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

QST: Indexing over empty DataFrame #41415

Closed
1 of 2 tasks
YarShev opened this issue May 11, 2021 · 4 comments
Closed
1 of 2 tasks

QST: Indexing over empty DataFrame #41415

YarShev opened this issue May 11, 2021 · 4 comments

Comments

@YarShev
Copy link
Contributor

YarShev commented May 11, 2021

  • I have searched the [pandas] tag on StackOverflow for similar questions.

  • I have asked my usage related question on StackOverflow.


Question about pandas

Is it correct behavior when indexing over empty DataFrame?

import pandas
df = pandas.DataFrame()
df
Empty DataFrame
Columns: []
Index: []

df.iloc[:, 3:9] # this doesn't throw an exception
Empty DataFrame
Columns: []
Index: []

df.iloc[:, 3] # this throws the exception
IndexError: single positional indexer is out-of-bounds

df[3:9] # this doesn't throw an exception
Empty DataFrame
Columns: []
Index: []

df[3] # this throws the exception
KeyError: 3
@YarShev YarShev added Needs Triage Issue that has not been reviewed by a pandas team member Usage Question labels May 11, 2021
@attack68
Copy link
Contributor

attack68 commented May 11, 2021

You will find that this is not just an empty DataFrame:

s = pd.Series([1,2])
s.loc[10:20] -> Series([], dtype: int64)
s.iloc[10:20] -> Series([], dtype: int64)

An automated set of indexing constructions can be seen here: #39775

Also a discussion here: #39424

@attack68 attack68 removed the Needs Triage Issue that has not been reviewed by a pandas team member label May 11, 2021
@asishm-wk
Copy link

It's similar for python lists / numpy arrays as well

>>> b = []
>>> b[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> b[3:5]
[]
>>> a = np.array([])
>>> a[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: index 3 is out of bounds for axis 0 with size 0
>>> a[3:5]
array([], dtype=float64)

@YarShev
Copy link
Contributor Author

YarShev commented May 13, 2021

@attack68 , @asishm-wk , thanks! That makes sense.

@mroeschke
Copy link
Member

Closing since the expected behavior has been explained.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants