Skip to content

DOC add example of Series.index #51842

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 27 commits into from
Apr 16, 2023
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5993,6 +5993,54 @@ def mask(

index = properties.AxisProperty(
axis=0, doc="The index (axis labels) of the Series."
"""
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 make this a single text, not concatenating in this way please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, but didn't understand. You mean like this ?
axis=0, doc="The index (axis labels) of the Series." """ A one-dimensional ndarray with hashable type axis labels. """

or this

axis=0, doc="The index (axis labels) of the Series."
""" A one-dimensional ndarray with hashable type axis labels. """

Copy link
Contributor

Choose a reason for hiding this comment

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

@ggold7046

Could you please use triple backticks to format code blocks? Without it, your question is really hard to read.

What @datapythonista means is that you have two strings here that will be concatenated:

a = (
    "The index [...]"
    """

    A one-dimensional [...]
    """
)
print(a)
The index [...]

    A one-dimensional [...]
    

Since you already have a multiline string (triple quotes), you can just merge the two:

b = """\
The index [...]

A one-dimensional [...]
"""
print(b)
The index [...]

A one-dimensional [...]


A one-dimensional ndarray with hashable type axis labels.

Pandas Series.index attribute is used to get or set the index labels of the given Series object.
The object supports both integer- and label-based indexing and provides a host of methods for performing
operations involving the index.
The labels need not be unique but must be of the hashable type.

Returns
-------
index

Examples
--------

**Use the Series.index attribute to set the index label for the given Series object.**

>>> s = pd.Series(['Kolkata', 'Chicago', 'Toronto', 'Lisbon'])
Copy link
Member

Choose a reason for hiding this comment

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

This example is not very realistic and confusing with the City 1, City 2 labels. Maybe something like using the city as index and then making the value a feature like population, average temperature... would make more sense.

Also, since here you're just showing the .index accessor, better create the index with Series(..., index=...) and simply show that your_series.index returns the index.

>>> print(s)
0 Kolkata
1 Chicago
2 Toronto
3 Lisbon
dtype: object

The ``Series.index`` attribute will now be used to set the index label for the given object.

>>> s.index = ['City 1', 'City 2', 'City 3', 'City 4']
>>> print(s)
City 1 Kolkata
City 2 Chicago
City 3 Toronto
City 4 Lisbon
dtype: object

As we can see in the output, the ``Series.index`` attribute has successfully set the index labels for the given Series object.

**We can also assign duplicate or nonunique indexes in Pandas.**

>>> s.index = ['City 1', 'City 1', 'City 3', 'City 3']
>>> print(s)
City 1 Kolkata
City 1 Chicago
City 3 Toronto
City 3 Lisbon
dtype: object
"""
)

# ----------------------------------------------------------------------
Expand Down