Created
May 5, 2017 11:35
-
-
Save cchwala/157b87d4e413b560f8ad8555a330b937 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import pandas as pd\n", | |
"import xarray as xr" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"t_minutes = np.arange(1.0,100000.0, 0.13, dtype=np.float64)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Timings " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"100 loops, best of 3: 4.35 ms per loop\n" | |
] | |
} | |
], | |
"source": [ | |
"%%timeit\n", | |
"\n", | |
"# Convert from float to int in nanoseconds and diretly to timedelta64[ns]\n", | |
"t_timedelta64ns = (t_minutes * 60 * 1e9).astype('timedelta64[ns]')\n", | |
"pd.TimedeltaIndex(t_timedelta64ns)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1 loop, best of 3: 5.23 s per loop\n" | |
] | |
} | |
], | |
"source": [ | |
"%%timeit\n", | |
"\n", | |
"# Let pd.to_timedelta do all the conversion\n", | |
"pd.to_timedelta(t_minutes, unit='m')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"100 loops, best of 3: 10.6 ms per loop\n" | |
] | |
} | |
], | |
"source": [ | |
"%%timeit\n", | |
"\n", | |
"# Convert from float to int in nanoseconds before pasing to timedelta64[ns]\n", | |
"pd.to_timedelta((t_minutes * 60 * 1e9).astype(np.int64), unit='ns')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Check output " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"TimedeltaIndex([ '0 days 00:01:00', '0 days 00:01:07.800000',\n", | |
" '0 days 00:01:15.600000', '0 days 00:01:23.399999',\n", | |
" '0 days 00:01:31.199999', '0 days 00:01:38.999999',\n", | |
" '0 days 00:01:46.799999', '0 days 00:01:54.599999',\n", | |
" '0 days 00:02:02.399999', '0 days 00:02:10.199999',\n", | |
" ...\n", | |
" '69 days 10:38:49.199999', '69 days 10:38:56.999999',\n", | |
" '69 days 10:39:04.799999', '69 days 10:39:12.599999',\n", | |
" '69 days 10:39:20.399999', '69 days 10:39:28.199999',\n", | |
" '69 days 10:39:35.999999', '69 days 10:39:43.799999',\n", | |
" '69 days 10:39:51.599999', '69 days 10:39:59.399999'],\n", | |
" dtype='timedelta64[ns]', length=769224, freq=None)" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"t_timedelta64ns = (t_minutes * 60 * 1e9).astype('timedelta64[ns]')\n", | |
"pd.TimedeltaIndex(t_timedelta64ns)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"TimedeltaIndex([ '0 days 00:01:00', '0 days 00:01:07.800000',\n", | |
" '0 days 00:01:15.600000', '0 days 00:01:23.400000',\n", | |
" '0 days 00:01:31.200000', '0 days 00:01:39',\n", | |
" '0 days 00:01:46.800000', '0 days 00:01:54.600000',\n", | |
" '0 days 00:02:02.400000', '0 days 00:02:10.200000',\n", | |
" ...\n", | |
" '69 days 10:38:49.200000', '69 days 10:38:57',\n", | |
" '69 days 10:39:04.800000', '69 days 10:39:12.600000',\n", | |
" '69 days 10:39:20.400000', '69 days 10:39:28.200000',\n", | |
" '69 days 10:39:36', '69 days 10:39:43.800000',\n", | |
" '69 days 10:39:51.600000', '69 days 10:39:59.400000'],\n", | |
" dtype='timedelta64[ns]', length=769224, freq=None)" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"pd.to_timedelta(t_minutes, unit='m')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"TimedeltaIndex([ '0 days 00:01:00', '0 days 00:01:07.800000',\n", | |
" '0 days 00:01:15.600000', '0 days 00:01:23.399999',\n", | |
" '0 days 00:01:31.199999', '0 days 00:01:38.999999',\n", | |
" '0 days 00:01:46.799999', '0 days 00:01:54.599999',\n", | |
" '0 days 00:02:02.399999', '0 days 00:02:10.199999',\n", | |
" ...\n", | |
" '69 days 10:38:49.199999', '69 days 10:38:56.999999',\n", | |
" '69 days 10:39:04.799999', '69 days 10:39:12.599999',\n", | |
" '69 days 10:39:20.399999', '69 days 10:39:28.199999',\n", | |
" '69 days 10:39:35.999999', '69 days 10:39:43.799999',\n", | |
" '69 days 10:39:51.599999', '69 days 10:39:59.399999'],\n", | |
" dtype='timedelta64[ns]', length=769224, freq=None)" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"pd.to_timedelta((t_minutes * 60 * 1e9).astype(np.int64), unit='ns')" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.13" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment