Skip to content

Working with pd.Period is very slow #13345

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

Closed
benrifkind opened this issue Jun 1, 2016 · 1 comment
Closed

Working with pd.Period is very slow #13345

benrifkind opened this issue Jun 1, 2016 · 1 comment
Labels
Performance Memory or execution speed performance Period Period data type

Comments

@benrifkind
Copy link

I find working with pd.Period very slow. Much much slower than working with pd.Timestamp and I was wondering why that is. I have around 2 million records and I'm forced to work with full timestamps even though my data is really on the level of months. Here is an example that I think shows the discrepancy between working with these types.

Code Sample, a copy-pastable example if possible

import pandas as pd
import numpy as np

# create a data frame with 500K dates
dates = pd.date_range('1/1/2011', periods=500000, freq='H')
values = np.random.random(size=len(dates))
df = pd.DataFrame({"Date":dates, "Values":values})

Creating the period index is fairly slow

%%time
df["Period"] = df.Date.dt.to_period(freq="1M")
Wall time: 1.47 s

And using boolean indexing with periods is extremely slow

%%time
near_future = df[df.Period <= df.Period.iloc[0] + 3] 
Wall time: 7.36 s

Especially when compared with using timestamps!

%%time
near_future = df[df.Date <= df.Date.iloc[0] + pd.Timedelta("90d")]
Wall time: 0 ns

Returning the values of the period is also very slow

%%time
periods = df.Period.unique()
Wall time: 17.4 s
%%time
times = df.Date.unique()
Wall time: 69 ms

Unrelated but I also found that I can only do the period comparison on a series in one direction. This throws an error even though all I did was flip the terms in the inequality

near_future = df[(df.Period.iloc[0] + 3 ) >= df.Period] 
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-8-f626f4b8a04f> in <module>()
----> 1 near_future = df[(df.Period.iloc[0] + 3 ) >= df.Period]


pandas\src\period.pyx in pandas._period.Period.__richcmp__ (pandas\src\period.c:12753)()


TypeError: Cannot compare type 'Period' with type 'Series'

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 61 Stepping 4, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.18.1
nose: 1.3.7
pip: 8.1.2
setuptools: 21.2.2
Cython: 0.24
numpy: 1.11.0
scipy: 0.17.0
statsmodels: 0.6.1
xarray: None
IPython: 4.1.2
sphinx: 1.3.1
patsy: 0.4.0
dateutil: 2.5.3
pytz: 2016.4
blosc: None
bottleneck: None
tables: 3.2.2
numexpr: 2.5.2
matplotlib: 1.5.1
openpyxl: 2.2.6
xlrd: 0.9.4
xlwt: 1.0.0
xlsxwriter: 0.7.7
lxml: 3.4.4
bs4: 4.4.1
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.11
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.38.0
pandas_datareader: None
@jreback
Copy link
Contributor

jreback commented Jun 1, 2016

this is a symptom of #7964 it is an object dtype now (actually has been forever) and is not first class. contributions along those lines are welcome.

@jreback jreback closed this as completed Jun 1, 2016
@jreback jreback added Performance Memory or execution speed performance Period Period data type labels Jun 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Performance Memory or execution speed performance Period Period data type
Projects
None yet
Development

No branches or pull requests

2 participants