Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/pandas/core/flags.py: 31%

34 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1from __future__ import annotations 

2 

3import weakref 

4 

5 

6class Flags: 

7 """ 

8 Flags that apply to pandas objects. 

9 

10 .. versionadded:: 1.2.0 

11 

12 Parameters 

13 ---------- 

14 obj : Series or DataFrame 

15 The object these flags are associated with. 

16 allows_duplicate_labels : bool, default True 

17 Whether to allow duplicate labels in this object. By default, 

18 duplicate labels are permitted. Setting this to ``False`` will 

19 cause an :class:`errors.DuplicateLabelError` to be raised when 

20 `index` (or columns for DataFrame) is not unique, or any 

21 subsequent operation on introduces duplicates. 

22 See :ref:`duplicates.disallow` for more. 

23 

24 .. warning:: 

25 

26 This is an experimental feature. Currently, many methods fail to 

27 propagate the ``allows_duplicate_labels`` value. In future versions 

28 it is expected that every method taking or returning one or more 

29 DataFrame or Series objects will propagate ``allows_duplicate_labels``. 

30 

31 Notes 

32 ----- 

33 Attributes can be set in two ways 

34 

35 >>> df = pd.DataFrame() 

36 >>> df.flags 

37 <Flags(allows_duplicate_labels=True)> 

38 >>> df.flags.allows_duplicate_labels = False 

39 >>> df.flags 

40 <Flags(allows_duplicate_labels=False)> 

41 

42 >>> df.flags['allows_duplicate_labels'] = True 

43 >>> df.flags 

44 <Flags(allows_duplicate_labels=True)> 

45 """ 

46 

47 _keys = {"allows_duplicate_labels"} 

48 

49 def __init__(self, obj, *, allows_duplicate_labels) -> None: 

50 self._allows_duplicate_labels = allows_duplicate_labels 

51 self._obj = weakref.ref(obj) 

52 

53 @property 

54 def allows_duplicate_labels(self) -> bool: 

55 """ 

56 Whether this object allows duplicate labels. 

57 

58 Setting ``allows_duplicate_labels=False`` ensures that the 

59 index (and columns of a DataFrame) are unique. Most methods 

60 that accept and return a Series or DataFrame will propagate 

61 the value of ``allows_duplicate_labels``. 

62 

63 See :ref:`duplicates` for more. 

64 

65 See Also 

66 -------- 

67 DataFrame.attrs : Set global metadata on this object. 

68 DataFrame.set_flags : Set global flags on this object. 

69 

70 Examples 

71 -------- 

72 >>> df = pd.DataFrame({"A": [1, 2]}, index=['a', 'a']) 

73 >>> df.flags.allows_duplicate_labels 

74 True 

75 >>> df.flags.allows_duplicate_labels = False 

76 Traceback (most recent call last): 

77 ... 

78 pandas.errors.DuplicateLabelError: Index has duplicates. 

79 positions 

80 label 

81 a [0, 1] 

82 """ 

83 return self._allows_duplicate_labels 

84 

85 @allows_duplicate_labels.setter 

86 def allows_duplicate_labels(self, value: bool) -> None: 

87 value = bool(value) 

88 obj = self._obj() 

89 if obj is None: 

90 raise ValueError("This flag's object has been deleted.") 

91 

92 if not value: 

93 for ax in obj.axes: 

94 ax._maybe_check_unique() 

95 

96 self._allows_duplicate_labels = value 

97 

98 def __getitem__(self, key): 

99 if key not in self._keys: 

100 raise KeyError(key) 

101 

102 return getattr(self, key) 

103 

104 def __setitem__(self, key, value) -> None: 

105 if key not in self._keys: 

106 raise ValueError(f"Unknown flag {key}. Must be one of {self._keys}") 

107 setattr(self, key, value) 

108 

109 def __repr__(self) -> str: 

110 return f"<Flags(allows_duplicate_labels={self.allows_duplicate_labels})>" 

111 

112 def __eq__(self, other): 

113 if isinstance(other, type(self)): 

114 return self.allows_duplicate_labels == other.allows_duplicate_labels 

115 return False