diff --git a/doc/source/reference/testing.rst b/doc/source/reference/testing.rst index 249c2c56cfe57..e617712aa8f5e 100644 --- a/doc/source/reference/testing.rst +++ b/doc/source/reference/testing.rst @@ -26,11 +26,14 @@ Exceptions and warnings errors.AbstractMethodError errors.AccessorRegistrationWarning + errors.AttributeConflictWarning + errors.ClosedFileError errors.CSSWarning errors.DataError errors.DtypeWarning errors.DuplicateLabelError errors.EmptyDataError + errors.IncompatibilityWarning errors.IndexingError errors.InvalidIndexError errors.IntCastingNaNError @@ -44,6 +47,7 @@ Exceptions and warnings errors.ParserError errors.ParserWarning errors.PerformanceWarning + errors.PossibleDataLossError errors.PyperclipException errors.PyperclipWindowsException errors.SettingWithCopyError diff --git a/pandas/errors/__init__.py b/pandas/errors/__init__.py index 47819ae5fad23..0e0409ccb0932 100644 --- a/pandas/errors/__init__.py +++ b/pandas/errors/__init__.py @@ -415,14 +415,58 @@ class CSSWarning(UserWarning): """ +class PossibleDataLossError(Exception): + """ + Exception is raised when trying to open a HDFStore file when the file is already + opened. + + Examples + -------- + >>> store = pd.HDFStore('my-store', 'a') # doctest: +SKIP + >>> store.open("w") # doctest: +SKIP + ... # PossibleDataLossError: Re-opening the file [my-store] with mode [a]... + """ + + +class ClosedFileError(Exception): + """ + Exception is raised when trying to perform an operation on a closed HDFStore file. + + Examples + -------- + >>> store = pd.HDFStore('my-store', 'a') # doctest: +SKIP + >>> store.close() # doctest: +SKIP + >>> store.keys() # doctest: +SKIP + ... # ClosedFileError: my-store file is not open! + """ + + +class IncompatibilityWarning(Warning): + """ + Warning is raised when trying to use where criteria on an incompatible + HDF5 file. + """ + + +class AttributeConflictWarning(Warning): + """ + Warning is raised when attempting to append an index with a different + name than the existing index on an HDFStore or attempting to append an index with a + different frequency than the existing index on an HDFStore. + """ + + __all__ = [ "AbstractMethodError", "AccessorRegistrationWarning", + "AttributeConflictWarning", + "ClosedFileError", "CSSWarning", "DataError", "DtypeWarning", "DuplicateLabelError", "EmptyDataError", + "IncompatibilityWarning", "IntCastingNaNError", "InvalidIndexError", "IndexingError", @@ -436,6 +480,7 @@ class CSSWarning(UserWarning): "ParserError", "ParserWarning", "PerformanceWarning", + "PossibleDataLossError", "PyperclipException", "PyperclipWindowsException", "SettingWithCopyError", diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index b96fa4a57f188..52a2883e70f93 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -48,7 +48,13 @@ ) from pandas.compat._optional import import_optional_dependency from pandas.compat.pickle_compat import patch_pickle -from pandas.errors import PerformanceWarning +from pandas.errors import ( + AttributeConflictWarning, + ClosedFileError, + IncompatibilityWarning, + PerformanceWarning, + PossibleDataLossError, +) from pandas.util._decorators import cache_readonly from pandas.util._exceptions import find_stack_level @@ -169,43 +175,17 @@ def _ensure_term(where, scope_level: int): return where if where is None or len(where) else None -class PossibleDataLossError(Exception): - pass - - -class ClosedFileError(Exception): - pass - - -class IncompatibilityWarning(Warning): - pass - - incompatibility_doc = """ where criteria is being ignored as this version [%s] is too old (or not-defined), read the file in and write it out to a new file to upgrade (with the copy_to method) """ - -class AttributeConflictWarning(Warning): - pass - - attribute_conflict_doc = """ the [%s] attribute of the existing index is [%s] which conflicts with the new [%s], resetting the attribute to None """ - -class DuplicateWarning(Warning): - pass - - -duplicate_doc = """ -duplicate entries in table, taking most recently appended -""" - performance_doc = """ your performance may suffer as PyTables will pickle object types that it cannot map directly to c-types [inferred_type->%s,key->%s] [items->%s] @@ -3550,7 +3530,7 @@ def get_attrs(self) -> None: def validate_version(self, where=None) -> None: """are we trying to operate on an old version?""" if where is not None: - if self.version[0] <= 0 and self.version[1] <= 10 and self.version[2] < 1: + if self.is_old_version: ws = incompatibility_doc % ".".join([str(x) for x in self.version]) warnings.warn(ws, IncompatibilityWarning) diff --git a/pandas/tests/io/pytables/test_file_handling.py b/pandas/tests/io/pytables/test_file_handling.py index 9fde65e3a1a43..13b6b94dda8d4 100644 --- a/pandas/tests/io/pytables/test_file_handling.py +++ b/pandas/tests/io/pytables/test_file_handling.py @@ -4,6 +4,10 @@ import pytest from pandas.compat import is_platform_little_endian +from pandas.errors import ( + ClosedFileError, + PossibleDataLossError, +) from pandas import ( DataFrame, @@ -20,11 +24,7 @@ ) from pandas.io import pytables as pytables -from pandas.io.pytables import ( - ClosedFileError, - PossibleDataLossError, - Term, -) +from pandas.io.pytables import Term pytestmark = pytest.mark.single_cpu diff --git a/pandas/tests/io/pytables/test_store.py b/pandas/tests/io/pytables/test_store.py index 3a6f699cce94e..e8f4e7ee92fc3 100644 --- a/pandas/tests/io/pytables/test_store.py +++ b/pandas/tests/io/pytables/test_store.py @@ -589,7 +589,6 @@ def test_store_series_name(setup_path): tm.assert_series_equal(recons, series) -@pytest.mark.filterwarnings("ignore:\\nduplicate:pandas.io.pytables.DuplicateWarning") def test_overwrite_node(setup_path): with ensure_clean_store(setup_path) as store: diff --git a/pandas/tests/test_errors.py b/pandas/tests/test_errors.py index 177ff566e347a..f003e1d07bca6 100644 --- a/pandas/tests/test_errors.py +++ b/pandas/tests/test_errors.py @@ -30,6 +30,10 @@ "IndexingError", "PyperclipException", "CSSWarning", + "ClosedFileError", + "PossibleDataLossError", + "IncompatibilityWarning", + "AttributeConflictWarning", ], ) def test_exception_importable(exc):