Optionally disallow duplicate labels by TomAugspurger · Pull Request #28394 · pandas-dev/pandas (original) (raw)

It's truncated, following our usual repr's settings. I'll remove the todo

In [3]: s = pd.Series(1, index=[0] * 200).set_flags(allows_duplicate_labels=False)

DuplicateLabelError Traceback (most recent call last) in ----> 1 s = pd.Series(1, index=[0] * 200).set_flags(allows_duplicate_labels=False)

~/sandbox/pandas/pandas/core/generic.py in set_flags(self, copy, allows_duplicate_labels) 349 df = self.copy(deep=copy) 350 if allows_duplicate_labels is not None: --> 351 df.flags["allows_duplicate_labels"] = allows_duplicate_labels 352 return df 353

~/sandbox/pandas/pandas/core/flags.py in setitem(self, key, value) 103 if key not in self._keys: 104 raise ValueError(f"Unknown flag {key}. Must be one of {self._keys}") --> 105 setattr(self, key, value) 106 107 def repr(self):

~/sandbox/pandas/pandas/core/flags.py in allows_duplicate_labels(self, value) 90 if not value: 91 for ax in obj.axes: ---> 92 ax._maybe_check_unique() 93 94 self._allows_duplicate_labels = value

~/sandbox/pandas/pandas/core/indexes/base.py in _maybe_check_unique(self) 491 msg += "\n{}".format(duplicates) 492 --> 493 raise DuplicateLabelError(msg) 494 495 def _format_duplicate_message(self):

DuplicateLabelError: Index has duplicates. positions label 0 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,...

In [10]: s = pd.Series(1, index=np.arange(100).repeat(10)).set_flags(allows_duplicate_labels=False)

DuplicateLabelError Traceback (most recent call last) in ----> 1 s = pd.Series(1, index=np.arange(100).repeat(10)).set_flags(allows_duplicate_labels=False)

~/sandbox/pandas/pandas/core/generic.py in set_flags(self, copy, allows_duplicate_labels) 349 df = self.copy(deep=copy) 350 if allows_duplicate_labels is not None: --> 351 df.flags["allows_duplicate_labels"] = allows_duplicate_labels 352 return df 353

~/sandbox/pandas/pandas/core/flags.py in setitem(self, key, value) 103 if key not in self._keys: 104 raise ValueError(f"Unknown flag {key}. Must be one of {self._keys}") --> 105 setattr(self, key, value) 106 107 def repr(self):

~/sandbox/pandas/pandas/core/flags.py in allows_duplicate_labels(self, value) 90 if not value: 91 for ax in obj.axes: ---> 92 ax._maybe_check_unique() 93 94 self._allows_duplicate_labels = value

~/sandbox/pandas/pandas/core/indexes/base.py in _maybe_check_unique(self) 491 msg += "\n{}".format(duplicates) 492 --> 493 raise DuplicateLabelError(msg) 494 495 def _format_duplicate_message(self):

DuplicateLabelError: Index has duplicates. positions label 0 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 1 [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] 2 [20, 21, 22, 23, 24, 25, 26, 27, 28, 29] 3 [30, 31, 32, 33, 34, 35, 36, 37, 38, 39] 4 [40, 41, 42, 43, 44, 45, 46, 47, 48, 49] ... ... 95 [950, 951, 952, 953, 954, 955, 956, 957, 958, ... 96 [960, 961, 962, 963, 964, 965, 966, 967, 968, ... 97 [970, 971, 972, 973, 974, 975, 976, 977, 978, ... 98 [980, 981, 982, 983, 984, 985, 986, 987, 988, ... 99 [990, 991, 992, 993, 994, 995, 996, 997, 998, ...

[100 rows x 1 columns]