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

42 statements  

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

1""" 

2frozen (immutable) data structures to support MultiIndexing 

3 

4These are used for: 

5 

6- .names (FrozenList) 

7 

8""" 

9from __future__ import annotations 

10 

11from typing import ( 

12 Any, 

13 NoReturn, 

14) 

15 

16from pandas.core.base import PandasObject 

17 

18from pandas.io.formats.printing import pprint_thing 

19 

20 

21class FrozenList(PandasObject, list): 

22 """ 

23 Container that doesn't allow setting item *but* 

24 because it's technically hashable, will be used 

25 for lookups, appropriately, etc. 

26 """ 

27 

28 # Side note: This has to be of type list. Otherwise, 

29 # it messes up PyTables type checks. 

30 

31 def union(self, other) -> FrozenList: 

32 """ 

33 Returns a FrozenList with other concatenated to the end of self. 

34 

35 Parameters 

36 ---------- 

37 other : array-like 

38 The array-like whose elements we are concatenating. 

39 

40 Returns 

41 ------- 

42 FrozenList 

43 The collection difference between self and other. 

44 """ 

45 if isinstance(other, tuple): 

46 other = list(other) 

47 return type(self)(super().__add__(other)) 

48 

49 def difference(self, other) -> FrozenList: 

50 """ 

51 Returns a FrozenList with elements from other removed from self. 

52 

53 Parameters 

54 ---------- 

55 other : array-like 

56 The array-like whose elements we are removing self. 

57 

58 Returns 

59 ------- 

60 FrozenList 

61 The collection difference between self and other. 

62 """ 

63 other = set(other) 

64 temp = [x for x in self if x not in other] 

65 return type(self)(temp) 

66 

67 # TODO: Consider deprecating these in favor of `union` (xref gh-15506) 

68 __add__ = __iadd__ = union 

69 

70 def __getitem__(self, n): 

71 if isinstance(n, slice): 

72 return type(self)(super().__getitem__(n)) 

73 return super().__getitem__(n) 

74 

75 def __radd__(self, other): 

76 if isinstance(other, tuple): 

77 other = list(other) 

78 return type(self)(other + list(self)) 

79 

80 def __eq__(self, other: Any) -> bool: 

81 if isinstance(other, (tuple, FrozenList)): 

82 other = list(other) 

83 return super().__eq__(other) 

84 

85 __req__ = __eq__ 

86 

87 def __mul__(self, other): 

88 return type(self)(super().__mul__(other)) 

89 

90 __imul__ = __mul__ 

91 

92 def __reduce__(self): 

93 return type(self), (list(self),) 

94 

95 # error: Signature of "__hash__" incompatible with supertype "list" 

96 def __hash__(self) -> int: # type: ignore[override] 

97 return hash(tuple(self)) 

98 

99 def _disabled(self, *args, **kwargs) -> NoReturn: 

100 """ 

101 This method will not function because object is immutable. 

102 """ 

103 raise TypeError(f"'{type(self).__name__}' does not support mutable operations.") 

104 

105 def __str__(self) -> str: 

106 return pprint_thing(self, quote_strings=True, escape_chars=("\t", "\r", "\n")) 

107 

108 def __repr__(self) -> str: 

109 return f"{type(self).__name__}({str(self)})" 

110 

111 __setitem__ = __setslice__ = _disabled # type: ignore[assignment] 

112 __delitem__ = __delslice__ = _disabled 

113 pop = append = extend = _disabled 

114 remove = sort = insert = _disabled # type: ignore[assignment]