-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
Optionally disallow duplicate labels #28394
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 1 commit
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
090a758
ENH: Optionally disallow duplicate labels
TomAugspurger 0962d19
fixup memory_usage test
TomAugspurger 8e0089c
pickle
TomAugspurger 6a2d568
Merge remote-tracking branch 'upstream/master' into unique-index
TomAugspurger b9873db
lint
TomAugspurger d326ed5
doc
TomAugspurger fba3536
fixups
TomAugspurger 7de8327
Merge remote-tracking branch 'upstream/master' into unique-index
TomAugspurger f1e5932
handle concat
TomAugspurger c0f6390
Merge remote-tracking branch 'upstream/master' into unique-index
TomAugspurger 3dad6d5
handle mi
TomAugspurger e81327d
note on setting
TomAugspurger 3254b8b
fixup
TomAugspurger 04b6322
fix import, docs
TomAugspurger 0c3db5b
Merge remote-tracking branch 'upstream/master' into unique-index
TomAugspurger fdcdb31
handle insert
TomAugspurger cb7aeb3
Merge remote-tracking branch 'upstream/master' into unique-index
TomAugspurger 7d71326
wip inplace
TomAugspurger c0a6105
Merge remote-tracking branch 'upstream/master' into unique-index
TomAugspurger 3fa067d
tests for inplace duplicates
TomAugspurger 91ca7a1
move to generic
TomAugspurger 097dd1c
fixup
TomAugspurger 8248634
add note
TomAugspurger df42b44
Merge remote-tracking branch 'upstream/master' into unique-index
TomAugspurger aff9303
update
TomAugspurger 64334ca
update
TomAugspurger bef80bd
update
TomAugspurger 7d09c8b
fixup docs
TomAugspurger 674cb97
typing
TomAugspurger cc80b02
fixed typo
TomAugspurger c9030f1
Merge remote-tracking branch 'upstream/master' into unique-index
TomAugspurger 457683c
Merge remote-tracking branch 'upstream/master' into unique-index
TomAugspurger 4ced351
todo
TomAugspurger f5bb12c
Merge remote-tracking branch 'upstream/master' into unique-index
TomAugspurger 4f7c350
flags
TomAugspurger ecb97d5
rm _flags
TomAugspurger d1a81fb
fixups
TomAugspurger 74a4eb8
lint
TomAugspurger 50042e1
lint
TomAugspurger 6a1560f
Merge remote-tracking branch 'upstream/master' into unique-index
TomAugspurger f61118d
Merge remote-tracking branch 'upstream/master' into unique-index
TomAugspurger a336027
Pickle
TomAugspurger 60c853c
Doc
TomAugspurger cb5d4f2
test flags
TomAugspurger 98af7b5
pickle
TomAugspurger 3a50741
remove debug
TomAugspurger 675e49d
Merge remote-tracking branch 'upstream/master' into unique-index
TomAugspurger e88f7e2
Merge remote-tracking branch 'upstream/master' into unique-index
TomAugspurger bf23fda
fixups
TomAugspurger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
todo
- Loading branch information
commit 4ced35143cc8809b24ddb2594c424ad0397b9ce9
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import weakref | ||
|
||
|
||
class Flags: | ||
""" | ||
Flags that apply to pandas objects. | ||
|
||
Parameters | ||
---------- | ||
obj : Series or DataFrame | ||
The object these flags are associated with | ||
allows_duplicate_labels : bool | ||
Whether the object allows duplicate labels | ||
|
||
Notes | ||
----- | ||
Attributes can be set in two ways | ||
|
||
>>> df = pd.DataFrame() | ||
>>> df.flags | ||
<Flags(allows_duplicate_labels=True)> | ||
>>> df.flags.allows_duplicate_labels = False | ||
>>> df.flags | ||
<Flags(allows_duplicate_labels=False)> | ||
|
||
>>> df.flags['allows_duplicate_labels'] = True | ||
>>> df.flags | ||
<Flags(allows_duplicate_labels=True)> | ||
""" | ||
|
||
_keys = {"allows_duplicate_labels"} | ||
|
||
def __init__(self, obj, *, allows_duplicate_labels): | ||
self._allows_duplicate_labels = allows_duplicate_labels | ||
self._obj = weakref.ref(obj) | ||
|
||
@property | ||
def allows_duplicate_labels(self) -> bool: | ||
""" | ||
Whether this object allows duplicate labels. | ||
|
||
Setting ``allows_duplicate_labels=False`` ensures that the | ||
index (and columns of a DataFrame) are unique. Most methods | ||
that accept and return a Series or DataFrame will propagate | ||
the value of ``allows_duplicate_labels``. | ||
|
||
See :ref:`duplicates` for more. | ||
|
||
See Also | ||
-------- | ||
DataFrame.attrs : Set global metadata on this object. | ||
DataFrame.set_flags : Set global flags on this object. | ||
|
||
Examples | ||
-------- | ||
>>> df = pd.DataFrame({"A": [1, 2]}, index=['a', 'a']) | ||
>>> df.allows_duplicate_labels | ||
True | ||
>>> df.allows_duplicate_labels = False | ||
Traceback (most recent call last): | ||
... | ||
pandas.errors.DuplicateLabelError: Index has duplicates. | ||
positions | ||
label | ||
a [0, 1] | ||
""" | ||
return self._allows_duplicate_labels | ||
|
||
@allows_duplicate_labels.setter | ||
def allows_duplicate_labels(self, value: bool): | ||
value = bool(value) | ||
obj = self._obj() | ||
if obj is None: | ||
raise ValueError("This flags object has been deleted.") | ||
|
||
if not value: | ||
for ax in obj.axes: | ||
ax._maybe_check_unique() | ||
|
||
self._allows_duplicate_labels = value | ||
|
||
def __getitem__(self, key): | ||
if key not in self._keys: | ||
raise KeyError(key) | ||
|
||
return getattr(self, key) | ||
|
||
def __setitem__(self, key, value): | ||
if key not in self._keys: | ||
raise ValueError(f"Unknown flag {key}. Must be one of {self._keys}") | ||
setattr(self, key, value) | ||
|
||
def __repr__(self): | ||
return f"<Flags(allows_duplicate_labels={self.allows_duplicate_labels})>" | ||
|
||
def __eq__(self, other): | ||
if isinstance(other, type(self)): | ||
return self.allows_duplicate_labels == other.allows_duplicate_labels | ||
return False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.