Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #412: distinct() filters None #414

Merged
merged 1 commit into from Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions petl/test/transform/test_dedup.py
Expand Up @@ -175,10 +175,12 @@ def test_distinct():
('A', 1, 2),
('B', '2', '3.4'),
('B', '2', '3.4'),
('D', 4, 12.3))
('D', 4, 12.3),
(None, None, None))

result = distinct(table)
expect = (('foo', 'bar', 'baz'),
(None, None, None),
('A', 1, 2),
('B', '2', '3.4'),
('D', 4, 12.3))
Expand All @@ -188,13 +190,15 @@ def test_distinct():
def test_distinct_count():

table = (('foo', 'bar', 'baz'),
(None, None, None),
('A', 1, 2),
('B', '2', '3.4'),
('B', '2', '3.4'),
('D', 4, 12.3))

result = distinct(table, count='count')
expect = (('foo', 'bar', 'baz', 'count'),
(None, None, None, 1),
('A', 1, 2, 1),
('B', '2', '3.4', 2),
('D', 4, 12.3, 1))
Expand All @@ -204,13 +208,15 @@ def test_distinct_count():
def test_key_distinct():

table = (('foo', 'bar', 'baz'),
(None, None, None),
('A', 1, 2),
('B', '2', '3.4'),
('B', '2', '5'),
('D', 4, 12.3))

result = distinct(table, key='foo')
expect = (('foo', 'bar', 'baz'),
(None, None, None),
('A', 1, 2),
('B', '2', '3.4'),
('D', 4, 12.3))
Expand All @@ -223,10 +229,12 @@ def test_key_distinct_2():
tbl = (('a', 'b'),
('x', '1'),
('x', '3'),
('y', '1'))
('y', '1'),
(None, None))

result = distinct(tbl, key='b')
expect = (('a', 'b'),
(None, None),
('x', '1'),
('x', '3'))
ieq(expect, result)
Expand All @@ -238,10 +246,12 @@ def test_key_distinct_count():
('A', 1, 2),
('B', '2', '3.4'),
('B', '2', '5'),
('D', 4, 12.3))
('D', 4, 12.3),
(None, None, None))

result = distinct(table, key='foo', count='count')
expect = (('foo', 'bar', 'baz', 'count'),
(None, None, None, 1),
('A', 1, 2, 1),
('B', '2', '3.4', 2),
('D', 4, 12.3, 1))
Expand Down
7 changes: 4 additions & 3 deletions petl/transform/dedup.py
Expand Up @@ -416,13 +416,14 @@ def __iter__(self):
# the field selection
getkey = operator.itemgetter(*indices)

INIT = object()
if self.count:
hdr = tuple(hdr) + (self.count,)
yield hdr
previous = None
previous = INIT
n_dup = 1
for row in it:
if previous is None:
if previous is INIT:
previous = row
else:
kprev = getkey(previous)
Expand All @@ -437,7 +438,7 @@ def __iter__(self):
yield tuple(previous) + (n_dup,)
else:
yield tuple(hdr)
previous_keys = None
previous_keys = INIT
for row in it:
keys = getkey(row)
if keys != previous_keys:
Expand Down