9
9
import os
10
10
import re
11
11
import time
12
- from typing import List , Optional , Type , Union
12
+ from typing import TYPE_CHECKING , Any , Dict , List , Optional , Type , Union
13
13
import warnings
14
14
15
15
import numpy as np
55
55
from pandas .io .common import _stringify_path
56
56
from pandas .io .formats .printing import adjoin , pprint_thing
57
57
58
+ if TYPE_CHECKING :
59
+ from tables import File # noqa:F401
60
+
61
+
58
62
# versioning attribute
59
63
_version = "0.15.2"
60
64
@@ -465,6 +469,8 @@ class HDFStore:
465
469
>>> store.close()
466
470
"""
467
471
472
+ _handle : Optional ["File" ]
473
+
468
474
def __init__ (
469
475
self ,
470
476
path ,
@@ -535,7 +541,7 @@ def __getattr__(self, name):
535
541
)
536
542
)
537
543
538
- def __contains__ (self , key ):
544
+ def __contains__ (self , key : str ):
539
545
""" check for existence of this key
540
546
can match the exact pathname or the pathnm w/o the leading '/'
541
547
"""
@@ -560,7 +566,7 @@ def __enter__(self):
560
566
def __exit__ (self , exc_type , exc_value , traceback ):
561
567
self .close ()
562
568
563
- def keys (self ):
569
+ def keys (self ) -> List [ str ] :
564
570
"""
565
571
Return a list of keys corresponding to objects stored in HDFStore.
566
572
@@ -698,13 +704,13 @@ def flush(self, fsync: bool = False):
698
704
except OSError :
699
705
pass
700
706
701
- def get (self , key ):
707
+ def get (self , key : str ):
702
708
"""
703
709
Retrieve pandas object stored in file.
704
710
705
711
Parameters
706
712
----------
707
- key : object
713
+ key : str
708
714
709
715
Returns
710
716
-------
@@ -718,7 +724,7 @@ def get(self, key):
718
724
719
725
def select (
720
726
self ,
721
- key ,
727
+ key : str ,
722
728
where = None ,
723
729
start = None ,
724
730
stop = None ,
@@ -733,7 +739,7 @@ def select(
733
739
734
740
Parameters
735
741
----------
736
- key : object
742
+ key : str
737
743
Object being retrieved from file.
738
744
where : list, default None
739
745
List of Term (or convertible) objects, optional.
@@ -784,13 +790,15 @@ def func(_start, _stop, _where):
784
790
785
791
return it .get_result ()
786
792
787
- def select_as_coordinates (self , key , where = None , start = None , stop = None , ** kwargs ):
793
+ def select_as_coordinates (
794
+ self , key : str , where = None , start = None , stop = None , ** kwargs
795
+ ):
788
796
"""
789
797
return the selection as an Index
790
798
791
799
Parameters
792
800
----------
793
- key : object
801
+ key : str
794
802
where : list of Term (or convertible) objects, optional
795
803
start : integer (defaults to None), row number to start selection
796
804
stop : integer (defaults to None), row number to stop selection
@@ -800,15 +808,16 @@ def select_as_coordinates(self, key, where=None, start=None, stop=None, **kwargs
800
808
where = where , start = start , stop = stop , ** kwargs
801
809
)
802
810
803
- def select_column (self , key , column , ** kwargs ):
811
+ def select_column (self , key : str , column : str , ** kwargs ):
804
812
"""
805
813
return a single column from the table. This is generally only useful to
806
814
select an indexable
807
815
808
816
Parameters
809
817
----------
810
- key : object
811
- column: the column of interest
818
+ key : str
819
+ column: str
820
+ The column of interest.
812
821
813
822
Raises
814
823
------
@@ -966,7 +975,7 @@ def put(self, key, value, format=None, append=False, **kwargs):
966
975
kwargs = self ._validate_format (format , kwargs )
967
976
self ._write_to_group (key , value , append = append , ** kwargs )
968
977
969
- def remove (self , key , where = None , start = None , stop = None ):
978
+ def remove (self , key : str , where = None , start = None , stop = None ):
970
979
"""
971
980
Remove pandas object partially by specifying the where condition
972
981
@@ -1152,16 +1161,17 @@ def append_to_multiple(
1152
1161
1153
1162
self .append (k , val , data_columns = dc , ** kwargs )
1154
1163
1155
- def create_table_index (self , key , ** kwargs ):
1156
- """ Create a pytables index on the table
1164
+ def create_table_index (self , key : str , ** kwargs ):
1165
+ """
1166
+ Create a pytables index on the table.
1167
+
1157
1168
Parameters
1158
1169
----------
1159
- key : object (the node to index)
1170
+ key : str
1160
1171
1161
1172
Raises
1162
1173
------
1163
- raises if the node is not a table
1164
-
1174
+ TypeError: raises if the node is not a table
1165
1175
"""
1166
1176
1167
1177
# version requirements
@@ -1247,17 +1257,19 @@ def walk(self, where="/"):
1247
1257
1248
1258
yield (g ._v_pathname .rstrip ("/" ), groups , leaves )
1249
1259
1250
- def get_node (self , key ):
1260
+ def get_node (self , key : str ):
1251
1261
""" return the node with the key or None if it does not exist """
1252
1262
self ._check_if_open ()
1263
+ if not key .startswith ("/" ):
1264
+ key = "/" + key
1265
+
1266
+ assert self ._handle is not None
1253
1267
try :
1254
- if not key .startswith ("/" ):
1255
- key = "/" + key
1256
1268
return self ._handle .get_node (self .root , key )
1257
- except _table_mod .exceptions .NoSuchNodeError :
1269
+ except _table_mod .exceptions .NoSuchNodeError : # type: ignore
1258
1270
return None
1259
1271
1260
- def get_storer (self , key ):
1272
+ def get_storer (self , key : str ):
1261
1273
""" return the storer object for a key, raise if not in the file """
1262
1274
group = self .get_node (key )
1263
1275
if group is None :
@@ -1481,7 +1493,7 @@ def error(t):
1481
1493
1482
1494
def _write_to_group (
1483
1495
self ,
1484
- key ,
1496
+ key : str ,
1485
1497
value ,
1486
1498
format ,
1487
1499
index = True ,
@@ -1492,6 +1504,10 @@ def _write_to_group(
1492
1504
):
1493
1505
group = self .get_node (key )
1494
1506
1507
+ # we make this assertion for mypy; the get_node call will already
1508
+ # have raised if this is incorrect
1509
+ assert self ._handle is not None
1510
+
1495
1511
# remove the node if we are not appending
1496
1512
if group is not None and not append :
1497
1513
self ._handle .remove_node (group , recursive = True )
@@ -2691,7 +2707,7 @@ def f(values, freq=None, tz=None):
2691
2707
2692
2708
return klass
2693
2709
2694
- def validate_read (self , kwargs ) :
2710
+ def validate_read (self , kwargs : Dict [ str , Any ]) -> Dict [ str , Any ] :
2695
2711
"""
2696
2712
remove table keywords from kwargs and return
2697
2713
raise if any keywords are passed which are not-None
@@ -2733,7 +2749,7 @@ def get_attrs(self):
2733
2749
def write (self , obj , ** kwargs ):
2734
2750
self .set_attrs ()
2735
2751
2736
- def read_array (self , key , start = None , stop = None ):
2752
+ def read_array (self , key : str , start = None , stop = None ):
2737
2753
""" read an array for the specified node (off of group """
2738
2754
import tables
2739
2755
@@ -4008,7 +4024,7 @@ def read_coordinates(self, where=None, start=None, stop=None, **kwargs):
4008
4024
4009
4025
return Index (coords )
4010
4026
4011
- def read_column (self , column , where = None , start = None , stop = None ):
4027
+ def read_column (self , column : str , where = None , start = None , stop = None ):
4012
4028
"""return a single column from the table, generally only indexables
4013
4029
are interesting
4014
4030
"""
@@ -4642,8 +4658,8 @@ def _convert_index(index, encoding=None, errors="strict", format_type=None):
4642
4658
converted ,
4643
4659
"datetime64" ,
4644
4660
_tables ().Int64Col (),
4645
- freq = getattr ( index , " freq" , None ) ,
4646
- tz = getattr ( index , "tz" , None ) ,
4661
+ freq = index . freq ,
4662
+ tz = index . tz ,
4647
4663
index_name = index_name ,
4648
4664
)
4649
4665
elif isinstance (index , TimedeltaIndex ):
@@ -4652,7 +4668,7 @@ def _convert_index(index, encoding=None, errors="strict", format_type=None):
4652
4668
converted ,
4653
4669
"timedelta64" ,
4654
4670
_tables ().Int64Col (),
4655
- freq = getattr ( index , " freq" , None ) ,
4671
+ freq = index . freq ,
4656
4672
index_name = index_name ,
4657
4673
)
4658
4674
elif isinstance (index , (Int64Index , PeriodIndex )):
0 commit comments