Skip to content

Instantly share code, notes, and snippets.

@gdevanla
Created October 9, 2015 04:58
Show Gist options
  • Save gdevanla/a70a4a72ec07b8f71201 to your computer and use it in GitHub Desktop.
Save gdevanla/a70a4a72ec07b8f71201 to your computer and use it in GitHub Desktop.
pandas-0.13-bug
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "pandas 0.13 bug"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": "import pandas as pd\nimport datetime",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": "# First frame\ndf1 = pd.DataFrame(dict(a=list(range(1,4)), b=list(range(1,4))))\ndf1.columns = pd.MultiIndex.from_product([['frame1'], df1.columns])",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": "df1",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr>\n <th></th>\n <th colspan=\"2\" halign=\"left\">frame1</th>\n </tr>\n <tr>\n <th></th>\n <th>a</th>\n <th>b</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td> 1</td>\n <td> 1</td>\n </tr>\n <tr>\n <th>1</th>\n <td> 2</td>\n <td> 2</td>\n </tr>\n <tr>\n <th>2</th>\n <td> 3</td>\n <td> 3</td>\n </tr>\n </tbody>\n</table>\n<p>3 rows \u00d7 2 columns</p>\n</div>",
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": " frame1 \n a b\n0 1 1\n1 2 2\n2 3 3\n\n[3 rows x 2 columns]"
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": "# Second frame with a datetime in first column\n\ndf2 = pd.DataFrame(dict(c=[datetime.datetime.now()]*3, d=[1,2,3]))\ndf2.columns = pd.MultiIndex.from_product([['frame2'], df2.columns])\n",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": "df2 ",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr>\n <th></th>\n <th colspan=\"2\" halign=\"left\">frame2</th>\n </tr>\n <tr>\n <th></th>\n <th>c</th>\n <th>d</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>2015-10-08 21:47:01.106762</td>\n <td> 1</td>\n </tr>\n <tr>\n <th>1</th>\n <td>2015-10-08 21:47:01.106762</td>\n <td> 2</td>\n </tr>\n <tr>\n <th>2</th>\n <td>2015-10-08 21:47:01.106762</td>\n <td> 3</td>\n </tr>\n </tbody>\n</table>\n<p>3 rows \u00d7 2 columns</p>\n</div>",
"metadata": {},
"output_type": "pyout",
"prompt_number": 16,
"text": " frame2 frame2\n c d\n0 2015-10-08 21:47:01.106762 1\n1 2015-10-08 21:47:01.106762 2\n2 2015-10-08 21:47:01.106762 3\n\n[3 rows x 2 columns]"
}
],
"prompt_number": 16
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": "# now create an empty dataframe using df2\ndf3=df2.loc[[]]\ndf3",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n<table border=\"1\" class=\"dataframe\">\n <tbody>\n <tr>\n <td>Index([], dtype='object')</td>\n <td>Empty DataFrame</td>\n </tr>\n </tbody>\n</table>\n<p>0 rows \u00d7 2 columns</p>\n</div>",
"metadata": {},
"output_type": "pyout",
"prompt_number": 17,
"text": "Empty DataFrame\nColumns: [(frame2, c), (frame2, d)]\nIndex: []\n\n[0 rows x 2 columns]"
}
],
"prompt_number": 17
},
{
"cell_type": "code",
"collapsed": false,
"input": "df3.columns",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 18,
"text": "MultiIndex(levels=[['frame2'], ['c', 'd']],\n labels=[[0, 0], [0, 1]])"
}
],
"prompt_number": 18
},
{
"cell_type": "code",
"collapsed": false,
"input": "# now merge df1 with df3\n\nmerged_df = pd.merge(df1, df3, how='left' , left_index=True, right_index=True)",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 22
},
{
"cell_type": "code",
"collapsed": false,
"input": "merged_df",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr>\n <th></th>\n <th colspan=\"2\" halign=\"left\">frame1</th>\n <th colspan=\"2\" halign=\"left\">frame2</th>\n </tr>\n <tr>\n <th></th>\n <th>a</th>\n <th>b</th>\n <th>c</th>\n <th>d</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td> 1</td>\n <td> 1</td>\n <td>NaT</td>\n <td>NaN</td>\n </tr>\n <tr>\n <th>1</th>\n <td> 2</td>\n <td> 2</td>\n <td>NaT</td>\n <td>NaN</td>\n </tr>\n <tr>\n <th>2</th>\n <td> 3</td>\n <td> 3</td>\n <td>NaT</td>\n <td>NaN</td>\n </tr>\n </tbody>\n</table>\n<p>3 rows \u00d7 4 columns</p>\n</div>",
"metadata": {},
"output_type": "pyout",
"prompt_number": 23,
"text": " frame1 frame2 frame2\n a b c d\n0 1 1 NaT NaN\n1 2 2 NaT NaN\n2 3 3 NaT NaN\n\n[3 rows x 4 columns]"
}
],
"prompt_number": 23
},
{
"cell_type": "code",
"collapsed": false,
"input": "# Now, here is the climax, observe how the values change based on how they are indexed into\nmerged_df['frame2'].loc[1, 'd'], merged_df['frame2'].at[1, 'd'], merged_df.loc[1, ('frame2', 'd')]",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 24,
"text": "(NaT, nan, nan)"
}
],
"prompt_number": 24
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": "*"
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": "*"
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment