@@ -724,8 +724,9 @@ def select_as_multiple(self, keys, where=None, selector=None, columns=None,
724
724
725
725
Exceptions
726
726
----------
727
- raise if any of the keys don't refer to tables or if they are not ALL
728
- THE SAME DIMENSIONS
727
+ raises KeyError if keys or selector is not found or keys is empty
728
+ raises TypeError if keys is not a list or tuple
729
+ raises ValueError if the tables are not ALL THE SAME DIMENSIONS
729
730
"""
730
731
731
732
# default to single select
@@ -748,12 +749,13 @@ def select_as_multiple(self, keys, where=None, selector=None, columns=None,
748
749
749
750
# collect the tables
750
751
tbls = [self .get_storer (k ) for k in keys ]
752
+ s = self .get_storer (selector )
751
753
752
754
# validate rows
753
755
nrows = None
754
- for t , k in zip (tbls , keys ):
756
+ for t , k in itertools . chain ([( s , selector )], zip (tbls , keys ) ):
755
757
if t is None :
756
- raise TypeError ("Invalid table [%s]" % k )
758
+ raise KeyError ("Invalid table [%s]" % k )
757
759
if not t .is_table :
758
760
raise TypeError (
759
761
"object [%s] is not a table, and cannot be used in all "
@@ -766,22 +768,17 @@ def select_as_multiple(self, keys, where=None, selector=None, columns=None,
766
768
raise ValueError (
767
769
"all tables must have exactly the same nrows!" )
768
770
769
- # select coordinates from the selector table
770
- try :
771
- c = self .select_as_coordinates (
772
- selector , where , start = start , stop = stop )
773
- nrows = len (c )
774
- except Exception :
775
- raise ValueError ("invalid selector [%s]" % selector )
771
+ # axis is the concentation axes
772
+ axis = list (set ([t .non_index_axes [0 ][0 ] for t in tbls ]))[0 ]
776
773
777
774
def func (_start , _stop ):
778
-
779
- # collect the returns objs
780
- objs = [ t . read ( where = c [ _start : _stop ], columns = columns )
781
- for t in tbls ]
782
-
783
- # axis is the concentation axes
784
- axis = list ( set ([ t . non_index_axes [ 0 ][ 0 ] for t in tbls ]))[ 0 ]
775
+ if where is not None :
776
+ c = s . read_coordinates ( where = where , start = _start , stop = _stop , ** kwargs )
777
+ else :
778
+ c = None
779
+
780
+ objs = [ t . read ( where = c , start = _start , stop = _stop ,
781
+ columns = columns , ** kwargs ) for t in tbls ]
785
782
786
783
# concat and return
787
784
return concat (objs , axis = axis ,
@@ -860,7 +857,7 @@ def remove(self, key, where=None, start=None, stop=None):
860
857
raise KeyError ('No object named %s in the file' % key )
861
858
862
859
# remove the node
863
- if where is None :
860
+ if where is None and start is None and stop is None :
864
861
s .group ._f_remove (recursive = True )
865
862
866
863
# delete from the table
@@ -2139,11 +2136,9 @@ def write(self, **kwargs):
2139
2136
raise NotImplementedError (
2140
2137
"cannot write on an abstract storer: sublcasses should implement" )
2141
2138
2142
- def delete (self , where = None , ** kwargs ):
2143
- """support fully deleting the node in its entirety (only) - where
2144
- specification must be None
2145
- """
2146
- if where is None :
2139
+ def delete (self , where = None , start = None , stop = None , ** kwargs ):
2140
+ """ support fully deleting the node in its entirety (only) - where specification must be None """
2141
+ if where is None and start is None and stop is None :
2147
2142
self ._handle .removeNode (self .group , recursive = True )
2148
2143
return None
2149
2144
@@ -3381,9 +3376,15 @@ def read_coordinates(self, where=None, start=None, stop=None, **kwargs):
3381
3376
# create the selection
3382
3377
self .selection = Selection (
3383
3378
self , where = where , start = start , stop = stop , ** kwargs )
3384
- return Index (self .selection .select_coords ())
3379
+ coords = self .selection .select_coords ()
3380
+ if self .selection .filter is not None :
3381
+ for field , op , filt in self .selection .filter .format ():
3382
+ data = self .read_column (field , start = coords .min (), stop = coords .max ()+ 1 )
3383
+ coords = coords [op (data .iloc [coords - coords .min ()], filt ).values ]
3385
3384
3386
- def read_column (self , column , where = None , ** kwargs ):
3385
+ return Index (coords )
3386
+
3387
+ def read_column (self , column , where = None , start = None , stop = None , ** kwargs ):
3387
3388
"""return a single column from the table, generally only indexables
3388
3389
are interesting
3389
3390
"""
@@ -3411,7 +3412,7 @@ def read_column(self, column, where=None, **kwargs):
3411
3412
# column must be an indexable or a data column
3412
3413
c = getattr (self .table .cols , column )
3413
3414
a .set_info (self .info )
3414
- return Series (a .convert (c [: ], nan_rep = self .nan_rep ,
3415
+ return Series (a .convert (c [start : stop ], nan_rep = self .nan_rep ,
3415
3416
encoding = self .encoding ).take_data ())
3416
3417
3417
3418
raise KeyError ("column [%s] not found in the table" % column )
@@ -3712,12 +3713,19 @@ def write_data_chunk(self, indexes, mask, values):
3712
3713
except Exception as detail :
3713
3714
raise TypeError ("tables cannot write this data -> %s" % detail )
3714
3715
3715
- def delete (self , where = None , ** kwargs ):
3716
+ def delete (self , where = None , start = None , stop = None , ** kwargs ):
3716
3717
3717
3718
# delete all rows (and return the nrows)
3718
3719
if where is None or not len (where ):
3719
- nrows = self .nrows
3720
- self ._handle .removeNode (self .group , recursive = True )
3720
+ if start is None and stop is None :
3721
+ nrows = self .nrows
3722
+ self ._handle .removeNode (self .group , recursive = True )
3723
+ else :
3724
+ # pytables<3.0 would remove a single row with stop=None
3725
+ if stop is None :
3726
+ stop = self .nrows
3727
+ nrows = self .table .removeRows (start = start , stop = stop )
3728
+ self .table .flush ()
3721
3729
return nrows
3722
3730
3723
3731
# infer the data kind
@@ -3726,7 +3734,7 @@ def delete(self, where=None, **kwargs):
3726
3734
3727
3735
# create the selection
3728
3736
table = self .table
3729
- self .selection = Selection (self , where , ** kwargs )
3737
+ self .selection = Selection (self , where , start = start , stop = stop , ** kwargs )
3730
3738
values = self .selection .select_coords ()
3731
3739
3732
3740
# delete the rows in reverse order
@@ -4303,13 +4311,25 @@ def select_coords(self):
4303
4311
"""
4304
4312
generate the selection
4305
4313
"""
4306
- if self .condition is None :
4307
- return np .arange (self .table .nrows )
4314
+ start , stop = self .start , self .stop
4315
+ nrows = self .table .nrows
4316
+ if start is None :
4317
+ start = 0
4318
+ elif start < 0 :
4319
+ start += nrows
4320
+ if self .stop is None :
4321
+ stop = nrows
4322
+ elif stop < 0 :
4323
+ stop += nrows
4308
4324
4309
- return self .table .table .getWhereList (self .condition .format (),
4310
- start = self .start , stop = self .stop ,
4311
- sort = True )
4325
+ if self .condition is not None :
4326
+ return self .table .table .getWhereList (self .condition .format (),
4327
+ start = start , stop = stop ,
4328
+ sort = True )
4329
+ elif self .coordinates is not None :
4330
+ return self .coordinates
4312
4331
4332
+ return np .arange (start , stop )
4313
4333
4314
4334
# utilities ###
4315
4335
0 commit comments