Skip to content

Instantly share code, notes, and snippets.

@jiffyclub
Last active June 10, 2016 18:51
Show Gist options
  • Save jiffyclub/607c8cca4feb90e5c13d753d11846138 to your computer and use it in GitHub Desktop.
Save jiffyclub/607c8cca4feb90e5c13d753d11846138 to your computer and use it in GitHub Desktop.
Example of doing Pandas date conversion using a converter function
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Examples of using a conversion function to convert some coded integers to a datetime while reading a file."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import datetime as dt\n",
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This function takes an integer, 0-pads it to a six digit string, and tries to convert it to a datetime. If there's any error during the conversion it returns `None`."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def date_converter(date_num):\n",
" date_str = '{:06}'.format(date_num)\n",
"\n",
" try:\n",
" return dt.datetime.strptime(date_str, '%m%d%y')\n",
" except:\n",
" return None"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"datetime.datetime(2012, 6, 4, 0, 0)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"date_converter(60412)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"datetime.datetime(2012, 10, 4, 0, 0)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"date_converter(100412)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"date_converter(0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If the column names are in the Excel file you can specify the column name and a conversion function while reading the file:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"pd.read_excel(f, header=None, sheetname='Sheet1', converters={'Date Paid/Coded': date_converter})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If there are no column names you can still specify the converter based on an integer column number (the numbering starts from 0):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"pd.read_excel(f, header=None, sheetname='Sheet1', converters={9: date_converter})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you want to do the conversion after reading the DataFrame out of the Excel file you can use the Series.map method to apply a function to the values in a column:"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Date Paid/Coded</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2012-06-04</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2012-10-04</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>NaT</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Date Paid/Coded\n",
"0 2012-06-04\n",
"1 2012-10-04\n",
"2 NaT"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.DataFrame({'Date Paid/Coded': [60412, 100412, 0]})\n",
"df['Date Paid/Coded'] = df['Date Paid/Coded'].map(date_converter)\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment