Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2frozen (immutable) data structures to support MultiIndexing 

3 

4These are used for: 

5 

6- .names (FrozenList) 

7 

8""" 

9 

10from typing import Any 

11 

12from pandas.core.base import PandasObject 

13 

14from pandas.io.formats.printing import pprint_thing 

15 

16 

17class FrozenList(PandasObject, list): 

18 """ 

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

20 because it's technically non-hashable, will be used 

21 for lookups, appropriately, etc. 

22 """ 

23 

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

25 # it messes up PyTables type checks. 

26 

27 def union(self, other) -> "FrozenList": 

28 """ 

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

30 

31 Parameters 

32 ---------- 

33 other : array-like 

34 The array-like whose elements we are concatenating. 

35 

36 Returns 

37 ------- 

38 FrozenList 

39 The collection difference between self and other. 

40 """ 

41 if isinstance(other, tuple): 

42 other = list(other) 

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

44 

45 def difference(self, other) -> "FrozenList": 

46 """ 

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

48 

49 Parameters 

50 ---------- 

51 other : array-like 

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

53 

54 Returns 

55 ------- 

56 FrozenList 

57 The collection difference between self and other. 

58 """ 

59 other = set(other) 

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

61 return type(self)(temp) 

62 

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

64 __add__ = __iadd__ = union 

65 

66 def __getitem__(self, n): 

67 if isinstance(n, slice): 

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

69 return super().__getitem__(n) 

70 

71 def __radd__(self, other): 

72 if isinstance(other, tuple): 

73 other = list(other) 

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

75 

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

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

78 other = list(other) 

79 return super().__eq__(other) 

80 

81 __req__ = __eq__ 

82 

83 def __mul__(self, other): 

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

85 

86 __imul__ = __mul__ 

87 

88 def __reduce__(self): 

89 return type(self), (list(self),) 

90 

91 def __hash__(self): 

92 return hash(tuple(self)) 

93 

94 def _disabled(self, *args, **kwargs): 

95 """ 

96 This method will not function because object is immutable. 

97 """ 

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

99 

100 def __str__(self) -> str: 

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

102 

103 def __repr__(self) -> str: 

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

105 

106 __setitem__ = __setslice__ = __delitem__ = __delslice__ = _disabled 

107 pop = append = extend = remove = sort = insert = _disabled