1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
|
from __future__ import absolute_import, division, print_function
import itertools
import operator
from petl.comparison import Comparable, comparable_itemgetter
from petl.compat import next, text_type
from petl.errors import ArgumentError
from petl.transform.basics import cut, cutout, stack
from petl.transform.dedup import distinct
from petl.transform.sorts import sort
from petl.util.base import Table, asindices, data, header, rowgetter, rowgroupby
def natural_key(left, right):
# determine key field or fields
lhdr = header(left)
lflds = list(map(str, lhdr))
rhdr = header(right)
rflds = list(map(str, rhdr))
key = [f for f in lflds if f in rflds]
assert len(key) > 0, 'no fields in common'
if len(key) == 1:
key = key[0] # deal with singletons
return key
def keys_from_args(left, right, key, lkey, rkey):
if key is lkey is rkey is None:
# no keys specified, attempt natural join
lkey = rkey = natural_key(left, right)
elif key is not None and lkey is rkey is None:
# common key specified
lkey = rkey = key
elif key is None and lkey is not None and rkey is not None:
# left and right keys specified
pass
else:
raise ArgumentError(
'bad key arguments: either specify key, or specify both lkey and '
'rkey, or provide no key/lkey/rkey arguments at all (natural join)'
)
return lkey, rkey
def join(left, right, key=None, lkey=None, rkey=None, presorted=False,
buffersize=None, tempdir=None, cache=True, lprefix=None, rprefix=None):
"""
Perform an equi-join on the given tables. E.g.::
>>> import petl as etl
>>> table1 = [['id', 'colour'],
... [1, 'blue'],
... [2, 'red'],
... [3, 'purple']]
>>> table2 = [['id', 'shape'],
... [1, 'circle'],
... [3, 'square'],
... [4, 'ellipse']]
>>> table3 = etl.join(table1, table2, key='id')
>>> table3
+----+----------+----------+
| id | colour | shape |
+====+==========+==========+
| 1 | 'blue' | 'circle' |
+----+----------+----------+
| 3 | 'purple' | 'square' |
+----+----------+----------+
>>> # if no key is given, a natural join is tried
... table4 = etl.join(table1, table2)
>>> table4
+----+----------+----------+
| id | colour | shape |
+====+==========+==========+
| 1 | 'blue' | 'circle' |
+----+----------+----------+
| 3 | 'purple' | 'square' |
+----+----------+----------+
>>> # note behaviour if the key is not unique in either or both tables
... table5 = [['id', 'colour'],
... [1, 'blue'],
... [1, 'red'],
... [2, 'purple']]
>>> table6 = [['id', 'shape'],
... [1, 'circle'],
... [1, 'square'],
... [2, 'ellipse']]
>>> table7 = etl.join(table5, table6, key='id')
>>> table7
+----+----------+-----------+
| id | colour | shape |
+====+==========+===========+
| 1 | 'blue' | 'circle' |
+----+----------+-----------+
| 1 | 'blue' | 'square' |
+----+----------+-----------+
| 1 | 'red' | 'circle' |
+----+----------+-----------+
| 1 | 'red' | 'square' |
+----+----------+-----------+
| 2 | 'purple' | 'ellipse' |
+----+----------+-----------+
>>> # compound keys are supported
... table8 = [['id', 'time', 'height'],
... [1, 1, 12.3],
... [1, 2, 34.5],
... [2, 1, 56.7]]
>>> table9 = [['id', 'time', 'weight'],
... [1, 2, 4.5],
... [2, 1, 6.7],
... [2, 2, 8.9]]
>>> table10 = etl.join(table8, table9, key=['id', 'time'])
>>> table10
+----+------+--------+--------+
| id | time | height | weight |
+====+======+========+========+
| 1 | 2 | 34.5 | 4.5 |
+----+------+--------+--------+
| 2 | 1 | 56.7 | 6.7 |
+----+------+--------+--------+
If `presorted` is True, it is assumed that the data are already sorted by
the given key, and the `buffersize`, `tempdir` and `cache` arguments are
ignored. Otherwise, the data are sorted, see also the discussion of the
`buffersize`, `tempdir` and `cache` arguments under the
:func:`petl.transform.sorts.sort` function.
Left and right tables with different key fields can be handled via the
`lkey` and `rkey` arguments.
.. versionchanged:: 1.7.16
To ensure correct results for tables with uneven rows, tables will be
squared up before joining to ensure correct results.
"""
# TODO don't read data twice (occurs if using natural key)
lkey, rkey = keys_from_args(left, right, key, lkey, rkey)
return JoinView(left, right, lkey=lkey, rkey=rkey,
presorted=presorted, buffersize=buffersize, tempdir=tempdir,
cache=cache, lprefix=lprefix, rprefix=rprefix)
Table.join = join
class JoinView(Table):
def __init__(self, left, right, lkey, rkey,
presorted=False, leftouter=False, rightouter=False,
missing=None, buffersize=None, tempdir=None, cache=True,
lprefix=None, rprefix=None):
self.lkey = lkey
self.rkey = rkey
self.left = stack(left, missing=missing)
self.right = stack(right, missing=missing)
if not presorted:
self.left = sort(self.left, lkey, buffersize=buffersize,
tempdir=tempdir, cache=cache)
self.right = sort(self.right, rkey, buffersize=buffersize,
tempdir=tempdir, cache=cache)
self.leftouter = leftouter
self.rightouter = rightouter
self.missing = missing
self.lprefix = lprefix
self.rprefix = rprefix
def __iter__(self):
return iterjoin(self.left, self.right, self.lkey, self.rkey,
leftouter=self.leftouter, rightouter=self.rightouter,
missing=self.missing, lprefix=self.lprefix,
rprefix=self.rprefix)
def leftjoin(left, right, key=None, lkey=None, rkey=None, missing=None,
presorted=False, buffersize=None, tempdir=None, cache=True,
lprefix=None, rprefix=None):
"""
Perform a left outer join on the given tables. E.g.::
>>> import petl as etl
>>> table1 = [['id', 'colour'],
... [1, 'blue'],
... [2, 'red'],
... [3, 'purple']]
>>> table2 = [['id', 'shape'],
... [1, 'circle'],
... [3, 'square'],
... [4, 'ellipse']]
>>> table3 = etl.leftjoin(table1, table2, key='id')
>>> table3
+----+----------+----------+
| id | colour | shape |
+====+==========+==========+
| 1 | 'blue' | 'circle' |
+----+----------+----------+
| 2 | 'red' | None |
+----+----------+----------+
| 3 | 'purple' | 'square' |
+----+----------+----------+
If `presorted` is True, it is assumed that the data are already sorted by
the given key, and the `buffersize`, `tempdir` and `cache` arguments are
ignored. Otherwise, the data are sorted, see also the discussion of the
`buffersize`, `tempdir` and `cache` arguments under the
:func:`petl.transform.sorts.sort` function.
Left and right tables with different key fields can be handled via the
`lkey` and `rkey` arguments.
.. versionchanged:: 1.7.16
To ensure correct results for tables with uneven rows, tables will be
squared up before joining to ensure correct results.
"""
# TODO don't read data twice (occurs if using natural key)
lkey, rkey = keys_from_args(left, right, key, lkey, rkey)
return JoinView(left, right, lkey=lkey, rkey=rkey,
presorted=presorted, leftouter=True, rightouter=False,
missing=missing, buffersize=buffersize, tempdir=tempdir,
cache=cache, lprefix=lprefix, rprefix=rprefix)
Table.leftjoin = leftjoin
def rightjoin(left, right, key=None, lkey=None, rkey=None, missing=None,
presorted=False, buffersize=None, tempdir=None, cache=True,
lprefix=None, rprefix=None):
"""
Perform a right outer join on the given tables. E.g.::
>>> import petl as etl
>>> table1 = [['id', 'colour'],
... [1, 'blue'],
... [2, 'red'],
... [3, 'purple']]
>>> table2 = [['id', 'shape'],
... [1, 'circle'],
... [3, 'square'],
... [4, 'ellipse']]
>>> table3 = etl.rightjoin(table1, table2, key='id')
>>> table3
+----+----------+-----------+
| id | colour | shape |
+====+==========+===========+
| 1 | 'blue' | 'circle' |
+----+----------+-----------+
| 3 | 'purple' | 'square' |
+----+----------+-----------+
| 4 | None | 'ellipse' |
+----+----------+-----------+
If `presorted` is True, it is assumed that the data are already sorted by
the given key, and the `buffersize`, `tempdir` and `cache` arguments are
ignored. Otherwise, the data are sorted, see also the discussion of the
`buffersize`, `tempdir` and `cache` arguments under the
:func:`petl.transform.sorts.sort` function.
Left and right tables with different key fields can be handled via the
`lkey` and `rkey` arguments.
.. versionchanged:: 1.7.16
To ensure correct results for tables with uneven rows, tables will be
squared up before joining to ensure correct results.
"""
# TODO don't read data twice (occurs if using natural key)
lkey, rkey = keys_from_args(left, right, key, lkey, rkey)
return JoinView(left, right, lkey=lkey, rkey=rkey,
presorted=presorted, leftouter=False, rightouter=True,
missing=missing, buffersize=buffersize,
tempdir=tempdir, cache=cache, lprefix=lprefix,
rprefix=rprefix)
Table.rightjoin = rightjoin
def outerjoin(left, right, key=None, lkey=None, rkey=None, missing=None,
presorted=False, buffersize=None, tempdir=None, cache=True,
lprefix=None, rprefix=None):
"""
Perform a full outer join on the given tables. E.g.::
>>> import petl as etl
>>> table1 = [['id', 'colour'],
... [1, 'blue'],
... [2, 'red'],
... [3, 'purple']]
>>> table2 = [['id', 'shape'],
... [1, 'circle'],
... [3, 'square'],
... [4, 'ellipse']]
>>> table3 = etl.outerjoin(table1, table2, key='id')
>>> table3
+----+----------+-----------+
| id | colour | shape |
+====+==========+===========+
| 1 | 'blue' | 'circle' |
+----+----------+-----------+
| 2 | 'red' | None |
+----+----------+-----------+
| 3 | 'purple' | 'square' |
+----+----------+-----------+
| 4 | None | 'ellipse' |
+----+----------+-----------+
If `presorted` is True, it is assumed that the data are already sorted by
the given key, and the `buffersize`, `tempdir` and `cache` arguments are
ignored. Otherwise, the data are sorted, see also the discussion of the
`buffersize`, `tempdir` and `cache` arguments under the
:func:`petl.transform.sorts.sort` function.
Left and right tables with different key fields can be handled via the
`lkey` and `rkey` arguments.
.. versionchanged:: 1.7.16
To ensure correct results for tables with uneven rows, tables will be
squared up before joining to ensure correct results.
"""
# TODO don't read data twice (occurs if using natural key)
lkey, rkey = keys_from_args(left, right, key, lkey, rkey)
return JoinView(left, right, lkey=lkey, rkey=rkey,
presorted=presorted, leftouter=True, rightouter=True,
missing=missing, buffersize=buffersize, tempdir=tempdir,
cache=cache, lprefix=lprefix, rprefix=rprefix)
Table.outerjoin = outerjoin
def iterjoin(left, right, lkey, rkey, leftouter=False, rightouter=False,
missing=None, lprefix=None, rprefix=None):
lit = iter(left)
rit = iter(right)
lhdr = next(lit)
rhdr = next(rit)
# determine indices of the key fields in left and right tables
lkind = asindices(lhdr, lkey)
rkind = asindices(rhdr, rkey)
# construct functions to extract key values from both tables
lgetk = comparable_itemgetter(*lkind)
rgetk = comparable_itemgetter(*rkind)
# determine indices of non-key fields in the right table
# (in the output, we only include key fields from the left table - we
# don't want to duplicate fields)
rvind = [i for i in range(len(rhdr)) if i not in rkind]
rgetv = rowgetter(*rvind)
# determine the output fields
if lprefix is None:
outhdr = list(lhdr)
else:
outhdr = [(text_type(lprefix) + text_type(f)) for f in lhdr]
if rprefix is None:
outhdr.extend(rgetv(rhdr))
else:
outhdr.extend([(text_type(rprefix) + text_type(f)) for f in rgetv(rhdr)])
yield tuple(outhdr)
# define a function to join two groups of rows
def joinrows(_lrowgrp, _rrowgrp):
if _rrowgrp is None:
for lrow in _lrowgrp:
outrow = list(lrow) # start with the left row
# extend with missing values in place of the right row
outrow.extend([missing] * len(rvind))
yield tuple(outrow)
elif _lrowgrp is None:
for rrow in _rrowgrp:
# start with missing values in place of the left row
outrow = [missing] * len(lhdr)
# set key values
for li, ri in zip(lkind, rkind):
outrow[li] = rrow[ri]
# extend with non-key values from the right row
outrow.extend(rgetv(rrow))
yield tuple(outrow)
else:
_rrowgrp = list(_rrowgrp) # may need to iterate more than once
for lrow in _lrowgrp:
for rrow in _rrowgrp:
# start with the left row
outrow = list(lrow)
# extend with non-key values from the right row
outrow.extend(rgetv(rrow))
yield tuple(outrow)
# construct group iterators for both tables
lgit = itertools.groupby(lit, key=lgetk)
rgit = itertools.groupby(rit, key=rgetk)
lrowgrp = []
rrowgrp = []
# loop until *either* of the iterators is exhausted
# initialise here to handle empty tables
lkval, rkval = Comparable(None), Comparable(None)
try:
# pick off initial row groups
lkval, lrowgrp = next(lgit)
rkval, rrowgrp = next(rgit)
while True:
if lkval < rkval:
if leftouter:
for row in joinrows(lrowgrp, None):
yield tuple(row)
# advance left
lkval, lrowgrp = next(lgit)
elif lkval > rkval:
if rightouter:
for row in joinrows(None, rrowgrp):
yield tuple(row)
# advance right
rkval, rrowgrp = next(rgit)
else:
for row in joinrows(lrowgrp, rrowgrp):
yield tuple(row)
# advance both
lkval, lrowgrp = next(lgit)
rkval, rrowgrp = next(rgit)
except StopIteration:
pass
# make sure any left rows remaining are yielded
if leftouter:
if lkval > rkval:
# yield anything that got left hanging
for row in joinrows(lrowgrp, None):
yield tuple(row)
# yield the rest
for lkval, lrowgrp in lgit:
for row in joinrows(lrowgrp, None):
yield tuple(row)
# make sure any right rows remaining are yielded
if rightouter:
if lkval < rkval:
# yield anything that got left hanging
for row in joinrows(None, rrowgrp):
yield tuple(row)
# yield the rest
for rkval, rrowgrp in rgit:
for row in joinrows(None, rrowgrp):
yield tuple(row)
def crossjoin(*tables, **kwargs):
"""
Form the cartesian product of the given tables. E.g.::
>>> import petl as etl
>>> table1 = [['id', 'colour'],
... [1, 'blue'],
... [2, 'red']]
>>> table2 = [['id', 'shape'],
... [1, 'circle'],
... [3, 'square']]
>>> table3 = etl.crossjoin(table1, table2)
>>> table3
+----+--------+----+----------+
| id | colour | id | shape |
+====+========+====+==========+
| 1 | 'blue' | 1 | 'circle' |
+----+--------+----+----------+
| 1 | 'blue' | 3 | 'square' |
+----+--------+----+----------+
| 2 | 'red' | 1 | 'circle' |
+----+--------+----+----------+
| 2 | 'red' | 3 | 'square' |
+----+--------+----+----------+
If `prefix` is `True` then field names in the output table header will be
prefixed by the index of the input table.
.. versionchanged:: 1.7.16
To ensure correct results for tables with uneven rows, tables will be
squared up before joining to ensure correct results.
"""
return CrossJoinView(*tables, **kwargs)
Table.crossjoin = crossjoin
class CrossJoinView(Table):
def __init__(self, *sources, **kwargs):
self.prefix = kwargs.get('prefix', False)
self.missing = kwargs.get('missing', None)
self.sources = [stack(source, missing=self.missing) for source in sources]
def __iter__(self):
return itercrossjoin(self.sources, self.prefix)
def itercrossjoin(sources, prefix):
# construct fields
outhdr = list()
for i, s in enumerate(sources):
if prefix:
# use one-based numbering
outhdr.extend([text_type(i+1) + '_' + text_type(f) for f in header(s)])
else:
outhdr.extend(header(s))
yield tuple(outhdr)
datasrcs = [data(src) for src in sources]
for prod in itertools.product(*datasrcs):
outrow = list()
for row in prod:
outrow.extend(row)
yield tuple(outrow)
def antijoin(left, right, key=None, lkey=None, rkey=None, presorted=False,
buffersize=None, tempdir=None, cache=True):
"""
Return rows from the `left` table where the key value does not occur in
the `right` table. E.g.::
>>> import petl as etl
>>> table1 = [['id', 'colour'],
... [0, 'black'],
... [1, 'blue'],
... [2, 'red'],
... [4, 'yellow'],
... [5, 'white']]
>>> table2 = [['id', 'shape'],
... [1, 'circle'],
... [3, 'square']]
>>> table3 = etl.antijoin(table1, table2, key='id')
>>> table3
+----+----------+
| id | colour |
+====+==========+
| 0 | 'black' |
+----+----------+
| 2 | 'red' |
+----+----------+
| 4 | 'yellow' |
+----+----------+
| 5 | 'white' |
+----+----------+
If `presorted` is True, it is assumed that the data are already sorted by
the given key, and the `buffersize`, `tempdir` and `cache` arguments are
ignored. Otherwise, the data are sorted, see also the discussion of the
`buffersize`, `tempdir` and `cache` arguments under the
:func:`petl.transform.sorts.sort` function.
Left and right tables with different key fields can be handled via the
`lkey` and `rkey` arguments.
"""
lkey, rkey = keys_from_args(left, right, key, lkey, rkey)
return AntiJoinView(left=left, right=right, lkey=lkey, rkey=rkey,
presorted=presorted, buffersize=buffersize,
tempdir=tempdir, cache=cache)
Table.antijoin = antijoin
class AntiJoinView(Table):
def __init__(self, left, right, lkey, rkey, presorted=False,
buffersize=None, tempdir=None, cache=True):
if presorted:
self.left = left
self.right = right
else:
self.left = sort(left, lkey, buffersize=buffersize,
tempdir=tempdir, cache=cache)
self.right = sort(right, rkey, buffersize=buffersize,
tempdir=tempdir, cache=cache)
self.lkey = lkey
self.rkey = rkey
def __iter__(self):
return iterantijoin(self.left, self.right, self.lkey, self.rkey)
def iterantijoin(left, right, lkey, rkey):
lit = iter(left)
rit = iter(right)
lhdr = next(lit)
rhdr = next(rit)
yield tuple(lhdr)
# determine indices of the key fields in left and right tables
lkind = asindices(lhdr, lkey)
rkind = asindices(rhdr, rkey)
# construct functions to extract key values from both tables
lgetk = comparable_itemgetter(*lkind)
rgetk = comparable_itemgetter(*rkind)
# construct group iterators for both tables
lgit = itertools.groupby(lit, key=lgetk)
rgit = itertools.groupby(rit, key=rgetk)
lrowgrp = []
# loop until *either* of the iterators is exhausted
lkval, rkval = Comparable(None), Comparable(None)
try:
# pick off initial row groups
lkval, lrowgrp = next(lgit)
rkval, _ = next(rgit)
while True:
if lkval < rkval:
for row in lrowgrp:
yield tuple(row)
# advance left
lkval, lrowgrp = next(lgit)
elif lkval > rkval:
# advance right
rkval, _ = next(rgit)
else:
# advance both
lkval, lrowgrp = next(lgit)
rkval, _ = next(rgit)
except StopIteration:
pass
# any left over?
if lkval > rkval:
# yield anything that got left hanging
for row in lrowgrp:
yield tuple(row)
# and the rest...
for lkval, lrowgrp in lgit:
for row in lrowgrp:
yield tuple(row)
def lookupjoin(left, right, key=None, lkey=None, rkey=None, missing=None,
presorted=False, buffersize=None, tempdir=None, cache=True,
lprefix=None, rprefix=None):
"""
Perform a left join, but where the key is not unique in the right-hand
table, arbitrarily choose the first row and ignore others. E.g.::
>>> import petl as etl
>>> table1 = [['id', 'color', 'cost'],
... [1, 'blue', 12],
... [2, 'red', 8],
... [3, 'purple', 4]]
>>> table2 = [['id', 'shape', 'size'],
... [1, 'circle', 'big'],
... [1, 'circle', 'small'],
... [2, 'square', 'tiny'],
... [2, 'square', 'big'],
... [3, 'ellipse', 'small'],
... [3, 'ellipse', 'tiny']]
>>> table3 = etl.lookupjoin(table1, table2, key='id')
>>> table3
+----+----------+------+-----------+---------+
| id | color | cost | shape | size |
+====+==========+======+===========+=========+
| 1 | 'blue' | 12 | 'circle' | 'big' |
+----+----------+------+-----------+---------+
| 2 | 'red' | 8 | 'square' | 'tiny' |
+----+----------+------+-----------+---------+
| 3 | 'purple' | 4 | 'ellipse' | 'small' |
+----+----------+------+-----------+---------+
See also :func:`petl.transform.joins.leftjoin`.
.. versionchanged:: 1.7.16
To ensure correct results for tables with uneven rows, tables will be
squared up before joining to ensure correct results.
"""
lkey, rkey = keys_from_args(left, right, key, lkey, rkey)
return LookupJoinView(left, right, lkey, rkey, presorted=presorted,
missing=missing, buffersize=buffersize,
tempdir=tempdir, cache=cache,
lprefix=lprefix, rprefix=rprefix)
Table.lookupjoin = lookupjoin
class LookupJoinView(Table):
def __init__(self, left, right, lkey, rkey, presorted=False, missing=None,
buffersize=None, tempdir=None, cache=True,
lprefix=None, rprefix=None):
self.left = stack(left, missing=missing)
self.right = stack(right, missing=missing)
if not presorted:
self.left = sort(self.left, lkey, buffersize=buffersize,
tempdir=tempdir, cache=cache)
self.right = sort(self.right, rkey, buffersize=buffersize,
tempdir=tempdir, cache=cache)
self.lkey = lkey
self.rkey = rkey
self.missing = missing
self.lprefix = lprefix
self.rprefix = rprefix
def __iter__(self):
return iterlookupjoin(self.left, self.right, self.lkey, self.rkey,
missing=self.missing, lprefix=self.lprefix,
rprefix=self.rprefix)
def iterlookupjoin(left, right, lkey, rkey, missing=None, lprefix=None,
rprefix=None):
lit = iter(left)
rit = iter(right)
lhdr = next(lit)
rhdr = next(rit)
# determine indices of the key fields in left and right tables
lkind = asindices(lhdr, lkey)
rkind = asindices(rhdr, rkey)
# construct functions to extract key values from both tables
lgetk = operator.itemgetter(*lkind)
rgetk = operator.itemgetter(*rkind)
# determine indices of non-key fields in the right table
# (in the output, we only include key fields from the left table - we
# don't want to duplicate fields)
rvind = [i for i in range(len(rhdr)) if i not in rkind]
rgetv = rowgetter(*rvind)
# determine the output fields
if lprefix is None:
outhdr = list(lhdr)
else:
outhdr = [(text_type(lprefix) + text_type(f)) for f in lhdr]
if rprefix is None:
outhdr.extend(rgetv(rhdr))
else:
outhdr.extend([(text_type(rprefix) + text_type(f)) for f in rgetv(rhdr)])
yield tuple(outhdr)
# define a function to join two groups of rows
def joinrows(_lrowgrp, _rrowgrp):
if _rrowgrp is None:
for lrow in _lrowgrp:
outrow = list(lrow) # start with the left row
# extend with missing values in place of the right row
outrow.extend([missing] * len(rvind))
yield tuple(outrow)
else:
rrow = next(iter(_rrowgrp)) # pick first arbitrarily
for lrow in _lrowgrp:
# start with the left row
outrow = list(lrow)
# extend with non-key values from the right row
outrow.extend(rgetv(rrow))
yield tuple(outrow)
# construct group iterators for both tables
lgit = itertools.groupby(lit, key=lgetk)
rgit = itertools.groupby(rit, key=rgetk)
lrowgrp = []
# loop until *either* of the iterators is exhausted
lkval, rkval = None, None # initialise here to handle empty tables
try:
# pick off initial row groups
lkval, lrowgrp = next(lgit)
rkval, rrowgrp = next(rgit)
while True:
if lkval < rkval:
for row in joinrows(lrowgrp, None):
yield tuple(row)
# advance left
lkval, lrowgrp = next(lgit)
elif lkval > rkval:
# advance right
rkval, rrowgrp = next(rgit)
else:
for row in joinrows(lrowgrp, rrowgrp):
yield tuple(row)
# advance both
lkval, lrowgrp = next(lgit)
rkval, rrowgrp = next(rgit)
except StopIteration:
pass
# make sure any left rows remaining are yielded
if lkval > rkval:
# yield anything that got left hanging
for row in joinrows(lrowgrp, None):
yield tuple(row)
# yield the rest
for lkval, lrowgrp in lgit:
for row in joinrows(lrowgrp, None):
yield tuple(row)
def unjoin(table, value, key=None, autoincrement=(1, 1), presorted=False,
buffersize=None, tempdir=None, cache=True):
"""
Split a table into two tables by reversing an inner join. E.g.::
>>> import petl as etl
>>> # join key is present in the table
... table1 = (('foo', 'bar', 'baz'),
... ('A', 1, 'apple'),
... ('B', 1, 'apple'),
... ('C', 2, 'orange'))
>>> table2, table3 = etl.unjoin(table1, 'baz', key='bar')
>>> table2
+-----+-----+
| foo | bar |
+=====+=====+
| 'A' | 1 |
+-----+-----+
| 'B' | 1 |
+-----+-----+
| 'C' | 2 |
+-----+-----+
>>> table3
+-----+----------+
| bar | baz |
+=====+==========+
| 1 | 'apple' |
+-----+----------+
| 2 | 'orange' |
+-----+----------+
>>> # an integer join key can also be reconstructed
... table4 = (('foo', 'bar'),
... ('A', 'apple'),
... ('B', 'apple'),
... ('C', 'orange'))
>>> table5, table6 = etl.unjoin(table4, 'bar')
>>> table5
+-----+--------+
| foo | bar_id |
+=====+========+
| 'A' | 1 |
+-----+--------+
| 'B' | 1 |
+-----+--------+
| 'C' | 2 |
+-----+--------+
>>> table6
+----+----------+
| id | bar |
+====+==========+
| 1 | 'apple' |
+----+----------+
| 2 | 'orange' |
+----+----------+
The `autoincrement` parameter controls how an integer join key is
reconstructed, and should be a tuple of (`start`, `step`).
"""
if key is None:
# first sort the table by the value field
if presorted:
tbl_sorted = table
else:
tbl_sorted = sort(table, value, buffersize=buffersize,
tempdir=tempdir, cache=cache)
# on the left, return the original table but with the value field
# replaced by an incrementing integer
left = ConvertToIncrementingCounterView(tbl_sorted, value,
autoincrement)
# on the right, return a new table with distinct values from the
# given field
right = EnumerateDistinctView(tbl_sorted, value, autoincrement)
else:
# on the left, return distinct rows from the original table
# with the value field cut out
left = distinct(cutout(table, value))
# on the right, return distinct rows from the original table
# with all fields but the key and value cut out
right = distinct(cut(table, key, value))
return left, right
class ConvertToIncrementingCounterView(Table):
def __init__(self, tbl, value, autoincrement):
self.table = tbl
self.value = value
self.autoincrement = autoincrement
def __iter__(self):
it = iter(self.table)
hdr = next(it)
table = itertools.chain([hdr], it)
value = self.value
vidx = hdr.index(value)
outhdr = list(hdr)
outhdr[vidx] = '%s_id' % value
yield tuple(outhdr)
offset, multiplier = self.autoincrement
for n, (_, group) in enumerate(rowgroupby(table, value)):
for row in group:
outrow = list(row)
outrow[vidx] = (n * multiplier) + offset
yield tuple(outrow)
Table.unjoin = unjoin
class EnumerateDistinctView(Table):
def __init__(self, tbl, value, autoincrement):
self.table = tbl
self.value = value
self.autoincrement = autoincrement
def __iter__(self):
offset, multiplier = self.autoincrement
yield ('id', self.value)
for n, (v, _) in enumerate(rowgroupby(self.table, self.value)):
yield ((n * multiplier) + offset, v)
|