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# flake8: noqa 

2 

3""" 

4Expose public exceptions & warnings 

5""" 

6 

7from pandas._libs.tslibs import NullFrequencyError, OutOfBoundsDatetime 

8 

9 

10class PerformanceWarning(Warning): 

11 """ 

12 Warning raised when there is a possible performance impact. 

13 """ 

14 

15 

16class UnsupportedFunctionCall(ValueError): 

17 """ 

18 Exception raised when attempting to call a numpy function 

19 on a pandas object, but that function is not supported by 

20 the object e.g. ``np.cumsum(groupby_object)``. 

21 """ 

22 

23 

24class UnsortedIndexError(KeyError): 

25 """ 

26 Error raised when attempting to get a slice of a MultiIndex, 

27 and the index has not been lexsorted. Subclass of `KeyError`. 

28 """ 

29 

30 

31class ParserError(ValueError): 

32 """ 

33 Exception that is raised by an error encountered in parsing file contents. 

34 

35 This is a generic error raised for errors encountered when functions like 

36 `read_csv` or `read_html` are parsing contents of a file. 

37 

38 See Also 

39 -------- 

40 read_csv : Read CSV (comma-separated) file into a DataFrame. 

41 read_html : Read HTML table into a DataFrame. 

42 """ 

43 

44 

45class DtypeWarning(Warning): 

46 """ 

47 Warning raised when reading different dtypes in a column from a file. 

48 

49 Raised for a dtype incompatibility. This can happen whenever `read_csv` 

50 or `read_table` encounter non-uniform dtypes in a column(s) of a given 

51 CSV file. 

52 

53 See Also 

54 -------- 

55 read_csv : Read CSV (comma-separated) file into a DataFrame. 

56 read_table : Read general delimited file into a DataFrame. 

57 

58 Notes 

59 ----- 

60 This warning is issued when dealing with larger files because the dtype 

61 checking happens per chunk read. 

62 

63 Despite the warning, the CSV file is read with mixed types in a single 

64 column which will be an object type. See the examples below to better 

65 understand this issue. 

66 

67 Examples 

68 -------- 

69 This example creates and reads a large CSV file with a column that contains 

70 `int` and `str`. 

71 

72 >>> df = pd.DataFrame({'a': (['1'] * 100000 + ['X'] * 100000 + 

73 ... ['1'] * 100000), 

74 ... 'b': ['b'] * 300000}) 

75 >>> df.to_csv('test.csv', index=False) 

76 >>> df2 = pd.read_csv('test.csv') 

77 ... # DtypeWarning: Columns (0) have mixed types 

78 

79 Important to notice that ``df2`` will contain both `str` and `int` for the 

80 same input, '1'. 

81 

82 >>> df2.iloc[262140, 0] 

83 '1' 

84 >>> type(df2.iloc[262140, 0]) 

85 <class 'str'> 

86 >>> df2.iloc[262150, 0] 

87 1 

88 >>> type(df2.iloc[262150, 0]) 

89 <class 'int'> 

90 

91 One way to solve this issue is using the `dtype` parameter in the 

92 `read_csv` and `read_table` functions to explicit the conversion: 

93 

94 >>> df2 = pd.read_csv('test.csv', sep=',', dtype={'a': str}) 

95 

96 No warning was issued. 

97 

98 >>> import os 

99 >>> os.remove('test.csv') 

100 """ 

101 

102 

103class EmptyDataError(ValueError): 

104 """ 

105 Exception that is thrown in `pd.read_csv` (by both the C and 

106 Python engines) when empty data or header is encountered. 

107 """ 

108 

109 

110class ParserWarning(Warning): 

111 """ 

112 Warning raised when reading a file that doesn't use the default 'c' parser. 

113 

114 Raised by `pd.read_csv` and `pd.read_table` when it is necessary to change 

115 parsers, generally from the default 'c' parser to 'python'. 

116 

117 It happens due to a lack of support or functionality for parsing a 

118 particular attribute of a CSV file with the requested engine. 

119 

120 Currently, 'c' unsupported options include the following parameters: 

121 

122 1. `sep` other than a single character (e.g. regex separators) 

123 2. `skipfooter` higher than 0 

124 3. `sep=None` with `delim_whitespace=False` 

125 

126 The warning can be avoided by adding `engine='python'` as a parameter in 

127 `pd.read_csv` and `pd.read_table` methods. 

128 

129 See Also 

130 -------- 

131 pd.read_csv : Read CSV (comma-separated) file into DataFrame. 

132 pd.read_table : Read general delimited file into DataFrame. 

133 

134 Examples 

135 -------- 

136 Using a `sep` in `pd.read_csv` other than a single character: 

137 

138 >>> import io 

139 >>> csv = '''a;b;c 

140 ... 1;1,8 

141 ... 1;2,1''' 

142 >>> df = pd.read_csv(io.StringIO(csv), sep='[;,]') # doctest: +SKIP 

143 ... # ParserWarning: Falling back to the 'python' engine... 

144 

145 Adding `engine='python'` to `pd.read_csv` removes the Warning: 

146 

147 >>> df = pd.read_csv(io.StringIO(csv), sep='[;,]', engine='python') 

148 """ 

149 

150 

151class MergeError(ValueError): 

152 """ 

153 Error raised when problems arise during merging due to problems 

154 with input data. Subclass of `ValueError`. 

155 """ 

156 

157 

158class AccessorRegistrationWarning(Warning): 

159 """ 

160 Warning for attribute conflicts in accessor registration. 

161 """ 

162 

163 

164class AbstractMethodError(NotImplementedError): 

165 """ 

166 Raise this error instead of NotImplementedError for abstract methods 

167 while keeping compatibility with Python 2 and Python 3. 

168 """ 

169 

170 def __init__(self, class_instance, methodtype="method"): 

171 types = {"method", "classmethod", "staticmethod", "property"} 

172 if methodtype not in types: 

173 raise ValueError( 

174 f"methodtype must be one of {methodtype}, got {types} instead." 

175 ) 

176 self.methodtype = methodtype 

177 self.class_instance = class_instance 

178 

179 def __str__(self) -> str: 

180 if self.methodtype == "classmethod": 

181 name = self.class_instance.__name__ 

182 else: 

183 name = type(self.class_instance).__name__ 

184 return f"This {self.methodtype} must be defined in the concrete class {name}"