From e4d254bce3311cc0e52c3e1d53a7b2a7064b5431 Mon Sep 17 00:00:00 2001 From: Derek Sharp Date: Wed, 6 Jul 2022 20:12:42 -0400 Subject: [PATCH 1/3] ENH: Move exceptions to error/__init__.py per GH27656 --- doc/source/reference/testing.rst | 2 ++ pandas/errors/__init__.py | 26 +++++++++++++++++++ pandas/io/pytables.py | 14 ++++------ .../tests/io/pytables/test_file_handling.py | 10 +++---- pandas/tests/test_errors.py | 2 ++ 5 files changed, 40 insertions(+), 14 deletions(-) diff --git a/doc/source/reference/testing.rst b/doc/source/reference/testing.rst index 249c2c56cfe57..039b5f56dfad6 100644 --- a/doc/source/reference/testing.rst +++ b/doc/source/reference/testing.rst @@ -26,6 +26,7 @@ Exceptions and warnings errors.AbstractMethodError errors.AccessorRegistrationWarning + errors.ClosedFileError errors.CSSWarning errors.DataError errors.DtypeWarning @@ -44,6 +45,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 98a9d2b35f09d..36597c7f81bcd 100644 --- a/pandas/errors/__init__.py +++ b/pandas/errors/__init__.py @@ -413,3 +413,29 @@ class CSSWarning(UserWarning): ... .to_excel('styled.xlsx') # doctest: +SKIP ... # CSSWarning: Too many tokens provided to "border" (expected 1-3) """ + + +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! + """ diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index b96fa4a57f188..a1ad2a6ef359d 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -48,7 +48,11 @@ ) 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 ( + ClosedFileError, + PerformanceWarning, + PossibleDataLossError, +) from pandas.util._decorators import cache_readonly from pandas.util._exceptions import find_stack_level @@ -169,14 +173,6 @@ 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 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/test_errors.py b/pandas/tests/test_errors.py index 177ff566e347a..95488973c93eb 100644 --- a/pandas/tests/test_errors.py +++ b/pandas/tests/test_errors.py @@ -30,6 +30,8 @@ "IndexingError", "PyperclipException", "CSSWarning", + "ClosedFileError", + "PossibleDataLossError", ], ) def test_exception_importable(exc): From 89742cf4f612a0682d0778d5526876cb761dd436 Mon Sep 17 00:00:00 2001 From: Derek Sharp Date: Thu, 7 Jul 2022 23:19:43 -0400 Subject: [PATCH 2/3] ENH: Move warnings to error/__init__.py per GH27656 --- doc/source/reference/testing.rst | 2 ++ pandas/errors/__init__.py | 15 +++++++++++++++ pandas/io/pytables.py | 22 +++------------------- pandas/tests/io/pytables/test_store.py | 1 - pandas/tests/test_errors.py | 2 ++ 5 files changed, 22 insertions(+), 20 deletions(-) diff --git a/doc/source/reference/testing.rst b/doc/source/reference/testing.rst index 039b5f56dfad6..e617712aa8f5e 100644 --- a/doc/source/reference/testing.rst +++ b/doc/source/reference/testing.rst @@ -26,12 +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 diff --git a/pandas/errors/__init__.py b/pandas/errors/__init__.py index 36597c7f81bcd..7b85f1bf4f9ff 100644 --- a/pandas/errors/__init__.py +++ b/pandas/errors/__init__.py @@ -439,3 +439,18 @@ class ClosedFileError(Exception): >>> 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 + dffierent frequency than the existing index on an HDFStore. + """ diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index a1ad2a6ef359d..52a2883e70f93 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -49,7 +49,9 @@ from pandas.compat._optional import import_optional_dependency from pandas.compat.pickle_compat import patch_pickle from pandas.errors import ( + AttributeConflictWarning, ClosedFileError, + IncompatibilityWarning, PerformanceWarning, PossibleDataLossError, ) @@ -173,35 +175,17 @@ def _ensure_term(where, scope_level: int): return where if where is None or len(where) else None -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] @@ -3546,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_store.py b/pandas/tests/io/pytables/test_store.py index 8a933f4981ff3..7bc611884dd8f 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 95488973c93eb..f003e1d07bca6 100644 --- a/pandas/tests/test_errors.py +++ b/pandas/tests/test_errors.py @@ -32,6 +32,8 @@ "CSSWarning", "ClosedFileError", "PossibleDataLossError", + "IncompatibilityWarning", + "AttributeConflictWarning", ], ) def test_exception_importable(exc): From 2f014dbf8cf83023509cd2e810d193389bd555f7 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Sun, 10 Jul 2022 16:06:14 -0700 Subject: [PATCH 3/3] Update pandas/errors/__init__.py --- pandas/errors/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/errors/__init__.py b/pandas/errors/__init__.py index a3584d680d864..0e0409ccb0932 100644 --- a/pandas/errors/__init__.py +++ b/pandas/errors/__init__.py @@ -452,7 +452,7 @@ 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 - dffierent frequency than the existing index on an HDFStore. + different frequency than the existing index on an HDFStore. """