test_quoting.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. """
  2. Tests that quoting specifications are properly handled
  3. during parsing for all of the parsers defined in parsers.py
  4. """
  5. import csv
  6. from io import StringIO
  7. import pytest
  8. from pandas.compat import (
  9. PY311,
  10. PY314,
  11. )
  12. from pandas.errors import ParserError
  13. from pandas import DataFrame
  14. import pandas._testing as tm
  15. pytestmark = pytest.mark.filterwarnings(
  16. "ignore:Passing a BlockManager to DataFrame:DeprecationWarning"
  17. )
  18. xfail_pyarrow = pytest.mark.usefixtures("pyarrow_xfail")
  19. skip_pyarrow = pytest.mark.usefixtures("pyarrow_skip")
  20. if PY314:
  21. # TODO: write a regex that works with all new possitibilities here
  22. MSG1 = ""
  23. MSG2 = r"[\s\S]*"
  24. else:
  25. MSG1 = "a(n)? 1-character string"
  26. MSG2 = "string( or None)?"
  27. @pytest.mark.parametrize(
  28. "kwargs,msg",
  29. [
  30. ({"quotechar": "foo"}, f'"quotechar" must be {MSG1}'),
  31. (
  32. {"quotechar": None, "quoting": csv.QUOTE_MINIMAL},
  33. "quotechar must be set if quoting enabled",
  34. ),
  35. ({"quotechar": 2}, f'"quotechar" must be {MSG2}, not int'),
  36. ],
  37. )
  38. @skip_pyarrow # ParserError: CSV parse error: Empty CSV file or block
  39. def test_bad_quote_char(all_parsers, kwargs, msg):
  40. data = "1,2,3"
  41. parser = all_parsers
  42. with pytest.raises(TypeError, match=msg):
  43. parser.read_csv(StringIO(data), **kwargs)
  44. @pytest.mark.parametrize(
  45. "quoting,msg",
  46. [
  47. ("foo", '"quoting" must be an integer|Argument'),
  48. (10, 'bad "quoting" value'), # quoting must be in the range [0, 3]
  49. ],
  50. )
  51. @xfail_pyarrow # ValueError: The 'quoting' option is not supported
  52. def test_bad_quoting(all_parsers, quoting, msg):
  53. data = "1,2,3"
  54. parser = all_parsers
  55. with pytest.raises(TypeError, match=msg):
  56. parser.read_csv(StringIO(data), quoting=quoting)
  57. def test_quote_char_basic(all_parsers):
  58. parser = all_parsers
  59. data = 'a,b,c\n1,2,"cat"'
  60. expected = DataFrame([[1, 2, "cat"]], columns=["a", "b", "c"])
  61. result = parser.read_csv(StringIO(data), quotechar='"')
  62. tm.assert_frame_equal(result, expected)
  63. @pytest.mark.parametrize("quote_char", ["~", "*", "%", "$", "@", "P"])
  64. def test_quote_char_various(all_parsers, quote_char):
  65. parser = all_parsers
  66. expected = DataFrame([[1, 2, "cat"]], columns=["a", "b", "c"])
  67. data = 'a,b,c\n1,2,"cat"'
  68. new_data = data.replace('"', quote_char)
  69. result = parser.read_csv(StringIO(new_data), quotechar=quote_char)
  70. tm.assert_frame_equal(result, expected)
  71. @xfail_pyarrow # ValueError: The 'quoting' option is not supported
  72. @pytest.mark.parametrize("quoting", [csv.QUOTE_MINIMAL, csv.QUOTE_NONE])
  73. @pytest.mark.parametrize("quote_char", ["", None])
  74. def test_null_quote_char(all_parsers, quoting, quote_char):
  75. kwargs = {"quotechar": quote_char, "quoting": quoting}
  76. data = "a,b,c\n1,2,3"
  77. parser = all_parsers
  78. if quoting != csv.QUOTE_NONE:
  79. # Sanity checking.
  80. if not PY314:
  81. msg = "1-character string"
  82. else:
  83. msg = "unicode character or None"
  84. msg = (
  85. f'"quotechar" must be a {msg}'
  86. if PY311 and all_parsers.engine == "python" and quote_char == ""
  87. else "quotechar must be set if quoting enabled"
  88. )
  89. with pytest.raises(TypeError, match=msg):
  90. parser.read_csv(StringIO(data), **kwargs)
  91. elif not (PY311 and all_parsers.engine == "python"):
  92. # Python 3.11+ doesn't support null/blank quote chars in their csv parsers
  93. expected = DataFrame([[1, 2, 3]], columns=["a", "b", "c"])
  94. result = parser.read_csv(StringIO(data), **kwargs)
  95. tm.assert_frame_equal(result, expected)
  96. @pytest.mark.parametrize(
  97. "kwargs,exp_data",
  98. [
  99. ({}, [[1, 2, "foo"]]), # Test default.
  100. # QUOTE_MINIMAL only applies to CSV writing, so no effect on reading.
  101. ({"quotechar": '"', "quoting": csv.QUOTE_MINIMAL}, [[1, 2, "foo"]]),
  102. # QUOTE_MINIMAL only applies to CSV writing, so no effect on reading.
  103. ({"quotechar": '"', "quoting": csv.QUOTE_ALL}, [[1, 2, "foo"]]),
  104. # QUOTE_NONE tells the reader to do no special handling
  105. # of quote characters and leave them alone.
  106. ({"quotechar": '"', "quoting": csv.QUOTE_NONE}, [[1, 2, '"foo"']]),
  107. # QUOTE_NONNUMERIC tells the reader to cast
  108. # all non-quoted fields to float
  109. ({"quotechar": '"', "quoting": csv.QUOTE_NONNUMERIC}, [[1.0, 2.0, "foo"]]),
  110. ],
  111. )
  112. @xfail_pyarrow # ValueError: The 'quoting' option is not supported
  113. def test_quoting_various(all_parsers, kwargs, exp_data):
  114. data = '1,2,"foo"'
  115. parser = all_parsers
  116. columns = ["a", "b", "c"]
  117. result = parser.read_csv(StringIO(data), names=columns, **kwargs)
  118. expected = DataFrame(exp_data, columns=columns)
  119. tm.assert_frame_equal(result, expected)
  120. @pytest.mark.parametrize(
  121. "doublequote,exp_data", [(True, [[3, '4 " 5']]), (False, [[3, '4 " 5"']])]
  122. )
  123. def test_double_quote(all_parsers, doublequote, exp_data, request):
  124. parser = all_parsers
  125. data = 'a,b\n3,"4 "" 5"'
  126. if parser.engine == "pyarrow" and not doublequote:
  127. mark = pytest.mark.xfail(reason="Mismatched result")
  128. request.applymarker(mark)
  129. result = parser.read_csv(StringIO(data), quotechar='"', doublequote=doublequote)
  130. expected = DataFrame(exp_data, columns=["a", "b"])
  131. tm.assert_frame_equal(result, expected)
  132. @pytest.mark.parametrize("quotechar", ['"', "\u0001"])
  133. def test_quotechar_unicode(all_parsers, quotechar):
  134. # see gh-14477
  135. data = "a\n1"
  136. parser = all_parsers
  137. expected = DataFrame({"a": [1]})
  138. result = parser.read_csv(StringIO(data), quotechar=quotechar)
  139. tm.assert_frame_equal(result, expected)
  140. @pytest.mark.parametrize("balanced", [True, False])
  141. def test_unbalanced_quoting(all_parsers, balanced, request):
  142. # see gh-22789.
  143. parser = all_parsers
  144. data = 'a,b,c\n1,2,"3'
  145. if parser.engine == "pyarrow" and not balanced:
  146. mark = pytest.mark.xfail(reason="Mismatched result")
  147. request.applymarker(mark)
  148. if balanced:
  149. # Re-balance the quoting and read in without errors.
  150. expected = DataFrame([[1, 2, 3]], columns=["a", "b", "c"])
  151. result = parser.read_csv(StringIO(data + '"'))
  152. tm.assert_frame_equal(result, expected)
  153. else:
  154. msg = (
  155. "EOF inside string starting at row 1"
  156. if parser.engine == "c"
  157. else "unexpected end of data"
  158. )
  159. with pytest.raises(ParserError, match=msg):
  160. parser.read_csv(StringIO(data))