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

40 statements  

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

1""" define generic base classes for pandas objects """ 

2from __future__ import annotations 

3 

4from typing import ( 

5 TYPE_CHECKING, 

6 Type, 

7 cast, 

8) 

9 

10if TYPE_CHECKING: 10 ↛ 11line 10 didn't jump to line 11, because the condition on line 10 was never true

11 from pandas import ( 

12 Categorical, 

13 CategoricalIndex, 

14 DataFrame, 

15 DatetimeIndex, 

16 Float64Index, 

17 Index, 

18 Int64Index, 

19 IntervalIndex, 

20 MultiIndex, 

21 PeriodIndex, 

22 RangeIndex, 

23 Series, 

24 TimedeltaIndex, 

25 UInt64Index, 

26 ) 

27 from pandas.core.arrays import ( 

28 DatetimeArray, 

29 ExtensionArray, 

30 PandasArray, 

31 PeriodArray, 

32 TimedeltaArray, 

33 ) 

34 from pandas.core.generic import NDFrame 

35 

36 

37# define abstract base classes to enable isinstance type checking on our 

38# objects 

39def create_pandas_abc_type(name, attr, comp): 

40 def _check(inst): 

41 return getattr(inst, attr, "_typ") in comp 

42 

43 # https://github.com/python/mypy/issues/1006 

44 # error: 'classmethod' used with a non-method 

45 @classmethod # type: ignore[misc] 

46 def _instancecheck(cls, inst) -> bool: 

47 return _check(inst) and not isinstance(inst, type) 

48 

49 @classmethod # type: ignore[misc] 

50 def _subclasscheck(cls, inst) -> bool: 

51 # Raise instead of returning False 

52 # This is consistent with default __subclasscheck__ behavior 

53 if not isinstance(inst, type): 53 ↛ 54line 53 didn't jump to line 54, because the condition on line 53 was never true

54 raise TypeError("issubclass() arg 1 must be a class") 

55 

56 return _check(inst) 

57 

58 dct = {"__instancecheck__": _instancecheck, "__subclasscheck__": _subclasscheck} 

59 meta = type("ABCBase", (type,), dct) 

60 return meta(name, (), dct) 

61 

62 

63ABCInt64Index = cast( 

64 "Type[Int64Index]", 

65 create_pandas_abc_type("ABCInt64Index", "_typ", ("int64index",)), 

66) 

67ABCUInt64Index = cast( 

68 "Type[UInt64Index]", 

69 create_pandas_abc_type("ABCUInt64Index", "_typ", ("uint64index",)), 

70) 

71ABCRangeIndex = cast( 

72 "Type[RangeIndex]", 

73 create_pandas_abc_type("ABCRangeIndex", "_typ", ("rangeindex",)), 

74) 

75ABCFloat64Index = cast( 

76 "Type[Float64Index]", 

77 create_pandas_abc_type("ABCFloat64Index", "_typ", ("float64index",)), 

78) 

79ABCMultiIndex = cast( 

80 "Type[MultiIndex]", 

81 create_pandas_abc_type("ABCMultiIndex", "_typ", ("multiindex",)), 

82) 

83ABCDatetimeIndex = cast( 

84 "Type[DatetimeIndex]", 

85 create_pandas_abc_type("ABCDatetimeIndex", "_typ", ("datetimeindex",)), 

86) 

87ABCTimedeltaIndex = cast( 

88 "Type[TimedeltaIndex]", 

89 create_pandas_abc_type("ABCTimedeltaIndex", "_typ", ("timedeltaindex",)), 

90) 

91ABCPeriodIndex = cast( 

92 "Type[PeriodIndex]", 

93 create_pandas_abc_type("ABCPeriodIndex", "_typ", ("periodindex",)), 

94) 

95ABCCategoricalIndex = cast( 

96 "Type[CategoricalIndex]", 

97 create_pandas_abc_type("ABCCategoricalIndex", "_typ", ("categoricalindex",)), 

98) 

99ABCIntervalIndex = cast( 

100 "Type[IntervalIndex]", 

101 create_pandas_abc_type("ABCIntervalIndex", "_typ", ("intervalindex",)), 

102) 

103ABCIndex = cast( 

104 "Type[Index]", 

105 create_pandas_abc_type( 

106 "ABCIndex", 

107 "_typ", 

108 { 

109 "index", 

110 "int64index", 

111 "rangeindex", 

112 "float64index", 

113 "uint64index", 

114 "numericindex", 

115 "multiindex", 

116 "datetimeindex", 

117 "timedeltaindex", 

118 "periodindex", 

119 "categoricalindex", 

120 "intervalindex", 

121 }, 

122 ), 

123) 

124 

125 

126ABCNDFrame = cast( 

127 "Type[NDFrame]", 

128 create_pandas_abc_type("ABCNDFrame", "_typ", ("series", "dataframe")), 

129) 

130ABCSeries = cast( 

131 "Type[Series]", 

132 create_pandas_abc_type("ABCSeries", "_typ", ("series",)), 

133) 

134ABCDataFrame = cast( 

135 "Type[DataFrame]", create_pandas_abc_type("ABCDataFrame", "_typ", ("dataframe",)) 

136) 

137 

138ABCCategorical = cast( 

139 "Type[Categorical]", 

140 create_pandas_abc_type("ABCCategorical", "_typ", ("categorical")), 

141) 

142ABCDatetimeArray = cast( 

143 "Type[DatetimeArray]", 

144 create_pandas_abc_type("ABCDatetimeArray", "_typ", ("datetimearray")), 

145) 

146ABCTimedeltaArray = cast( 

147 "Type[TimedeltaArray]", 

148 create_pandas_abc_type("ABCTimedeltaArray", "_typ", ("timedeltaarray")), 

149) 

150ABCPeriodArray = cast( 

151 "Type[PeriodArray]", 

152 create_pandas_abc_type("ABCPeriodArray", "_typ", ("periodarray",)), 

153) 

154ABCExtensionArray = cast( 

155 "Type[ExtensionArray]", 

156 create_pandas_abc_type( 

157 "ABCExtensionArray", 

158 "_typ", 

159 # Note: IntervalArray and SparseArray are included bc they have _typ="extension" 

160 {"extension", "categorical", "periodarray", "datetimearray", "timedeltaarray"}, 

161 ), 

162) 

163ABCPandasArray = cast( 

164 "Type[PandasArray]", 

165 create_pandas_abc_type("ABCPandasArray", "_typ", ("npy_extension",)), 

166)