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

ENH: Faster hashing of Period objects #12817

Closed
wcbeard opened this issue Apr 6, 2016 · 2 comments
Closed

ENH: Faster hashing of Period objects #12817

wcbeard opened this issue Apr 6, 2016 · 2 comments
Labels
Dtype Conversions Unexpected or buggy dtype conversions Performance Memory or execution speed performance Period Period data type
Milestone

Comments

@wcbeard
Copy link
Contributor

wcbeard commented Apr 6, 2016

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

@sinhrks
Copy link
Member

sinhrks commented Apr 6, 2016

The root cause is Period is internally stored as object. xref #7964

@sinhrks sinhrks added Performance Memory or execution speed performance Dtype Conversions Unexpected or buggy dtype conversions Period Period data type labels Apr 6, 2016
@sinhrks sinhrks modified the milestones: Next Major Release, 0.19.0 Apr 6, 2016
@shoyer
Copy link
Member

shoyer commented Apr 6, 2016

A cleaner way to do this might be to update DateOffset.__hash__ to use freqstr or rule_code instead of _params().

@jreback jreback modified the milestones: 0.19.0, 0.20.0 Jul 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions Performance Memory or execution speed performance Period Period data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants