test_float.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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.compat import is_platform_linux
  9. from pandas import DataFrame
  10. import pandas._testing as tm
  11. pytestmark = pytest.mark.usefixtures("pyarrow_skip")
  12. def test_float_parser(all_parsers):
  13. # see gh-9565
  14. parser = all_parsers
  15. data = "45e-1,4.5,45.,inf,-inf"
  16. result = parser.read_csv(StringIO(data), header=None)
  17. expected = DataFrame([[float(s) for s in data.split(",")]])
  18. tm.assert_frame_equal(result, expected)
  19. def test_scientific_no_exponent(all_parsers_all_precisions):
  20. # see gh-12215
  21. df = DataFrame.from_dict({"w": ["2e"], "x": ["3E"], "y": ["42e"], "z": ["632E"]})
  22. data = df.to_csv(index=False)
  23. parser, precision = all_parsers_all_precisions
  24. df_roundtrip = parser.read_csv(StringIO(data), float_precision=precision)
  25. tm.assert_frame_equal(df_roundtrip, df)
  26. @pytest.mark.parametrize("neg_exp", [-617, -100000, -99999999999999999])
  27. def test_very_negative_exponent(all_parsers_all_precisions, neg_exp):
  28. # GH#38753
  29. parser, precision = all_parsers_all_precisions
  30. data = f"data\n10E{neg_exp}"
  31. result = parser.read_csv(StringIO(data), float_precision=precision)
  32. expected = DataFrame({"data": [0.0]})
  33. tm.assert_frame_equal(result, expected)
  34. @pytest.mark.parametrize("exp", [999999999999999999, -999999999999999999])
  35. def test_too_many_exponent_digits(all_parsers_all_precisions, exp, request):
  36. # GH#38753
  37. parser, precision = all_parsers_all_precisions
  38. data = f"data\n10E{exp}"
  39. result = parser.read_csv(StringIO(data), float_precision=precision)
  40. if precision == "round_trip":
  41. if exp == 999999999999999999 and is_platform_linux():
  42. mark = pytest.mark.xfail(reason="GH38794, on Linux gives object result")
  43. request.node.add_marker(mark)
  44. value = np.inf if exp > 0 else 0.0
  45. expected = DataFrame({"data": [value]})
  46. else:
  47. expected = DataFrame({"data": [f"10E{exp}"]})
  48. tm.assert_frame_equal(result, expected)