8000 Unicode III : revenge of the character planes · Pull Request #2104 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

Unicode III : revenge of the character planes #2104

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

Closed
wants to merge 9 commits into from
Next Next commit
ENH: Eliminate __setitem__ in pd.Index
`Index` inherits from np.ndarray, yet implements an immutable datatype,
Pythonic duck-typing would suggest that the presence of __setitem__
signals a mutable datatype, while the overriden implementaion
just raises an exception - a bait and switch.
Python does not offer a really clean way to eliminate an attribute
inherited from a superclass, but overriding __getattribute__ gives
us the same end result.
squash with 218fe0a
  • Loading branch information
y-p committed Oct 21, 2012
commit 61f34056b9a86d351def9d483c0281e350d8c0dc
8 changes: 5 additions & 3 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,11 @@ def __contains__(self, key):
def __hash__(self):
return hash(self.view(np.ndarray))

def __setitem__(self, key, value):
"""Disable the setting of values."""
raise Exception(str(self.__class__) + ' object is immutable')
def __getattribute__(self,name):
if name=="__setitem__": # emulate an Immutable ndarray
raise AttributeError(str(self.__class__) + ' object is immutable')
else:
return object.__getattribute__(self,name)

def __getitem__(self, key):
"""Override numpy.ndarray's __getitem__ method to work as desired"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ def test_to_html_with_classes(self):
<table border="1" class="dataframe sortable draggable">
<tbody>
<tr>
<td>Index([], dtype=object)</td>
<td>Index((), dtype=object)</td>
<td>Empty DataFrame</td>
</tr>
</tbody>
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ def test_sort(self):
self.assertRaises(Exception, self.strIndex.sort)

def test_mutability(self):
self.assertRaises(Exception, self.strIndex.__setitem__, 5, 0)
self.assertRaises(Exception, self.strIndex.__setitem__, slice(1,5), 0)
self.assertFalse(hasattr(self.strIndex,"__setitem__"))

def test_constructor(self):
# regular instance creation
Expand Down
0