diff --git a/pandas/_libs/properties.pyx b/pandas/_libs/properties.pyx index e3f16f224db1c..0f2900619fdb6 100644 --- a/pandas/_libs/properties.pyx +++ b/pandas/_libs/properties.pyx @@ -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 diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 22ef2fe7aa19e..ff9c86fbfe384 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -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