Skip to content

Commit 63caef9

Browse files
dataxerikmroeschke
andauthored
Enh move pytable errors and warnings (#47662)
* ENH: Move exceptions to error/__init__.py per GH27656 * ENH: Move warnings to error/__init__.py per GH27656 * Update pandas/errors/__init__.py Co-authored-by: Matthew Roeschke <[email protected]>
1 parent f7f3e6e commit 63caef9

File tree

6 files changed

+66
-34
lines changed

6 files changed

+66
-34
lines changed

doc/source/reference/testing.rst

+4
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ Exceptions and warnings
2626

2727
errors.AbstractMethodError
2828
errors.AccessorRegistrationWarning
29+
errors.AttributeConflictWarning
30+
errors.ClosedFileError
2931
errors.CSSWarning
3032
errors.DataError
3133
errors.DtypeWarning
3234
errors.DuplicateLabelError
3335
errors.EmptyDataError
36+
errors.IncompatibilityWarning
3437
errors.IndexingError
3538
errors.InvalidIndexError
3639
errors.IntCastingNaNError
@@ -44,6 +47,7 @@ Exceptions and warnings
4447
errors.ParserError
4548
errors.ParserWarning
4649
errors.PerformanceWarning
50+
errors.PossibleDataLossError
4751
errors.PyperclipException
4852
errors.PyperclipWindowsException
4953
errors.SettingWithCopyError

pandas/errors/__init__.py

+45
Original file line numberDiff line numberDiff line change
@@ -415,14 +415,58 @@ class CSSWarning(UserWarning):
415415
"""
416416

417417

418+
class PossibleDataLossError(Exception):
419+
"""
420+
Exception is raised when trying to open a HDFStore file when the file is already
421+
opened.
422+
423+
Examples
424+
--------
425+
>>> store = pd.HDFStore('my-store', 'a') # doctest: +SKIP
426+
>>> store.open("w") # doctest: +SKIP
427+
... # PossibleDataLossError: Re-opening the file [my-store] with mode [a]...
428+
"""
429+
430+
431+
class ClosedFileError(Exception):
432+
"""
433+
Exception is raised when trying to perform an operation on a closed HDFStore file.
434+
435+
Examples
436+
--------
437+
>>> store = pd.HDFStore('my-store', 'a') # doctest: +SKIP
438+
>>> store.close() # doctest: +SKIP
439+
>>> store.keys() # doctest: +SKIP
440+
... # ClosedFileError: my-store file is not open!
441+
"""
442+
443+
444+
class IncompatibilityWarning(Warning):
445+
"""
446+
Warning is raised when trying to use where criteria on an incompatible
447+
HDF5 file.
448+
"""
449+
450+
451+
class AttributeConflictWarning(Warning):
452+
"""
453+
Warning is raised when attempting to append an index with a different
454+
name than the existing index on an HDFStore or attempting to append an index with a
455+
different frequency than the existing index on an HDFStore.
456+
"""
457+
458+
418459
__all__ = [
419460
"AbstractMethodError",
420461
"AccessorRegistrationWarning",
462+
"AttributeConflictWarning",
463+
"ClosedFileError",
421464
"CSSWarning",
422465
"DataError",
423466
"DtypeWarning",
424467
"DuplicateLabelError",
425468
"EmptyDataError",
469+
"IncompatibilityWarning",
426470
"IntCastingNaNError",
427471
"InvalidIndexError",
428472
"IndexingError",
@@ -436,6 +480,7 @@ class CSSWarning(UserWarning):
436480
"ParserError",
437481
"ParserWarning",
438482
"PerformanceWarning",
483+
"PossibleDataLossError",
439484
"PyperclipException",
440485
"PyperclipWindowsException",
441486
"SettingWithCopyError",

pandas/io/pytables.py

+8-28
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@
4848
)
4949
from pandas.compat._optional import import_optional_dependency
5050
from pandas.compat.pickle_compat import patch_pickle
51-
from pandas.errors import PerformanceWarning
51+
from pandas.errors import (
52+
AttributeConflictWarning,
53+
ClosedFileError,
54+
IncompatibilityWarning,
55+
PerformanceWarning,
56+
PossibleDataLossError,
57+
)
5258
from pandas.util._decorators import cache_readonly
5359
from pandas.util._exceptions import find_stack_level
5460

@@ -169,43 +175,17 @@ def _ensure_term(where, scope_level: int):
169175
return where if where is None or len(where) else None
170176

171177

172-
class PossibleDataLossError(Exception):
173-
pass
174-
175-
176-
class ClosedFileError(Exception):
177-
pass
178-
179-
180-
class IncompatibilityWarning(Warning):
181-
pass
182-
183-
184178
incompatibility_doc = """
185179
where criteria is being ignored as this version [%s] is too old (or
186180
not-defined), read the file in and write it out to a new file to upgrade (with
187181
the copy_to method)
188182
"""
189183

190-
191-
class AttributeConflictWarning(Warning):
192-
pass
193-
194-
195184
attribute_conflict_doc = """
196185
the [%s] attribute of the existing index is [%s] which conflicts with the new
197186
[%s], resetting the attribute to None
198187
"""
199188

200-
201-
class DuplicateWarning(Warning):
202-
pass
203-
204-
205-
duplicate_doc = """
206-
duplicate entries in table, taking most recently appended
207-
"""
208-
209189
performance_doc = """
210190
your performance may suffer as PyTables will pickle object types that it cannot
211191
map directly to c-types [inferred_type->%s,key->%s] [items->%s]
@@ -3550,7 +3530,7 @@ def get_attrs(self) -> None:
35503530
def validate_version(self, where=None) -> None:
35513531
"""are we trying to operate on an old version?"""
35523532
if where is not None:
3553-
if self.version[0] <= 0 and self.version[1] <= 10 and self.version[2] < 1:
3533+
if self.is_old_version:
35543534
ws = incompatibility_doc % ".".join([str(x) for x in self.version])
35553535
warnings.warn(ws, IncompatibilityWarning)
35563536

pandas/tests/io/pytables/test_file_handling.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import pytest
55

66
from pandas.compat import is_platform_little_endian
7+
from pandas.errors import (
8+
ClosedFileError,
9+
PossibleDataLossError,
10+
)
711

812
from pandas import (
913
DataFrame,
@@ -20,11 +24,7 @@
2024
)
2125

2226
from pandas.io import pytables as pytables
23-
from pandas.io.pytables import (
24-
ClosedFileError,
25-
PossibleDataLossError,
26-
Term,
27-
)
27+
from pandas.io.pytables import Term
2828

2929
pytestmark = pytest.mark.single_cpu
3030

pandas/tests/io/pytables/test_store.py

-1
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,6 @@ def test_store_series_name(setup_path):
589589
tm.assert_series_equal(recons, series)
590590

591591

592-
@pytest.mark.filterwarnings("ignore:\\nduplicate:pandas.io.pytables.DuplicateWarning")
593592
def test_overwrite_node(setup_path):
594593

595594
with ensure_clean_store(setup_path) as store:

pandas/tests/test_errors.py

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
"IndexingError",
3131
"PyperclipException",
3232
"CSSWarning",
33+
"ClosedFileError",
34+
"PossibleDataLossError",
35+
"IncompatibilityWarning",
36+
"AttributeConflictWarning",
3337
],
3438
)
3539
def test_exception_importable(exc):

0 commit comments

Comments
 (0)