本文整理汇总了Python中pandas.errors.ParserError方法的典型用法代码示例。如果您正苦于以下问题:Python errors.ParserError方法的具体用法?Python errors.ParserError怎么用?Python errors.ParserError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.errors
的用法示例。
在下文中一共展示了errors.ParserError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_malformed_chunks
# 需要导入模块: from pandas import errors [as 别名]
# 或者: from pandas.errors import ParserError [as 别名]
def test_malformed_chunks(all_parsers, nrows):
data = """ignore
A,B,C
skip
1,2,3
3,5,10 # comment
1,2,3,4,5
2,3,4
"""
parser = all_parsers
msg = 'Expected 3 fields in line 6, saw 5'
reader = parser.read_csv(StringIO(data), header=1, comment="#",
iterator=True, chunksize=1, skiprows=[2])
with pytest.raises(ParserError, match=msg):
reader.read(nrows)
示例2: test_uneven_lines_with_usecols
# 需要导入模块: from pandas import errors [as 别名]
# 或者: from pandas.errors import ParserError [as 别名]
def test_uneven_lines_with_usecols(all_parsers, usecols):
# see gh-12203
parser = all_parsers
data = r"""a,b,c
0,1,2
3,4,5,6,7
8,9,10"""
if usecols is None:
# Make sure that an error is still raised
# when the "usecols" parameter is not provided.
msg = r"Expected \d+ fields in line \d+, saw \d+"
with pytest.raises(ParserError, match=msg):
parser.read_csv(StringIO(data))
else:
expected = DataFrame({
"a": [0, 3, 8],
"b": [1, 4, 9]
})
result = parser.read_csv(StringIO(data), usecols=usecols)
tm.assert_frame_equal(result, expected)
示例3: test_multi_char_sep_quotes
# 需要导入模块: from pandas import errors [as 别名]
# 或者: from pandas.errors import ParserError [as 别名]
def test_multi_char_sep_quotes(python_parser_only, quoting):
# see gh-13374
kwargs = dict(sep=",,")
parser = python_parser_only
data = 'a,,b\n1,,a\n2,,"2,,b"'
msg = "ignored when a multi-char delimiter is used"
def fail_read():
with pytest.raises(ParserError, match=msg):
parser.read_csv(StringIO(data), quoting=quoting, **kwargs)
if quoting == csv.QUOTE_NONE:
# We expect no match, so there should be an assertion
# error out of the inner context manager.
with pytest.raises(AssertionError):
fail_read()
else:
fail_read()
示例4: test_skipfooter_bad_row
# 需要导入模块: from pandas import errors [as 别名]
# 或者: from pandas.errors import ParserError [as 别名]
def test_skipfooter_bad_row(python_parser_only, data, skipfooter):
# see gh-13879 and gh-15910
msg = "parsing errors in the skipped footer rows"
parser = python_parser_only
def fail_read():
with pytest.raises(ParserError, match=msg):
parser.read_csv(StringIO(data), skipfooter=skipfooter)
if skipfooter:
fail_read()
else:
# We expect no match, so there should be an assertion
# error out of the inner context manager.
with pytest.raises(AssertionError):
fail_read()
示例5: test_error_rename
# 需要导入模块: from pandas import errors [as 别名]
# 或者: from pandas.errors import ParserError [as 别名]
def test_error_rename():
# see gh-12665
from pandas.errors import ParserError
from pandas.io.common import CParserError
try:
raise CParserError()
except ParserError:
pass
try:
raise ParserError()
except CParserError:
pass
with catch_warnings(record=True):
try:
raise ParserError()
except pd.parser.CParserError:
pass
示例6: test_skipfooter_bad_row
# 需要导入模块: from pandas import errors [as 别名]
# 或者: from pandas.errors import ParserError [as 别名]
def test_skipfooter_bad_row(self):
# see gh-13879
# see gh-15910
msg = 'parsing errors in the skipped footer rows'
for data in ('a\n1\n"b"a',
'a,b,c\ncat,foo,bar\ndog,foo,"baz'):
with tm.assert_raises_regex(ParserError, msg):
self.read_csv(StringIO(data), skipfooter=1)
# We expect no match, so there should be an assertion
# error out of the inner context manager.
with pytest.raises(AssertionError):
with tm.assert_raises_regex(ParserError, msg):
self.read_csv(StringIO(data))
示例7: test_bad_stream_exception
# 需要导入模块: from pandas import errors [as 别名]
# 或者: from pandas.errors import ParserError [as 别名]
def test_bad_stream_exception(self):
# Issue 13652:
# This test validates that both python engine
# and C engine will raise UnicodeDecodeError instead of
# c engine raising ParserError and swallowing exception
# that caused read to fail.
codec = codecs.lookup("utf-8")
utf8 = codecs.lookup('utf-8')
if compat.PY3:
msg = "'utf-8' codec can't decode byte"
else:
msg = "'utf8' codec can't decode byte"
# stream must be binary UTF8
with open(self.csv_shiftjs, "rb") as handle, codecs.StreamRecoder(
handle, utf8.encode, utf8.decode, codec.streamreader,
codec.streamwriter) as stream:
with tm.assert_raises_regex(UnicodeDecodeError, msg):
self.read_csv(stream)
示例8: _alert_malformed
# 需要导入模块: from pandas import errors [as 别名]
# 或者: from pandas.errors import ParserError [as 别名]
def _alert_malformed(self, msg, row_num):
"""
Alert a user about a malformed row.
If `self.error_bad_lines` is True, the alert will be `ParserError`.
If `self.warn_bad_lines` is True, the alert will be printed out.
Parameters
----------
msg : The error message to display.
row_num : The row number where the parsing error occurred.
Because this row number is displayed, we 1-index,
even though we 0-index internally.
"""
if self.error_bad_lines:
raise ParserError(msg)
elif self.warn_bad_lines:
base = 'Skipping line {row_num}: '.format(row_num=row_num)
sys.stderr.write(base + msg + '\n')
示例9: _load_df
# 需要导入模块: from pandas import errors [as 别名]
# 或者: from pandas.errors import ParserError [as 别名]
def _load_df(self):
if self._df is None:
try:
self._df = pd.read_csv(self.filename)
except pd.errors.EmptyDataError:
logger.warning(
'Trace %s is empty and needs to be resampled!' %
self.filename)
os.remove(self.filename)
self.corrupted_flag = True
except CParserError:
logger.warning(
'Trace %s has wrong size!' % self.filename)
self.corrupted_flag = True
os.remove(self.filename)
if len(self.flat_names) == 0 and not self.corrupted_flag:
self.flat_names, self.var_shapes = extract_variables_from_df(
self._df)
self.varnames = list(self.var_shapes.keys())
示例10: test_bad_stream_exception
# 需要导入模块: from pandas import errors [as 别名]
# 或者: from pandas.errors import ParserError [as 别名]
def test_bad_stream_exception(self):
# Issue 13652:
# This test validates that both python engine
# and C engine will raise UnicodeDecodeError instead of
# c engine raising ParserError and swallowing exception
# that caused read to fail.
handle = open(self.csv_shiftjs, "rb")
codec = codecs.lookup("utf-8")
utf8 = codecs.lookup('utf-8')
# stream must be binary UTF8
stream = codecs.StreamRecoder(
handle, utf8.encode, utf8.decode, codec.streamreader,
codec.streamwriter)
if compat.PY3:
msg = "'utf-8' codec can't decode byte"
else:
msg = "'utf8' codec can't decode byte"
with tm.assert_raises_regex(UnicodeDecodeError, msg):
self.read_csv(stream)
stream.close()
示例11: test_error_rename
# 需要导入模块: from pandas import errors [as 别名]
# 或者: from pandas.errors import ParserError [as 别名]
def test_error_rename():
# see gh-12665
from pandas.errors import ParserError
from pandas.io.common import CParserError
try:
raise CParserError()
except ParserError:
pass
try:
raise ParserError()
except CParserError:
pass
示例12: test_computer_sales_page
# 需要导入模块: from pandas import errors [as 别名]
# 或者: from pandas.errors import ParserError [as 别名]
def test_computer_sales_page(self, datapath):
data = datapath('io', 'data', 'computer_sales_page.html')
msg = (r"Passed header=\[0,1\] are too many "
r"rows for this multi_index of columns")
with pytest.raises(ParserError, match=msg):
self.read_html(data, header=[0, 1])
data = datapath('io', 'data', 'computer_sales_page.html')
assert self.read_html(data, header=[1, 2])
示例13: test_parser_error_on_empty_header_row
# 需要导入模块: from pandas import errors [as 别名]
# 或者: from pandas.errors import ParserError [as 别名]
def test_parser_error_on_empty_header_row(self):
msg = (r"Passed header=\[0,1\] are too many "
r"rows for this multi_index of columns")
with pytest.raises(ParserError, match=msg):
self.read_html("""
<table>
<thead>
<tr><th></th><th></tr>
<tr><th>A</th><th>B</th></tr>
</thead>
<tbody>
<tr><td>a</td><td>b</td></tr>
</tbody>
</table>
""", header=[0, 1])
示例14: test_buffer_overflow
# 需要导入模块: from pandas import errors [as 别名]
# 或者: from pandas.errors import ParserError [as 别名]
def test_buffer_overflow(c_parser_only, malformed):
# see gh-9205: test certain malformed input files that cause
# buffer overflows in tokenizer.c
msg = "Buffer overflow caught - possible malformed input file."
parser = c_parser_only
with pytest.raises(ParserError, match=msg):
parser.read_csv(StringIO(malformed))
示例15: test_multi_index_unnamed
# 需要导入模块: from pandas import errors [as 别名]
# 或者: from pandas.errors import ParserError [as 别名]
def test_multi_index_unnamed(all_parsers, index_col, columns):
# see gh-23687
#
# When specifying a multi-index header, make sure that
# we don't error just because one of the rows in our header
# has ALL column names containing the string "Unnamed". The
# correct condition to check is whether the row contains
# ALL columns that did not have names (and instead were given
# placeholder ones).
parser = all_parsers
header = [0, 1]
if index_col is None:
data = ",".join(columns or ["", ""]) + "\n0,1\n2,3\n4,5\n"
else:
data = (",".join([""] + (columns or ["", ""])) +
"\n,0,1\n0,2,3\n1,4,5\n")
if columns is None:
msg = (r"Passed header=\[0,1\] are too "
r"many rows for this multi_index of columns")
with pytest.raises(ParserError, match=msg):
parser.read_csv(StringIO(data), header=header,
index_col=index_col)
else:
result = parser.read_csv(StringIO(data), header=header,
index_col=index_col)
template = "Unnamed: {i}_level_0"
exp_columns = []
for i, col in enumerate(columns):
if not col: # Unnamed.
col = template.format(i=i if index_col is None else i + 1)
exp_columns.append(col)
columns = MultiIndex.from_tuples(zip(exp_columns, ["0", "1"]))
expected = DataFrame([[2, 3], [4, 5]], columns=columns)
tm.assert_frame_equal(result, expected)