allow rename to work with any Mapping, not just dict by jab · Pull Request #11461 · pandas-dev/pandas (original) (raw)
@jreback Just to be clear, try/except clauses are only slow when an exception is raised and so you should only worry about avoiding them for performance when you think the raise/not raise ratio cases go against exceptions, otherwise try/except is faster than an if
statement in the successful case.
PS C:\Users\brcan> py -m timeit -c "example = {'a': 42}" "try: example['a']" "except KeyError: pass"
10000000 loops, best of 3: 0.13 usec per loop
PS C:\Users\brcan> py -m timeit -c "example = {'a': 42}" "if isinstance(example, dict):" " example['a']"
1000000 loops, best of 3: 0.209 usec per loop
PS C:\Users\brcan> py -m timeit -c "example = {'a': 42}" "if isinstance(example, list):" " example['a']"
1000000 loops, best of 3: 0.212 usec per loop
PS C:\Users\brcan> py -m timeit -c "example = {'a': 42}" "try: example.append('b')" "except AttributeError: pass"
1000000 loops, best of 3: 0.565 usec per loop