Skip to content

BUG: raise error when setting cached properties #20487

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
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions pandas/_libs/properties.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ cdef class CachedProperty(object):
PyDict_SetItem(cache, self.name, val)
return val

def __set__(self, obj, value):
raise AttributeError("Can't set attribute")


cache_readonly = CachedProperty

Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2056,6 +2056,11 @@ def test_iadd_preserves_name(self):
ser.index -= 1
assert ser.index.name == "foo"

def test_cached_properties_not_settable(self):
idx = pd.Index([1, 2, 3])
with tm.assert_raises_regex(AttributeError, "Can't set attribute"):
idx.is_unique = False


class TestMixedIntIndex(Base):
# Mostly the tests from common.py for which the results differ
Expand Down