ENH: Faster hashing of Period objects · Issue #12817 · pandas-dev/pandas (original) (raw)
I've noticed that a lot of manipulations using Periods are pretty slow. It looks like it's hashing the tuple of the ordinal
and freq
attributes. I'm not sure what the mapping is between freq
and freqstr
is, but if freqstr
can stand in for freq
, it looks like hashing the string gives a decent speedup. Subclassing Period
with this change speeds up operations like drop_duplicates
a lot on my machine.
class Period_hsh(Period):
def __hash__(self):
return hash((self.ordinal, self.freqstr))
p = Period('2016', 'A-DEC')
ph = Period_hsh(p)
%timeit Series([p] * 100000).drop_duplicates()
%timeit Series([ph] * 100000).drop_duplicates()
"""
1 loops, best of 3: 1.14 s per loop
10 loops, best of 3: 171 ms per loop
"""
Does this change seem reasonable?
output of pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Darwin
OS-release: 15.3.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
pandas: 0.18.0
nose: None
pip: 8.1.1
setuptools: 20.3
Cython: 0.23.5
numpy: 1.10.4
scipy: 0.17.0
statsmodels: None
xarray: None
IPython: 3.2.3
sphinx: None
patsy: None
dateutil: 2.5.2
pytz: 2016.3
blosc: None
bottleneck: None
tables: None
numexpr: 2.5.1
matplotlib: 1.5.1
openpyxl: None
xlrd: 0.9.4
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None