test_errors.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import pytest
  2. from pandas.errors import (
  3. AbstractMethodError,
  4. UndefinedVariableError,
  5. )
  6. import pandas as pd
  7. @pytest.mark.parametrize(
  8. "exc",
  9. [
  10. "AttributeConflictWarning",
  11. "CSSWarning",
  12. "CategoricalConversionWarning",
  13. "ClosedFileError",
  14. "DataError",
  15. "DatabaseError",
  16. "DtypeWarning",
  17. "EmptyDataError",
  18. "IncompatibilityWarning",
  19. "IndexingError",
  20. "InvalidColumnName",
  21. "InvalidComparison",
  22. "InvalidVersion",
  23. "LossySetitemError",
  24. "MergeError",
  25. "NoBufferPresent",
  26. "NumExprClobberingError",
  27. "NumbaUtilError",
  28. "OptionError",
  29. "OutOfBoundsDatetime",
  30. "ParserError",
  31. "ParserWarning",
  32. "PerformanceWarning",
  33. "PossibleDataLossError",
  34. "PossiblePrecisionLoss",
  35. "PyperclipException",
  36. "SettingWithCopyError",
  37. "SettingWithCopyWarning",
  38. "SpecificationError",
  39. "UnsortedIndexError",
  40. "UnsupportedFunctionCall",
  41. "ValueLabelTypeMismatch",
  42. ],
  43. )
  44. def test_exception_importable(exc):
  45. from pandas import errors
  46. err = getattr(errors, exc)
  47. assert err is not None
  48. # check that we can raise on them
  49. msg = "^$"
  50. with pytest.raises(err, match=msg):
  51. raise err()
  52. def test_catch_oob():
  53. from pandas import errors
  54. msg = "Cannot cast 1500-01-01 00:00:00 to unit='ns' without overflow"
  55. with pytest.raises(errors.OutOfBoundsDatetime, match=msg):
  56. pd.Timestamp("15000101").as_unit("ns")
  57. @pytest.mark.parametrize(
  58. "is_local",
  59. [
  60. True,
  61. False,
  62. ],
  63. )
  64. def test_catch_undefined_variable_error(is_local):
  65. variable_name = "x"
  66. if is_local:
  67. msg = f"local variable '{variable_name}' is not defined"
  68. else:
  69. msg = f"name '{variable_name}' is not defined"
  70. with pytest.raises(UndefinedVariableError, match=msg):
  71. raise UndefinedVariableError(variable_name, is_local)
  72. class Foo:
  73. @classmethod
  74. def classmethod(cls):
  75. raise AbstractMethodError(cls, methodtype="classmethod")
  76. @property
  77. def property(self):
  78. raise AbstractMethodError(self, methodtype="property")
  79. def method(self):
  80. raise AbstractMethodError(self)
  81. def test_AbstractMethodError_classmethod():
  82. xpr = "This classmethod must be defined in the concrete class Foo"
  83. with pytest.raises(AbstractMethodError, match=xpr):
  84. Foo.classmethod()
  85. xpr = "This property must be defined in the concrete class Foo"
  86. with pytest.raises(AbstractMethodError, match=xpr):
  87. Foo().property
  88. xpr = "This method must be defined in the concrete class Foo"
  89. with pytest.raises(AbstractMethodError, match=xpr):
  90. Foo().method()