It is better not to use pd.tseries.offsets.DateOffset.__mul__
.
pandas provides a class called pd.Timestamp
to handle dates and times. Also, if you want to calculate a date and time that is a certain period away from a certain date and time, use pd.tseries.offsets.DateOffset
.
This time, I'll show you the interesting behavior of pd.tseries.offsets.DateOffset
.
First, look at the execution result below.
$ python -m timeit -u msec -n 10 -s "import pandas as pd" "pd.Timestamp('2010-01-01 00:00:00') + 100 * pd.tseries.offsets.DateOffset(seconds=1)"
10 loops, best of 3: 0.438 msec per loop
$ python -m timeit -u msec -n 10 -s "import pandas as pd" "pd.Timestamp('2010-01-01 00:00:00') + 1000 * pd.tseries.offsets.DateOffset(seconds=1)"
10 loops, best of 3: 3.85 msec per loop
$ python -m timeit -u msec -n 10 -s "import pandas as pd" "pd.Timestamp('2010-01-01 00:00:00') + 10000 * pd.tseries.offsets.DateOffset(seconds=1)"
10 loops, best of 3: 41.9 msec per loop
Do you know what you mean?
** Execution time increases linearly by multiplying DateOffset. ** **
Probably, it is called internally as many times as pd.Timestamp.__add__
is multiplied.
This is a good way to do the same thing.
$ python -m timeit -u msec -n 10 -s "import pandas as pd" "pd.Timestamp('2010-01-01 00:00:00') + pd.tseries.offsets.DateOffset(seconds=1*100)"
10 loops, best of 3: 0.0328 msec per loop
$ python -m timeit -u msec -n 10 -s "import pandas as pd" "pd.Timestamp('2010-01-01 00:00:00') + pd.tseries.offsets.DateOffset(seconds=1*1000)"
10 loops, best of 3: 0.0373 msec per loop
$ python -m timeit -u msec -n 10 -s "import pandas as pd" "pd.Timestamp('2010-01-01 00:00:00') + pd.tseries.offsets.DateOffset(seconds=1*10000)"
10 loops, best of 3: 0.0336 msec per loop
It became fast (or rather, intuitive behavior).
By the way, if you use datetime of python standard module, it will be like this. It's very fast.
$ python -m timeit -u msec -n 10 -s "import datetime" "datetime.datetime(2010,1,1) + 100 * datetime.timedelta(seconds=1)"
10 loops, best of 3: 0.0031 msec per loop
$ python -m timeit -u msec -n 10 -s "import datetime" "datetime.datetime(2010,1,1) + 1000 * datetime.timedelta(seconds=1)"
10 loops, best of 3: 0.00276 msec per loop
$ python -m timeit -u msec -n 10 -s "import datetime" "datetime.datetime(2010,1,1) + 10000 * datetime.timedelta(seconds=1)"
10 loops, best of 3: 0.00227 msec per loop
Recommended Posts