Last active
August 29, 2015 14:07
-
-
Save taldcroft/c0a7bade5f62195a0c1a to your computer and use it in GitHub Desktop.
Demo of astropy table mixin columns and Quantity support
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
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:d273286dd5912b94ea89ddb491cc903848ab5d3330db854b341e691c11167ef7" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Demonstration of mixin columns and Quantity support\n", | |
"\n", | |
"Assumes https://github.com/taldcroft/astropy/pull/11 (`taldcroft/table-mixin` branch). (Note there is an old `table/mixin` branch that is different)." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from astropy.table import *\n", | |
"from astropy.table import table\n", | |
"import numpy as np\n", | |
"from astropy.units import Quantity\n", | |
"import astropy.units as u\n", | |
"from astropy.time import Time, TimeDelta\n", | |
"from astropy.coordinates import SkyCoord" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Make a standard table from time, skycoord, and quantity" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"tm = Time(['2000-01-02', '2000-01-02'])\n", | |
"sc = SkyCoord([[1,2], [3,4]], unit=(u.deg, u.deg))\n", | |
"q = [1, 2] * u.m" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"t_no_q = Table([[1,2], q, tm, sc], names=('a', 'quant', 'time', 'skycoord'))\n", | |
"t_no_q" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<table id=\"table4434220752\"><thead><tr><th>a</th><th>quant</th><th>time</th><th>icrs</th></tr></thead><thead><tr><th></th><th>m</th><th></th><th></th></tr></thead><tr><td>1</td><td>1.0</td><td>2000-01-02 00:00:00.000</td><td><SkyCoord (ICRS): ra=1.0 deg, dec=2.0 deg></td></tr><tr><td>2</td><td>2.0</td><td>2000-01-02 00:00:00.000</td><td><SkyCoord (ICRS): ra=3.0 deg, dec=4.0 deg></td></tr></table>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 3, | |
"text": [ | |
"<Table rows=2 names=('a','quant','time','icrs') units=(None,'m',None,None)>\n", | |
"array([ (1, 1.0, <Time object: scale='utc' format='iso' value=2000-01-02 00:00:00.000>, <SkyCoord (ICRS): ra=1.0 deg, dec=2.0 deg>),\n", | |
" (2, 2.0, <Time object: scale='utc' format='iso' value=2000-01-02 00:00:00.000>, <SkyCoord (ICRS): ra=3.0 deg, dec=4.0 deg>)], \n", | |
" dtype=[('a', '<i8'), ('quant', '<f8'), ('time', 'O'), ('icrs', 'O')])" | |
] | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"t_no_q.use_quantity" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 4, | |
"text": [ | |
"False" | |
] | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# No quantity, just a regular Column\n", | |
"type(t_no_q['quant'])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 5, | |
"text": [ | |
"astropy.table.column.Column" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Adding a Quantity col still results in regular Column => CONSISTENCY\n", | |
"t_no_q['q2'] = [1, 2] * (u.km / u.s)\n", | |
"print type(t_no_q['q2'])\n", | |
"print t_no_q['q2'].unit" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"<class 'astropy.table.column.Column'>\n", | |
"km / s\n" | |
] | |
} | |
], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Make a quantity table" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"t = QTable([[1,2], q, tm, sc], names=('a', 'quant', 'time', 'skycoord'))\n", | |
"t" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<table id=\"table4434221072\"><thead><tr><th>a</th><th>quant</th><th>time</th><th>icrs</th></tr></thead><thead><tr><th></th><th>m</th><th></th><th></th></tr></thead><tr><td>1</td><td>1.0 m</td><td>2000-01-02 00:00:00.000</td><td><SkyCoord (ICRS): ra=1.0 deg, dec=2.0 deg></td></tr><tr><td>2</td><td>2.0 m</td><td>2000-01-02 00:00:00.000</td><td><SkyCoord (ICRS): ra=3.0 deg, dec=4.0 deg></td></tr></table>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 7, | |
"text": [ | |
"<Table rows=2 names=('a','quant','time','icrs') units=(None,'m',None,None)>\n", | |
"array([ (1, 1.0, <Time object: scale='utc' format='iso' value=2000-01-02 00:00:00.000>, <SkyCoord (ICRS): ra=1.0 deg, dec=2.0 deg>),\n", | |
" (2, 2.0, <Time object: scale='utc' format='iso' value=2000-01-02 00:00:00.000>, <SkyCoord (ICRS): ra=3.0 deg, dec=4.0 deg>)], \n", | |
" dtype=[('a', '<i8'), ('quant', 'O'), ('time', 'O'), ('icrs', 'O')])" | |
] | |
} | |
], | |
"prompt_number": 7 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"t.use_quantity" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 8, | |
"text": [ | |
"True" | |
] | |
} | |
], | |
"prompt_number": 8 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"try:\n", | |
" t.use_quantity = False\n", | |
"except Exception as err:\n", | |
" print(str(err))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Cannot change use_quantity attribute\n" | |
] | |
} | |
], | |
"prompt_number": 9 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print type(t)\n", | |
"print type(t['quant'])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"<class 'astropy.table.table.Table'>\n", | |
"<class 'astropy.units.quantity.Quantity'>\n" | |
] | |
} | |
], | |
"prompt_number": 10 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Formatting works!\n", | |
"t['quant'].format = \"{0.value:.5f}\"\n", | |
"t" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<table id=\"table4434221072\"><thead><tr><th>a</th><th>quant</th><th>time</th><th>icrs</th></tr></thead><thead><tr><th></th><th>m</th><th></th><th></th></tr></thead><tr><td>1</td><td>1.00000</td><td>2000-01-02 00:00:00.000</td><td><SkyCoord (ICRS): ra=1.0 deg, dec=2.0 deg></td></tr><tr><td>2</td><td>2.00000</td><td>2000-01-02 00:00:00.000</td><td><SkyCoord (ICRS): ra=3.0 deg, dec=4.0 deg></td></tr></table>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 11, | |
"text": [ | |
"<Table rows=2 names=('a','quant','time','icrs') units=(None,'m',None,None)>\n", | |
"array([ (1, 1.0, <Time object: scale='utc' format='iso' value=2000-01-02 00:00:00.000>, <SkyCoord (ICRS): ra=1.0 deg, dec=2.0 deg>),\n", | |
" (2, 2.0, <Time object: scale='utc' format='iso' value=2000-01-02 00:00:00.000>, <SkyCoord (ICRS): ra=3.0 deg, dec=4.0 deg>)], \n", | |
" dtype=[('a', '<i8'), ('quant', 'O'), ('time', 'O'), ('icrs', 'O')])" | |
] | |
} | |
], | |
"prompt_number": 11 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# t['time'] is a real Time object so format attributes work\n", | |
"t['time'].precision = 0\n", | |
"t" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<table id=\"table4434221072\"><thead><tr><th>a</th><th>quant</th><th>time</th><th>icrs</th></tr></thead><thead><tr><th></th><th>m</th><th></th><th></th></tr></thead><tr><td>1</td><td>1.00000</td><td>2000-01-02 00:00:00</td><td><SkyCoord (ICRS): ra=1.0 deg, dec=2.0 deg></td></tr><tr><td>2</td><td>2.00000</td><td>2000-01-02 00:00:00</td><td><SkyCoord (ICRS): ra=3.0 deg, dec=4.0 deg></td></tr></table>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 12, | |
"text": [ | |
"<Table rows=2 names=('a','quant','time','icrs') units=(None,'m',None,None)>\n", | |
"array([ (1, 1.0, <Time object: scale='utc' format='iso' value=2000-01-02 00:00:00>, <SkyCoord (ICRS): ra=1.0 deg, dec=2.0 deg>),\n", | |
" (2, 2.0, <Time object: scale='utc' format='iso' value=2000-01-02 00:00:00>, <SkyCoord (ICRS): ra=3.0 deg, dec=4.0 deg>)], \n", | |
" dtype=[('a', '<i8'), ('quant', 'O'), ('time', 'O'), ('icrs', 'O')])" | |
] | |
} | |
], | |
"prompt_number": 12 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Convert back and forth between standard and quantity tables" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"t2 = Table(t)\n", | |
"t2['quant']" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 13, | |
"text": [ | |
"<Column name='quant' unit=u'm' format=None description=None>\n", | |
"array([ 1., 2.])" | |
] | |
} | |
], | |
"prompt_number": 13 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"t3 = QTable(t2)\n", | |
"t3['quant']" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 14, | |
"text": [ | |
"<Quantity [ 1., 2.] m>" | |
] | |
} | |
], | |
"prompt_number": 14 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Normal row, array conversion and slicing work and show object dtype" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# The 'O' object dtype is slightly misleading, maybe show actual type,\n", | |
"# e.g. Quantity, Time, SkyCoord?\n", | |
"t[1]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 15, | |
"text": [ | |
"<Row 1 of table\n", | |
" values=(2, 2.0, <Time object: scale='utc' format='iso' value=2000-01-02 00:00:00>, <SkyCoord (ICRS): ra=3.0 deg, dec=4.0 deg>)\n", | |
" dtype=[('a', '<i8'), ('quant', 'O'), ('time', 'O'), ('icrs', 'O')]>" | |
] | |
} | |
], | |
"prompt_number": 15 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"np.array(t)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 16, | |
"text": [ | |
"array([ (1, 1.0, <Time object: scale='utc' format='iso' value=2000-01-02 00:00:00>, <SkyCoord (ICRS): ra=1.0 deg, dec=2.0 deg>),\n", | |
" (2, 2.0, <Time object: scale='utc' format='iso' value=2000-01-02 00:00:00>, <SkyCoord (ICRS): ra=3.0 deg, dec=4.0 deg>)], \n", | |
" dtype=[('a', '<i8'), ('quant', 'O'), ('time', 'O'), ('icrs', 'O')])" | |
] | |
} | |
], | |
"prompt_number": 16 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"t[:1]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<table id=\"table4435841616\"><thead><tr><th>a</th><th>quant</th><th>time</th><th>icrs</th></tr></thead><thead><tr><th></th><th>m</th><th></th><th></th></tr></thead><tr><td>1</td><td>1.0 m</td><td>2000-01-02 00:00:00</td><td><SkyCoord (ICRS): ra=1.0 deg, dec=2.0 deg></td></tr></table>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 17, | |
"text": [ | |
"<Table rows=1 names=('a','quant','time','icrs') units=(None,'m',None,None)>\n", | |
"array([ (1, 1.0, <Time object: scale='utc' format='iso' value=2000-01-02 00:00:00>, <SkyCoord (ICRS): ra=1.0 deg, dec=2.0 deg>)], \n", | |
" dtype=[('a', '<i8'), ('quant', 'O'), ('time', 'O'), ('icrs', 'O')])" | |
] | |
} | |
], | |
"prompt_number": 17 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"t[1]['quant']" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"latex": [ | |
"$2 \\; \\mathrm{m}$" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 18, | |
"text": [ | |
"<Quantity 2.0 m>" | |
] | |
} | |
], | |
"prompt_number": 18 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"str(t[1]['time'])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 19, | |
"text": [ | |
"'2000-01-02 00:00:00'" | |
] | |
} | |
], | |
"prompt_number": 19 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"t.dtype" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 20, | |
"text": [ | |
"dtype([('a', '<i8'), ('quant', 'O'), ('time', 'O'), ('icrs', 'O')])" | |
] | |
} | |
], | |
"prompt_number": 20 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Adding row not implemented yet for mixin cols" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"try:\n", | |
" t.add_row([3, 3*u.m, 3])\n", | |
"except Exception as err:\n", | |
" print(err)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Cannot add a row in table with column types ['Quantity', 'SkyCoord', 'Time']\n" | |
] | |
} | |
], | |
"prompt_number": 21 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Assignment works when it should" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"t['quant'] = [4, 2] * u.km\n", | |
"t" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<table id=\"table4434221072\"><thead><tr><th>a</th><th>quant</th><th>time</th><th>icrs</th></tr></thead><thead><tr><th></th><th>m</th><th></th><th></th></tr></thead><tr><td>1</td><td>4000.00000</td><td>2000-01-02 00:00:00</td><td><SkyCoord (ICRS): ra=1.0 deg, dec=2.0 deg></td></tr><tr><td>2</td><td>2000.00000</td><td>2000-01-02 00:00:00</td><td><SkyCoord (ICRS): ra=3.0 deg, dec=4.0 deg></td></tr></table>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 22, | |
"text": [ | |
"<Table rows=2 names=('a','quant','time','icrs') units=(None,'m',None,None)>\n", | |
"array([ (1, 4000.0, <Time object: scale='utc' format='iso' value=2000-01-02 00:00:00>, <SkyCoord (ICRS): ra=1.0 deg, dec=2.0 deg>),\n", | |
" (2, 2000.0, <Time object: scale='utc' format='iso' value=2000-01-02 00:00:00>, <SkyCoord (ICRS): ra=3.0 deg, dec=4.0 deg>)], \n", | |
" dtype=[('a', '<i8'), ('quant', 'O'), ('time', 'O'), ('icrs', 'O')])" | |
] | |
} | |
], | |
"prompt_number": 22 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Assignment to Time object is not allowed ()\n", | |
"try:\n", | |
" t['time'] = [1,2]\n", | |
"except TypeError as err:\n", | |
" print(err)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"'Time' object does not support item assignment\n" | |
] | |
} | |
], | |
"prompt_number": 23 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Copying works\n", | |
"t_copy = t.copy()\n", | |
"t_copy['quant'][0] = -100 * u.m\n", | |
"t" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<table id=\"table4434221072\"><thead><tr><th>a</th><th>quant</th><th>time</th><th>icrs</th></tr></thead><thead><tr><th></th><th>m</th><th></th><th></th></tr></thead><tr><td>1</td><td>4000.00000</td><td>2000-01-02 00:00:00</td><td><SkyCoord (ICRS): ra=1.0 deg, dec=2.0 deg></td></tr><tr><td>2</td><td>2000.00000</td><td>2000-01-02 00:00:00</td><td><SkyCoord (ICRS): ra=3.0 deg, dec=4.0 deg></td></tr></table>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 24, | |
"text": [ | |
"<Table rows=2 names=('a','quant','time','icrs') units=(None,'m',None,None)>\n", | |
"array([ (1, 4000.0, <Time object: scale='utc' format='iso' value=2000-01-02 00:00:00>, <SkyCoord (ICRS): ra=1.0 deg, dec=2.0 deg>),\n", | |
" (2, 2000.0, <Time object: scale='utc' format='iso' value=2000-01-02 00:00:00>, <SkyCoord (ICRS): ra=3.0 deg, dec=4.0 deg>)], \n", | |
" dtype=[('a', '<i8'), ('quant', 'O'), ('time', 'O'), ('icrs', 'O')])" | |
] | |
} | |
], | |
"prompt_number": 24 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Array subsetting works\n", | |
"t4 = t['a', 'quant']\n", | |
"t4" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<table id=\"table4435688464\"><thead><tr><th>a</th><th>quant</th></tr></thead><thead><tr><th></th><th>m</th></tr></thead><tr><td>1</td><td>4000.0 m</td></tr><tr><td>2</td><td>2000.0 m</td></tr></table>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 25, | |
"text": [ | |
"<Table rows=2 names=('a','quant') units=(None,'m')>\n", | |
"array([(1, 4000.0), (2, 2000.0)], \n", | |
" dtype=[('a', '<i8'), ('quant', 'O')])" | |
] | |
} | |
], | |
"prompt_number": 25 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# If a mixin column has a `name` attribute it gets used\n", | |
"q4 = [1, 2] * u.km / u.s\n", | |
"q4.name = 'xx'\n", | |
"t4.add_column(q4)\n", | |
"t4" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<table id=\"table4435688464\"><thead><tr><th>a</th><th>quant</th><th>xx</th></tr></thead><thead><tr><th></th><th>m</th><th>km / s</th></tr></thead><tr><td>1</td><td>4000.0 m</td><td>1.0 km / s</td></tr><tr><td>2</td><td>2000.0 m</td><td>2.0 km / s</td></tr></table>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 26, | |
"text": [ | |
"<Table rows=2 names=('a','quant','xx') units=(None,'m','km / s')>\n", | |
"array([(1, 4000.0, 1.0), (2, 2000.0, 2.0)], \n", | |
" dtype=[('a', '<i8'), ('quant', 'O'), ('xx', 'O')])" | |
] | |
} | |
], | |
"prompt_number": 26 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Trivial mixin column" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class MixinList(list):\n", | |
" _astropy_table_compatible = True" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 27 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"m = MixinList([10, 20])\n", | |
"m.name = 'mixinlist'\n", | |
"m.format = '05d'" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 28 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"t4.add_column(m)\n", | |
"t4" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<table id=\"table4435688464\"><thead><tr><th>a</th><th>quant</th><th>xx</th><th>mixinlist</th></tr></thead><thead><tr><th></th><th>m</th><th>km / s</th><th></th></tr></thead><tr><td>1</td><td>4000.0 m</td><td>1.0 km / s</td><td>00010</td></tr><tr><td>2</td><td>2000.0 m</td><td>2.0 km / s</td><td>00020</td></tr></table>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 29, | |
"text": [ | |
"<Table rows=2 names=('a','quant','xx','mixinlist') units=(None,'m','km / s',None)>\n", | |
"array([(1, 4000.0, 1.0, 10), (2, 2000.0, 2.0, 20)], \n", | |
" dtype=[('a', '<i8'), ('quant', 'O'), ('xx', 'O'), ('mixinlist', 'O')])" | |
] | |
} | |
], | |
"prompt_number": 29 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"type(t4['mixinlist'])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 30, | |
"text": [ | |
"__main__.MixinList" | |
] | |
} | |
], | |
"prompt_number": 30 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Pandas!" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from pandas import DataFrame" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 31 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"df = DataFrame([[1,2], [3,4]], columns=('c', 'd'))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 32 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"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 style=\"text-align: right;\">\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> 1</td>\n", | |
" <td> 2</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td> 3</td>\n", | |
" <td> 4</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 33, | |
"text": [ | |
" c d\n", | |
"0 1 2\n", | |
"1 3 4" | |
] | |
} | |
], | |
"prompt_number": 33 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"df['c']._astropy_table_compatible = True" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 34 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"t4['c'] = df['c']" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 35 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"t4" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<table id=\"table4435688464\"><thead><tr><th>a</th><th>quant</th><th>xx</th><th>mixinlist</th><th>c</th></tr></thead><thead><tr><th></th><th>m</th><th>km / s</th><th></th><th></th></tr></thead><tr><td>1</td><td>4000.0 m</td><td>1.0 km / s</td><td>00010</td><td>1</td></tr><tr><td>2</td><td>2000.0 m</td><td>2.0 km / s</td><td>00020</td><td>3</td></tr></table>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 36, | |
"text": [ | |
"<Table rows=2 names=('a','quant','xx','mixinlist','c') units=(None,'m','km / s',None,None)>\n", | |
"array([(1, 4000.0, 1.0, 10, 1), (2, 2000.0, 2.0, 20, 3)], \n", | |
" dtype=[('a', '<i8'), ('quant', 'O'), ('xx', 'O'), ('mixinlist', 'O'), ('c', 'O')])" | |
] | |
} | |
], | |
"prompt_number": 36 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"type(t4['c'])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 37, | |
"text": [ | |
"pandas.core.series.Series" | |
] | |
} | |
], | |
"prompt_number": 37 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### ToDo\n", | |
"\n", | |
"- High level operations won't work properly because they are implemented via numpy structured arrays\n", | |
"- Need to figure out what to do about masked tables" | |
] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment