66
66
class TestHDFStore :
67
67
def test_format_kwarg_in_constructor (self , setup_path ):
68
68
# GH 13291
69
+
70
+ msg = "format is not a defined argument for HDFStore"
71
+
69
72
with ensure_clean_path (setup_path ) as path :
70
- with pytest .raises (ValueError ):
73
+ with pytest .raises (ValueError , match = msg ):
71
74
HDFStore (path , format = "table" )
72
75
73
76
def test_context (self , setup_path ):
@@ -203,21 +206,27 @@ def test_api(self, setup_path):
203
206
# Invalid.
204
207
df = tm .makeDataFrame ()
205
208
206
- with pytest .raises (ValueError ):
209
+ msg = "Can only append to Tables"
210
+
211
+ with pytest .raises (ValueError , match = msg ):
207
212
df .to_hdf (path , "df" , append = True , format = "f" )
208
213
209
- with pytest .raises (ValueError ):
214
+ with pytest .raises (ValueError , match = msg ):
210
215
df .to_hdf (path , "df" , append = True , format = "fixed" )
211
216
212
- with pytest .raises (TypeError ):
217
+ msg = r"invalid HDFStore format specified \[foo\]"
218
+
219
+ with pytest .raises (TypeError , match = msg ):
213
220
df .to_hdf (path , "df" , append = True , format = "foo" )
214
221
215
- with pytest .raises (TypeError ):
216
- df .to_hdf (path , "df" , append = False , format = "bar " )
222
+ with pytest .raises (TypeError , match = msg ):
223
+ df .to_hdf (path , "df" , append = False , format = "foo " )
217
224
218
225
# File path doesn't exist
219
226
path = ""
220
- with pytest .raises (FileNotFoundError ):
227
+ msg = f"File { path } does not exist"
228
+
229
+ with pytest .raises (FileNotFoundError , match = msg ):
221
230
read_hdf (path , "df" )
222
231
223
232
def test_api_default_format (self , setup_path ):
@@ -230,7 +239,10 @@ def test_api_default_format(self, setup_path):
230
239
_maybe_remove (store , "df" )
231
240
store .put ("df" , df )
232
241
assert not store .get_storer ("df" ).is_table
233
- with pytest .raises (ValueError ):
242
+
243
+ msg = "Can only append to Tables"
244
+
245
+ with pytest .raises (ValueError , match = msg ):
234
246
store .append ("df2" , df )
235
247
236
248
pd .set_option ("io.hdf.default_format" , "table" )
@@ -251,7 +263,7 @@ def test_api_default_format(self, setup_path):
251
263
df .to_hdf (path , "df" )
252
264
with HDFStore (path ) as store :
253
265
assert not store .get_storer ("df" ).is_table
254
- with pytest .raises (ValueError ):
266
+ with pytest .raises (ValueError , match = msg ):
255
267
df .to_hdf (path , "df2" , append = True )
256
268
257
269
pd .set_option ("io.hdf.default_format" , "table" )
@@ -384,7 +396,10 @@ def test_versioning(self, setup_path):
384
396
# this is an error because its table_type is appendable, but no
385
397
# version info
386
398
store .get_node ("df2" )._v_attrs .pandas_version = None
387
- with pytest .raises (Exception ):
399
+
400
+ msg = "'NoneType' object has no attribute 'startswith'"
401
+
402
+ with pytest .raises (Exception , match = msg ):
388
403
store .select ("df2" )
389
404
390
405
def test_mode (self , setup_path ):
@@ -428,7 +443,11 @@ def check(mode):
428
443
429
444
# conv read
430
445
if mode in ["w" ]:
431
- with pytest .raises (ValueError ):
446
+ msg = (
447
+ "mode w is not allowed while performing a read. "
448
+ r"Allowed modes are r, r\+ and a."
449
+ )
450
+ with pytest .raises (ValueError , match = msg ):
432
451
read_hdf (path , "df" , mode = mode )
433
452
else :
434
453
result = read_hdf (path , "df" , mode = mode )
0 commit comments