Skip to content

Instantly share code, notes, and snippets.

@SaswatPadhi
Created September 13, 2019 18:42
Show Gist options
  • Save SaswatPadhi/93974b55e715adb612fd0f9c28867d90 to your computer and use it in GitHub Desktop.
Save SaswatPadhi/93974b55e715adb612fd0f9c28867d90 to your computer and use it in GitHub Desktop.
stackoverflow.com#57928364
# Comparing solutions for: https://stackoverflow.com/questions/57928364/pandas-and-tuple-check/57928434#57928434
import random
import string
import pandas as pd
def rnd_str(l):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(l))
unique_strings = set(rnd_str(3) for _ in range(20000))
cols = pd.Index(unique_strings)
tup = tuple(rnd_str(3) for _ in range(5000))
%timeit all(cols.get_indexer(tup)>-1)
# 714 µs ± 12.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit all(e in cols for e in tup)
# 639 ns ± 0.988 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment