Skip to content

Instantly share code, notes, and snippets.

@taldcroft
Created February 4, 2013 22:08
Show Gist options
  • Select an option

  • Save taldcroft/4710147 to your computer and use it in GitHub Desktop.

Select an option

Save taldcroft/4710147 to your computer and use it in GitHub Desktop.
Test auto-creation of new columns in astropy.table
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "test_new_col"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"from astropy.table import Table, Column, MaskedColumn"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"ca = Column('a', [1, 2])\n",
"cb = Column('b', [3, 4])"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t = Table([ca])"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<table><tr><th>a</th></tr><tr><td>1</td></tr><tr><td>2</td></tr></table>"
],
"output_type": "pyout",
"prompt_number": 4,
"text": [
"<Table rows=2 names=('a')>\n",
"array([(1,), (2,)], \n",
" dtype=[('a', '<i8')])"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t['b'] = cb"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<table><tr><th>a</th><th>b</th></tr><tr><td>1</td><td>3</td></tr><tr><td>2</td><td>4</td></tr></table>"
],
"output_type": "pyout",
"prompt_number": 6,
"text": [
"<Table rows=2 names=('a','b')>\n",
"array([(1, 3), (2, 4)], \n",
" dtype=[('a', '<i8'), ('b', '<i8')])"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t['c'] = 2\n",
"t"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<table><tr><th>a</th><th>b</th><th>c</th></tr><tr><td>1</td><td>3</td><td>2</td></tr><tr><td>2</td><td>4</td><td>2</td></tr></table>"
],
"output_type": "pyout",
"prompt_number": 7,
"text": [
"<Table rows=2 names=('a','b','c')>\n",
"array([(1, 3, 2), (2, 4, 2)], \n",
" dtype=[('a', '<i8'), ('b', '<i8'), ('c', '<i8')])"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t['d'] = t['a']\n",
"t"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<table><tr><th>a</th><th>b</th><th>c</th><th>d</th></tr><tr><td>1</td><td>3</td><td>2</td><td>1</td></tr><tr><td>2</td><td>4</td><td>2</td><td>2</td></tr></table>"
],
"output_type": "pyout",
"prompt_number": 8,
"text": [
"<Table rows=2 names=('a','b','c','d')>\n",
"array([(1, 3, 2, 1), (2, 4, 2, 2)], \n",
" dtype=[('a', '<i8'), ('b', '<i8'), ('c', '<i8'), ('d', '<i8')])"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"try:\n",
" t['e'] = [4, 5, 6]\n",
"except ValueError as err:\n",
" print err"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"operands could not be broadcast together with shapes (2) (3) \n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t['e'] = t['b'] * 5\n",
"t"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<table><tr><th>a</th><th>b</th><th>c</th><th>d</th><th>e</th></tr><tr><td>1</td><td>3</td><td>2</td><td>1</td><td>15</td></tr><tr><td>2</td><td>4</td><td>2</td><td>2</td><td>20</td></tr></table>"
],
"output_type": "pyout",
"prompt_number": 10,
"text": [
"<Table rows=2 names=('a','b','c','d','e')>\n",
"array([(1, 3, 2, 1, 15), (2, 4, 2, 2, 20)], \n",
" dtype=[('a', '<i8'), ('b', '<i8'), ('c', '<i8'), ('d', '<i8'), ('e', '<i8')])"
]
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t = Table([ca], masked=True)\n",
"print repr(t)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<Table rows=2 names=('a')>\n",
"masked_array(data = [(1,) (2,)],\n",
" mask = [(False,) (False,)],\n",
" fill_value = (999999,),\n",
" dtype = [('a', '<i8')])\n",
"\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t['b'] = ['hello', 'world']\n",
"t"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<table><tr><th>a</th><th>b</th></tr><tr><td>1</td><td>hello</td></tr><tr><td>2</td><td>world</td></tr></table>"
],
"output_type": "pyout",
"prompt_number": 12,
"text": [
"<Table rows=2 names=('a','b')>\n",
"masked_array(data = [(1, 'hello') (2, 'world')],\n",
" mask = [(False, False) (False, False)],\n",
" fill_value = (999999, 'N/A'),\n",
" dtype = [('a', '<i8'), ('b', '|S5')])\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t['c'] = MaskedColumn('ignored', [1.2, 3.4])\n",
"t"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<table><tr><th>a</th><th>b</th><th>c</th></tr><tr><td>1</td><td>hello</td><td>1.2</td></tr><tr><td>2</td><td>world</td><td>3.4</td></tr></table>"
],
"output_type": "pyout",
"prompt_number": 13,
"text": [
"<Table rows=2 names=('a','b','c')>\n",
"masked_array(data = [(1, 'hello', 1.2) (2, 'world', 3.4)],\n",
" mask = [(False, False, False) (False, False, False)],\n",
" fill_value = (999999, 'N/A', 1e+20),\n",
" dtype = [('a', '<i8'), ('b', '|S5'), ('c', '<f8')])\n"
]
}
],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print repr(t)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<Table rows=2 names=('a','b','c')>\n",
"masked_array(data = [(1, 'hello', 1.2) (2, 'world', 3.4)],\n",
" mask = [(False, False, False) (False, False, False)],\n",
" fill_value = (999999, 'N/A', 1e+20),\n",
" dtype = [('a', '<i8'), ('b', '|S5'), ('c', '<f8')])\n",
"\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 14
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment