Skip to content

Commit 1a6987e

Browse files
committed
CLN: Deduplicate code in duplicated (pandas-dev#49550)
* CLN: Deduplicate code in duplicated * Clean * Fix
1 parent f4f9969 commit 1a6987e

File tree

1 file changed

+6
-19
lines changed

1 file changed

+6
-19
lines changed

pandas/_libs/hashtable_func_helper.pxi.in

+6-19
Original file line numberDiff line numberDiff line change
@@ -141,32 +141,18 @@ cdef duplicated_{{dtype}}(const {{dtype}}_t[:] values, object keep='first', cons
141141
if keep not in ('last', 'first', False):
142142
raise ValueError('keep must be either "first", "last" or False')
143143

144-
if keep == 'last':
144+
{{for cond, keep in [('if', '"last"'), ('elif', '"first"')]}}
145+
{{cond}} keep == {{keep}}:
145146
{{if dtype == 'object'}}
146147
if True:
147148
{{else}}
148149
with nogil:
149150
{{endif}}
151+
{{if keep == '"last"'}}
150152
for i in range(n - 1, -1, -1):
151-
# equivalent: range(n)[::-1], which cython doesn't like in nogil
152-
if uses_mask and mask[i]:
153-
if seen_na:
154-
out[i] = True
155-
else:
156-
out[i] = False
157-
seen_na = True
158-
else:
159-
value = {{to_c_type}}(values[i])
160-
kh_put_{{ttype}}(table, value, &ret)
161-
out[i] = ret == 0
162-
163-
elif keep == 'first':
164-
{{if dtype == 'object'}}
165-
if True:
166-
{{else}}
167-
with nogil:
168-
{{endif}}
153+
{{else}}
169154
for i in range(n):
155+
{{endif}}
170156
if uses_mask and mask[i]:
171157
if seen_na:
172158
out[i] = True
@@ -177,6 +163,7 @@ cdef duplicated_{{dtype}}(const {{dtype}}_t[:] values, object keep='first', cons
177163
value = {{to_c_type}}(values[i])
178164
kh_put_{{ttype}}(table, value, &ret)
179165
out[i] = ret == 0
166+
{{endfor}}
180167

181168
else:
182169
{{if dtype == 'object'}}

0 commit comments

Comments
 (0)