From 59116cf1bb7b66a54b7a18e4f491e4700c02fc47 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 26 Mar 2018 14:48:49 +0200 Subject: [PATCH] BUG: raise error when setting cached properties (#20487) --- pandas/_libs/properties.pyx | 3 +++ pandas/tests/indexes/test_base.py | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/pandas/_libs/properties.pyx b/pandas/_libs/properties.pyx index e3f16f224db1ce..0f2900619fdb68 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 22ef2fe7aa19e1..ff9c86fbfe3840 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