Created
June 10, 2014 11:29
-
-
Save catawbasam/003743259cf0a6ec968d to your computer and use it in GitHub Desktop.
draft of push!(df::DataFrame, iterable)
This file contains 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
{ | |
"metadata": { | |
"language": "Julia", | |
"name": "", | |
"signature": "sha256:7fe5fda6897de221effe8497f91c4705de9d01b5396e52caecc256d571986d30" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## push! an iterable with data for a new row into a dataframe" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"using DataFrames" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### a DataFrame is an array of DataArrays. push! to each, if possible\n", | |
"if a few columns work and then we have a failure, need to clean up." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"da'" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 28, | |
"text": [ | |
"1x7 DataArray{Int64,2}:\n", | |
" 1 2 4 5 0 1 88" | |
] | |
} | |
], | |
"prompt_number": 28 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import Base.push!\n", | |
"function push!(df::DataFrame, iterable)\n", | |
" K = length(iterable)\n", | |
" assert(size(df,2)==K)\n", | |
" i=1\n", | |
" for t in iterable\n", | |
" try \n", | |
" #println(i,t, typeof(t))\n", | |
" push!(df.columns[i], t)\n", | |
" catch\n", | |
" #clean up partial row\n", | |
" for j in 1:(i-1)\n", | |
" pop!(df.columns[j])\n", | |
" end\n", | |
" msg = \"Error adding $t to column $i.\"\n", | |
" throw(ArgumentError(msg))\n", | |
" end \n", | |
" i=i+1\n", | |
" end\n", | |
"end" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 71, | |
"text": [ | |
"push! (generic function with 20 methods)" | |
] | |
} | |
], | |
"prompt_number": 71 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"df= DataFrame( first=[1,2], second=[\"apple\",\"orange\"] ) " | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<table><tr><th></th><th>first</th><th>second</th></tr><tr><th>1</th><td>1</td><td>apple</td></tr><tr><th>2</th><td>2</td><td>orange</td></tr></table>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 72, | |
"text": [ | |
"2x2 DataFrame\n", | |
"|-------|-------|----------|\n", | |
"| Row # | first | second |\n", | |
"| 1 | 1 | \"apple\" |\n", | |
"| 2 | 2 | \"orange\" |" | |
] | |
} | |
], | |
"prompt_number": 72 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"push!(df, [3,\"pear\"])\n", | |
"df" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<table><tr><th></th><th>first</th><th>second</th></tr><tr><th>1</th><td>1</td><td>apple</td></tr><tr><th>2</th><td>2</td><td>orange</td></tr><tr><th>3</th><td>3</td><td>pear</td></tr></table>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 73, | |
"text": [ | |
"3x2 DataFrame\n", | |
"|-------|-------|----------|\n", | |
"| Row # | first | second |\n", | |
"| 1 | 1 | \"apple\" |\n", | |
"| 2 | 2 | \"orange\" |\n", | |
"| 3 | 3 | \"pear\" |" | |
] | |
} | |
], | |
"prompt_number": 73 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"push!(df, (4,\"banana\"))\n", | |
"df" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<table><tr><th></th><th>first</th><th>second</th></tr><tr><th>1</th><td>1</td><td>apple</td></tr><tr><th>2</th><td>2</td><td>orange</td></tr><tr><th>3</th><td>3</td><td>pear</td></tr><tr><th>4</th><td>4</td><td>banana</td></tr></table>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 74, | |
"text": [ | |
"4x2 DataFrame\n", | |
"|-------|-------|----------|\n", | |
"| Row # | first | second |\n", | |
"| 1 | 1 | \"apple\" |\n", | |
"| 2 | 2 | \"orange\" |\n", | |
"| 3 | 3 | \"pear\" |\n", | |
"| 4 | 4 | \"banana\" |" | |
] | |
} | |
], | |
"prompt_number": 74 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"push!(df, (4,77.8))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "LoadError", | |
"evalue": "ArgumentError(\"Error adding 77.8 to column 2.\")\nwhile loading In[75], in expression starting on line 1", | |
"output_type": "pyerr", | |
"traceback": [ | |
"ArgumentError(\"Error adding 77.8 to column 2.\")\nwhile loading In[75], in expression starting on line 1", | |
" in push! at In[71]:16" | |
] | |
} | |
], | |
"prompt_number": 75 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"push!(df, (\"bad\",\"stuff\"))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "LoadError", | |
"evalue": "ArgumentError(\"Error adding bad to column 1.\")\nwhile loading In[77], in expression starting on line 1", | |
"output_type": "pyerr", | |
"traceback": [ | |
"ArgumentError(\"Error adding bad to column 1.\")\nwhile loading In[77], in expression starting on line 1", | |
" in push! at In[71]:16" | |
] | |
} | |
], | |
"prompt_number": 77 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"push!(df, (44.4,\"pineapple\"))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "LoadError", | |
"evalue": "ArgumentError(\"Error adding 44.4 to column 1.\")\nwhile loading In[78], in expression starting on line 1", | |
"output_type": "pyerr", | |
"traceback": [ | |
"ArgumentError(\"Error adding 44.4 to column 1.\")\nwhile loading In[78], in expression starting on line 1", | |
" in push! at In[71]:16" | |
] | |
} | |
], | |
"prompt_number": 78 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"df" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<table><tr><th></th><th>first</th><th>second</th></tr><tr><th>1</th><td>1</td><td>apple</td></tr><tr><th>2</th><td>2</td><td>orange</td></tr><tr><th>3</th><td>3</td><td>pear</td></tr><tr><th>4</th><td>4</td><td>banana</td></tr></table>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 79, | |
"text": [ | |
"4x2 DataFrame\n", | |
"|-------|-------|----------|\n", | |
"| Row # | first | second |\n", | |
"| 1 | 1 | \"apple\" |\n", | |
"| 2 | 2 | \"orange\" |\n", | |
"| 3 | 3 | \"pear\" |\n", | |
"| 4 | 4 | \"banana\" |" | |
] | |
} | |
], | |
"prompt_number": 79 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment