@@ -527,13 +527,15 @@ def copy(self, *, shallow=False):
527
527
self ._add_column_and_format (table , label , column )
528
528
return table
529
529
530
- def select (self , column_label_or_labels ):
530
+ def select (self , * column_label_or_labels ):
531
531
"""Return a Table with selected column or columns by label or index.
532
532
533
533
Args:
534
- ``column_label_or_labels`` (string or list of strings): The header
535
- names or indices of the columns to be selected. ``column_label_or_labels`` must
536
- be an existing header name, or a valid column index.
534
+ ``column_label_or_labels`` (string, list of strings, or several
535
+ separate argument strings): The header names or indices of the
536
+ columns to be selected. ``column_label_or_labels`` must
537
+ be an existing header name, or a valid column index, or a list
538
+ thereof.
537
539
538
540
Returns:
539
541
An instance of ``Table`` containing only selected columns.
@@ -557,6 +559,11 @@ def select(self, column_label_or_labels):
557
559
6
558
560
5
559
561
5
562
+ >>> t.select('burgers', 'calories')
563
+ burgers | calories
564
+ cheeseburger | 743
565
+ hamburger | 651
566
+ veggie burger | 582
560
567
>>> t.select(1)
561
568
prices
562
569
6
@@ -567,8 +574,13 @@ def select(self, column_label_or_labels):
567
574
743 | cheeseburger
568
575
651 | hamburger
569
576
582 | veggie burger
577
+ >>> t.select(2, 0)
578
+ calories | burgers
579
+ 743 | cheeseburger
580
+ 651 | hamburger
581
+ 582 | veggie burger
570
582
"""
571
- labels = self ._as_labels (column_label_or_labels )
583
+ labels = self ._varargs_as_labels (column_label_or_labels )
572
584
table = Table ()
573
585
for label in labels :
574
586
self ._add_column_and_format (table , label , np .copy (self [label ]))
@@ -583,7 +595,7 @@ def take(self):
583
595
def exclude (self ):
584
596
raise NotImplementedError ()
585
597
586
- def drop (self , column_label_or_labels ):
598
+ def drop (self , * column_label_or_labels ):
587
599
"""Return a Table with only columns other than selected label or labels.
588
600
589
601
Args:
@@ -613,18 +625,28 @@ def drop(self, column_label_or_labels):
613
625
6
614
626
5
615
627
5
628
+ >>> t.drop('burgers', 'calories')
629
+ prices
630
+ 6
631
+ 5
632
+ 5
616
633
>>> t.drop([0, 2])
617
634
prices
618
635
6
619
636
5
620
637
5
638
+ >>> t.drop(0, 2)
639
+ prices
640
+ 6
641
+ 5
642
+ 5
621
643
>>> t.drop(1)
622
644
burgers | calories
623
645
cheeseburger | 743
624
646
hamburger | 651
625
647
veggie burger | 582
626
648
"""
627
- exclude = _as_labels (column_label_or_labels )
649
+ exclude = _varargs_labels_as_list (column_label_or_labels )
628
650
return self .select ([c for (i , c ) in enumerate (self .labels ) if i not in exclude and c not in exclude ])
629
651
630
652
def where (self , column_or_label , value_or_predicate = None , other = None ):
@@ -1068,6 +1090,11 @@ def _as_labels(self, label_or_labels):
1068
1090
"""Convert single label to list and convert indices to labels."""
1069
1091
return [self ._as_label (s ) for s in _as_labels (label_or_labels )]
1070
1092
1093
+ def _varargs_as_labels (self , label_list ):
1094
+ """Converts a list of labels or singleton list of list of labels into
1095
+ a list of labels. Useful when labels are passed as varargs."""
1096
+ return self ._as_labels (_varargs_labels_as_list (label_list ))
1097
+
1071
1098
def _unused_label (self , label ):
1072
1099
"""Generate an unused label."""
1073
1100
original = label
@@ -1327,7 +1354,7 @@ def with_column(self, label, values):
1327
1354
new_table .append_column (label , values )
1328
1355
return new_table
1329
1356
1330
- def with_columns (self , labels_and_values ):
1357
+ def with_columns (self , * labels_and_values ):
1331
1358
"""Return a table with additional or replaced columns.
1332
1359
1333
1360
Args:
@@ -1341,18 +1368,52 @@ def with_columns(self, labels_and_values):
1341
1368
letter | count
1342
1369
c | 2
1343
1370
d | 4
1371
+ >>> Table().with_columns(
1372
+ ... 'letter', ['c', 'd'],
1373
+ ... 'count', [2, 4],
1374
+ ... )
1375
+ letter | count
1376
+ c | 2
1377
+ d | 4
1344
1378
>>> Table().with_columns([
1345
1379
... ['letter', ['c', 'd']],
1346
1380
... ['count', [2, 4]],
1347
1381
... ])
1348
1382
letter | count
1349
1383
c | 2
1350
1384
d | 4
1385
+ >>> Table().with_columns(
1386
+ ... ['letter', ['c', 'd']],
1387
+ ... ['count', [2, 4]],
1388
+ ... )
1389
+ letter | count
1390
+ c | 2
1391
+ d | 4
1392
+ >>> Table().with_columns([
1393
+ ... ['letter', ['c', 'd']],
1394
+ ... ])
1395
+ letter
1396
+ c
1397
+ d
1398
+ >>> Table().with_columns(
1399
+ ... 'letter', ['c', 'd'],
1400
+ ... )
1401
+ letter
1402
+ c
1403
+ d
1404
+ >>> Table().with_columns(
1405
+ ... ['letter', ['c', 'd']],
1406
+ ... )
1407
+ letter
1408
+ c
1409
+ d
1351
1410
>>> Table().with_columns({'letter': ['c', 'd']})
1352
1411
letter
1353
1412
c
1354
1413
d
1355
1414
"""
1415
+ if len (labels_and_values ) == 1 :
1416
+ labels_and_values = labels_and_values [0 ]
1356
1417
if isinstance (labels_and_values , collections .abc .Mapping ):
1357
1418
labels_and_values = list (labels_and_values .items ())
1358
1419
if not isinstance (labels_and_values , collections .abc .Sequence ):
@@ -2176,6 +2237,20 @@ def _as_labels(column_label_or_labels):
2176
2237
else :
2177
2238
return column_label_or_labels
2178
2239
2240
+ def _varargs_labels_as_list (label_list ):
2241
+ """Return a list of labels for a list of labels or singleton list of list
2242
+ of labels."""
2243
+ if len (label_list ) == 0 :
2244
+ return []
2245
+ elif not _is_non_string_iterable (label_list [0 ]):
2246
+ # Assume everything is a label. If not, it'll be caught later.
2247
+ return label_list
2248
+ elif len (label_list ) == 1 :
2249
+ return label_list [0 ]
2250
+ else :
2251
+ raise ValueError ("Labels {} contain more than list." .format (label_list ),
2252
+ "Pass just one list of labels." )
2253
+
2179
2254
def _assert_same (values ):
2180
2255
"""Assert that all values are identical and return the unique value."""
2181
2256
assert len (values ) > 0
0 commit comments