test_inf.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """
  2. Tests that work on both the Python and C engines but do not have a
  3. specific classification into the other test modules.
  4. """
  5. from io import StringIO
  6. import numpy as np
  7. import pytest
  8. from pandas import (
  9. DataFrame,
  10. option_context,
  11. )
  12. import pandas._testing as tm
  13. xfail_pyarrow = pytest.mark.usefixtures("pyarrow_xfail")
  14. @xfail_pyarrow
  15. @pytest.mark.parametrize("na_filter", [True, False])
  16. def test_inf_parsing(all_parsers, na_filter):
  17. parser = all_parsers
  18. data = """\
  19. ,A
  20. a,inf
  21. b,-inf
  22. c,+Inf
  23. d,-Inf
  24. e,INF
  25. f,-INF
  26. g,+INf
  27. h,-INf
  28. i,inF
  29. j,-inF"""
  30. expected = DataFrame(
  31. {"A": [float("inf"), float("-inf")] * 5},
  32. index=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"],
  33. )
  34. result = parser.read_csv(StringIO(data), index_col=0, na_filter=na_filter)
  35. tm.assert_frame_equal(result, expected)
  36. @xfail_pyarrow
  37. @pytest.mark.parametrize("na_filter", [True, False])
  38. def test_infinity_parsing(all_parsers, na_filter):
  39. parser = all_parsers
  40. data = """\
  41. ,A
  42. a,Infinity
  43. b,-Infinity
  44. c,+Infinity
  45. """
  46. expected = DataFrame(
  47. {"A": [float("infinity"), float("-infinity"), float("+infinity")]},
  48. index=["a", "b", "c"],
  49. )
  50. result = parser.read_csv(StringIO(data), index_col=0, na_filter=na_filter)
  51. tm.assert_frame_equal(result, expected)
  52. def test_read_csv_with_use_inf_as_na(all_parsers):
  53. # https://github.com/pandas-dev/pandas/issues/35493
  54. parser = all_parsers
  55. data = "1.0\nNaN\n3.0"
  56. with option_context("use_inf_as_na", True):
  57. result = parser.read_csv(StringIO(data), header=None)
  58. expected = DataFrame([1.0, np.nan, 3.0])
  59. tm.assert_frame_equal(result, expected)