65
65
from pandas .io .formats .printing import adjoin , pprint_thing
66
66
67
67
if TYPE_CHECKING :
68
- from tables import File , Node # noqa:F401
68
+ from tables import File , Node , Col # noqa:F401
69
69
70
70
71
71
# versioning attribute
@@ -1092,6 +1092,9 @@ def remove(self, key: str, where=None, start=None, stop=None):
1092
1092
except KeyError :
1093
1093
# the key is not a valid store, re-raising KeyError
1094
1094
raise
1095
+ except AssertionError :
1096
+ # surface any assertion errors for e.g. debugging
1097
+ raise
1095
1098
except Exception :
1096
1099
# In tests we get here with ClosedFileError, TypeError, and
1097
1100
# _table_mod.NoSuchNodeError. TODO: Catch only these?
@@ -1519,6 +1522,9 @@ def info(self) -> str:
1519
1522
if s is not None :
1520
1523
keys .append (pprint_thing (s .pathname or k ))
1521
1524
values .append (pprint_thing (s or "invalid_HDFStore node" ))
1525
+ except AssertionError :
1526
+ # surface any assertion errors for e.g. debugging
1527
+ raise
1522
1528
except Exception as detail :
1523
1529
keys .append (k )
1524
1530
dstr = pprint_thing (detail )
@@ -1680,7 +1686,7 @@ def _write_to_group(
1680
1686
self ._handle .remove_node (group , recursive = True )
1681
1687
group = None
1682
1688
1683
- # we don't want to store a table node at all if are object is 0-len
1689
+ # we don't want to store a table node at all if our object is 0-len
1684
1690
# as there are not dtypes
1685
1691
if getattr (value , "empty" , None ) and (format == "table" or append ):
1686
1692
return
@@ -2356,11 +2362,9 @@ def set_atom_string(self, data_converted: np.ndarray):
2356
2362
self .typ = self .get_atom_string (data_converted .shape , itemsize )
2357
2363
self .set_data (data_converted )
2358
2364
2359
- def get_atom_coltype (self , kind = None ) :
2365
+ def get_atom_coltype (self , kind : str ) -> Type [ "Col" ] :
2360
2366
""" return the PyTables column class for this column """
2361
- if kind is None :
2362
- kind = self .kind
2363
- if self .kind .startswith ("uint" ):
2367
+ if kind .startswith ("uint" ):
2364
2368
k4 = kind [4 :]
2365
2369
col_name = f"UInt{ k4 } Col"
2366
2370
else :
@@ -2369,8 +2373,8 @@ def get_atom_coltype(self, kind=None):
2369
2373
2370
2374
return getattr (_tables (), col_name )
2371
2375
2372
- def get_atom_data (self , block , kind = None ) :
2373
- return self .get_atom_coltype (kind = kind )(shape = block . shape [0 ])
2376
+ def get_atom_data (self , shape , kind : str ) -> "Col" :
2377
+ return self .get_atom_coltype (kind = kind )(shape = shape [0 ])
2374
2378
2375
2379
def set_atom_complex (self , block ):
2376
2380
self .kind = block .dtype .name
@@ -2380,7 +2384,7 @@ def set_atom_complex(self, block):
2380
2384
2381
2385
def set_atom_data (self , block ):
2382
2386
self .kind = block .dtype .name
2383
- self .typ = self .get_atom_data (block )
2387
+ self .typ = self .get_atom_data (block . shape , kind = block . dtype . name )
2384
2388
self .set_data (block .values )
2385
2389
2386
2390
def set_atom_categorical (self , block ):
@@ -2389,19 +2393,22 @@ def set_atom_categorical(self, block):
2389
2393
2390
2394
values = block .values
2391
2395
codes = values .codes
2392
- self .kind = "integer"
2393
- self .dtype = codes .dtype .name
2396
+
2394
2397
if values .ndim > 1 :
2395
2398
raise NotImplementedError ("only support 1-d categoricals" )
2396
2399
2400
+ assert codes .dtype .name .startswith ("int" ), codes .dtype .name
2401
+
2397
2402
# write the codes; must be in a block shape
2398
2403
self .ordered = values .ordered
2399
- self .typ = self .get_atom_data (block , kind = codes .dtype .name )
2404
+ self .typ = self .get_atom_data (block . shape , kind = codes .dtype .name )
2400
2405
self .set_data (block .values )
2401
2406
2402
2407
# write the categories
2403
2408
self .meta = "category"
2404
2409
self .metadata = np .array (block .values .categories , copy = False ).ravel ()
2410
+ assert self .kind == "integer" , self .kind
2411
+ assert self .dtype == codes .dtype .name , codes .dtype .name
2405
2412
2406
2413
def get_atom_datetime64 (self , block ):
2407
2414
return _tables ().Int64Col (shape = block .shape [0 ])
@@ -2554,7 +2561,7 @@ def validate_names(self):
2554
2561
def get_atom_string (self , shape , itemsize ):
2555
2562
return _tables ().StringCol (itemsize = itemsize )
2556
2563
2557
- def get_atom_data (self , block , kind = None ) :
2564
+ def get_atom_data (self , shape , kind : str ) -> "Col" :
2558
2565
return self .get_atom_coltype (kind = kind )()
2559
2566
2560
2567
def get_atom_datetime64 (self , block ):
0 commit comments