Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pandas/core/indexes/frozen.py: 52%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

44 statements  

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 # error: Incompatible types in assignment (expression has type 

69 # "Callable[[FrozenList, Any], FrozenList]", base class "list" defined the 

70 # type as overloaded function) 

71 __add__ = __iadd__ = union # type: ignore[assignment] 

72 

73 def __getitem__(self, n): 

74 if isinstance(n, slice): 

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

76 return super().__getitem__(n) 

77 

78 def __radd__(self, other): 

79 if isinstance(other, tuple): 

80 other = list(other) 

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

82 

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

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

85 other = list(other) 

86 return super().__eq__(other) 

87 

88 __req__ = __eq__ 

89 

90 def __mul__(self, other): 

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

92 

93 __imul__ = __mul__ 

94 

95 def __reduce__(self): 

96 return type(self), (list(self),) 

97 

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

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

100 return hash(tuple(self)) 

101 

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

103 """ 

104 This method will not function because object is immutable. 

105 """ 

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

107 

108 def __str__(self) -> str: 

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

110 

111 def __repr__(self) -> str: 

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

113 

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

115 __delitem__ = __delslice__ = _disabled 

116 pop = append = extend = _disabled 

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