Skip to content

DOC: trim error traceback in allows_duplicate_labels whatsnew entry #38184

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
Changes from all commits
Commits
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
50 changes: 34 additions & 16 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,45 @@ prevent accidental introduction of duplicate labels, which can affect downstream

By default, duplicates continue to be allowed.

.. ipython:: python
.. code-block:: ipython

pd.Series([1, 2], index=['a', 'a'])
In [1]: pd.Series([1, 2], index=['a', 'a'])
Copy link
Contributor

Choose a reason for hiding this comment

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

technically you don't need to change the block on data creation, but nbd

Copy link
Member Author

Choose a reason for hiding this comment

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

By keeping it in a single directive, it gives a consistent code block (not 2 different code blocks with different look or inconsistent number), so in this case nicer I think.

Out[1]:
a 1
a 2
Length: 2, dtype: int64

.. ipython:: python
:okexcept:

pd.Series([1, 2], index=['a', 'a']).set_flags(allows_duplicate_labels=False)
In [2]: pd.Series([1, 2], index=['a', 'a']).set_flags(allows_duplicate_labels=False)
...
DuplicateLabelError: Index has duplicates.
positions
label
a [0, 1]

pandas will propagate the ``allows_duplicate_labels`` property through many operations.

.. ipython:: python
:okexcept:

a = (
pd.Series([1, 2], index=['a', 'b'])
.set_flags(allows_duplicate_labels=False)
)
a
# An operation introducing duplicates
a.reindex(['a', 'b', 'a'])
.. code-block:: ipython

In [3]: a = (
...: pd.Series([1, 2], index=['a', 'b'])
...: .set_flags(allows_duplicate_labels=False)
...: )

In [4]: a
Out[4]:
a 1
b 2
Length: 2, dtype: int64

# An operation introducing duplicates
In [5]: a.reindex(['a', 'b', 'a'])
...
DuplicateLabelError: Index has duplicates.
positions
label
a [0, 2]

[1 rows x 1 columns]

.. warning::

Expand Down