Skip to content

DOC:Improve the docstring of DataFrame.iloc() #20228

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

Merged
merged 12 commits into from
Jul 7, 2018
34 changes: 31 additions & 3 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1629,7 +1629,8 @@ def _getitem_axis(self, key, axis=None):


class _iLocIndexer(_LocationIndexer):
"""Purely integer-location based indexing for selection by position.
"""
Purely integer-location based indexing for selection by position.

``.iloc[]`` is primarily integer position based (from ``0`` to
``length-1`` of the axis), but may also be used with a boolean
Expand All @@ -1642,14 +1643,41 @@ class _iLocIndexer(_LocationIndexer):
- A slice object with ints, e.g. ``1:7``.
- A boolean array.
- A ``callable`` function with one argument (the calling Series, DataFrame
or Panel) and that returns valid output for indexing (one of the above)
or Panel) and that returns valid output for indexing (one of the above).

``.iloc`` will raise ``IndexError`` if a requested indexer is
out-of-bounds, except *slice* indexers which allow out-of-bounds
indexing (this conforms with python/numpy *slice* semantics).

See more at :ref:`Selection by Position <indexing.integer>`
ref:`Selection by Position <indexing.integer>`.

See Also
--------
DataFrame.iat : Fast integer location scalar accessor.
DataFrame.loc : Purely label-location based indexer for selection by label.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add Series.iloc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added Series.iloc

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you look at other PRs for those accessors to have a consistent way to reference them? See eg https://github.com/pandas-dev/pandas/pull/20229/files, They use:

    DateFrame.at : Access a single value for a row/column label pair
    DateFrame.iloc : Access group of rows and columns by integer position(s)
    Series.loc : Access group of values using labels

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tuhinmahmud make sure to pull my changes before looking into this.


Examples
--------
>>> import pandas as pd
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need the pandas import

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed pandas import

>>> mydict = [{'a': 1, 'b': 2, 'c': 3, 'd': 4},
... {'a': 100, 'b': 200, 'c': 300, 'd': 400},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to indent here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undated the indentation

... {'a': 1000, 'b': 2000, 'c': 3000, 'd': 4000 }]
>>> df = pd.DataFrame(mydict)
>>> df
a b c d
0 1 2 3 4
1 100 200 300 400
2 1000 2000 3000 4000
>>> df.iloc[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank lines between cases.

show additional examples, including selecting with .iloc[0] vs .iloc[[0]], and use a multi-axis selction .iloc[0, 0] and lists for the last, e.g. .iloc[[0, 1], [0, 1]]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also add a sentence for each case explaining it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added sentences and put different types of example of .iloc in paragraph

a 1
b 2
c 3
d 4
Name: 0, dtype: int64
>>> df.iloc[0:2]
a b c d
0 1 2 3 4
1 100 200 300 400
"""

_valid_types = ("integer, integer slice (START point is INCLUDED, END "
Expand Down