-
-
Save justmarkham/e792d10ce63b0f1136e303ff5e55361e to your computer and use it in GitHub Desktop.
Data School's Tuesday Tip #7
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": "markdown", | |
"id": "fb685b42", | |
"metadata": {}, | |
"source": [ | |
"## [Tuesday Tip #7](https://tuesday.tips/): Time zones and Daylight Savings Time in pandas" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "869919ba", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "dd80c5b3", | |
"metadata": {}, | |
"source": [ | |
"[List of offset aliases](https://pandas.pydata.org/docs/user_guide/timeseries.html#timeseries-offset-aliases) (such as \"H\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "a888cdd3", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0 2023-03-12 04:00:00\n", | |
"1 2023-03-12 05:00:00\n", | |
"2 2023-03-12 06:00:00\n", | |
"3 2023-03-12 07:00:00\n", | |
"4 2023-03-12 08:00:00\n", | |
"5 2023-03-12 09:00:00\n", | |
"dtype: datetime64[ns]" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"times = pd.Series(pd.date_range(start='2023-03-12 4:00', periods=6, freq='H'))\n", | |
"times" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "33f98c8e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0 2023-03-12 04:00:00+00:00\n", | |
"1 2023-03-12 05:00:00+00:00\n", | |
"2 2023-03-12 06:00:00+00:00\n", | |
"3 2023-03-12 07:00:00+00:00\n", | |
"4 2023-03-12 08:00:00+00:00\n", | |
"5 2023-03-12 09:00:00+00:00\n", | |
"dtype: datetime64[ns, UTC]" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"UTC_times = times.dt.tz_localize('UTC')\n", | |
"UTC_times" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "e221ee76", | |
"metadata": {}, | |
"source": [ | |
"[List of time zones](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) (such as \"America/New_York\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "107d60e7", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0 2023-03-11 23:00:00-05:00\n", | |
"1 2023-03-12 00:00:00-05:00\n", | |
"2 2023-03-12 01:00:00-05:00\n", | |
"3 2023-03-12 03:00:00-04:00\n", | |
"4 2023-03-12 04:00:00-04:00\n", | |
"5 2023-03-12 05:00:00-04:00\n", | |
"dtype: datetime64[ns, America/New_York]" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"UTC_times.dt.tz_convert('America/New_York')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "003d4346", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"times = pd.Series(pd.date_range(start='2023-11-05 3:00', periods=6, freq='H'))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "34163f28", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0 2023-11-04 23:00:00-04:00\n", | |
"1 2023-11-05 00:00:00-04:00\n", | |
"2 2023-11-05 01:00:00-04:00\n", | |
"3 2023-11-05 01:00:00-05:00\n", | |
"4 2023-11-05 02:00:00-05:00\n", | |
"5 2023-11-05 03:00:00-05:00\n", | |
"dtype: datetime64[ns, America/New_York]" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"times.dt.tz_localize('UTC').dt.tz_convert('America/New_York')" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.9.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment