Last active
August 29, 2015 14:01
-
-
Save shoyer/94936e5b71613683d95a to your computer and use it in GitHub Desktop.
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": { | |
"name": "", | |
"signature": "sha256:246e288aef59cc17b63f35b68b7ed204b0b9bd5a86620aa6b857a56e8d6ef2d1" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "heading", | |
"level": 1, | |
"metadata": {}, | |
"source": [ | |
"Changes to `DataArray.__repr__`" | |
] | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 4, | |
"metadata": {}, | |
"source": [ | |
"Setup the example from the tutorial:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import numpy as np\n", | |
"import pandas as pd\n", | |
"import xray" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"foo_values = np.random.RandomState(0).rand(3, 4)\n", | |
"times = pd.date_range('2000-01-01', periods=3)\n", | |
"\n", | |
"ds = xray.Dataset({'time': ('time', times),\n", | |
" 'foo': (['time', 'space'], foo_values)})\n", | |
"ds['numbers'] = ('space', [10, 10, 20, 20])\n", | |
"ds['abc'] = ('time', ['A', 'B', 'C'])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 48 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 4, | |
"metadata": {}, | |
"source": [ | |
"Some DataArray representations:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"ds['time']" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 49, | |
"text": [ | |
"<xray.DataArray 'time' (time: 3)>\n", | |
"array(['1999-12-31T16:00:00.000000000-0800',\n", | |
" '2000-01-01T16:00:00.000000000-0800',\n", | |
" '2000-01-02T16:00:00.000000000-0800'], dtype='datetime64[ns]')\n", | |
"Coordinates:\n", | |
" time: <class 'pandas.tseries.index.DatetimeIndex'>\n", | |
" [2000-01-01, ..., 2000-01-03]\n", | |
" Length: 3, Freq: D, Timezone: None\n", | |
"Linked dataset variables:\n", | |
" foo, space, numbers, abc" | |
] | |
} | |
], | |
"prompt_number": 49 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"ds['space']" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 50, | |
"text": [ | |
"<xray.DataArray 'space' (space: 4)>\n", | |
"array([0, 1, 2, 3])\n", | |
"Coordinates:\n", | |
" space: Int64Index([0, 1, 2, 3], dtype='int64')\n", | |
"Linked dataset variables:\n", | |
" foo, time, numbers, abc" | |
] | |
} | |
], | |
"prompt_number": 50 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"ds['foo']" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 51, | |
"text": [ | |
"<xray.DataArray 'foo' (time: 3, space: 4)>\n", | |
"array([[ 0.5488135 , 0.71518937, 0.60276338, 0.54488318],\n", | |
" [ 0.4236548 , 0.64589411, 0.43758721, 0.891773 ],\n", | |
" [ 0.96366276, 0.38344152, 0.79172504, 0.52889492]])\n", | |
"Coordinates:\n", | |
" time: <class 'pandas.tseries.index.DatetimeIndex'>\n", | |
" [2000-01-01, ..., 2000-01-03]\n", | |
" Length: 3, Freq: D, Timezone: None\n", | |
" space: Int64Index([0, 1, 2, 3], dtype='int64')\n", | |
"Linked dataset variables:\n", | |
" numbers, abc" | |
] | |
} | |
], | |
"prompt_number": 51 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"ds['foo'].attrs['units'] = 'kelvin'" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 52 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"ds['foo']" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 53, | |
"text": [ | |
"<xray.DataArray 'foo' (time: 3, space: 4)>\n", | |
"array([[ 0.5488135 , 0.71518937, 0.60276338, 0.54488318],\n", | |
" [ 0.4236548 , 0.64589411, 0.43758721, 0.891773 ],\n", | |
" [ 0.96366276, 0.38344152, 0.79172504, 0.52889492]])\n", | |
"Coordinates:\n", | |
" time: <class 'pandas.tseries.index.DatetimeIndex'>\n", | |
" [2000-01-01, ..., 2000-01-03]\n", | |
" Length: 3, Freq: D, Timezone: None\n", | |
" space: Int64Index([0, 1, 2, 3], dtype='int64')\n", | |
"Linked dataset variables:\n", | |
" numbers, abc\n", | |
"Attributes:\n", | |
" units: kelvin" | |
] | |
} | |
], | |
"prompt_number": 53 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"ds['foo'][0, 0]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 54, | |
"text": [ | |
"<xray.DataArray 'foo' ()>\n", | |
"array(0.5488135039273248)\n", | |
"Linked dataset variables:\n", | |
" time, space, numbers, abc\n", | |
"Attributes:\n", | |
" units: kelvin" | |
] | |
} | |
], | |
"prompt_number": 54 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 4, | |
"metadata": {}, | |
"source": [ | |
"Because \"numbers\" is a linked variable, this works:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"ds['foo']['numbers']" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 56, | |
"text": [ | |
"<xray.DataArray 'numbers' (space: 4)>\n", | |
"array([10, 10, 20, 20])\n", | |
"Coordinates:\n", | |
" space: Int64Index([0, 1, 2, 3], dtype='int64')\n", | |
"Linked dataset variables:\n", | |
" foo, time, abc" | |
] | |
} | |
], | |
"prompt_number": 56 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 4, | |
"metadata": {}, | |
"source": [ | |
"Ultimately, this is because data arrays always keeps track of an associated dataset:" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"In this case it's the original dataset `ds`." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"ds['foo'].dataset" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 57, | |
"text": [ | |
"<xray.Dataset>\n", | |
"Dimensions: (space: 4, time: 3)\n", | |
"Coordinates:\n", | |
" space X \n", | |
" time X \n", | |
"Noncoordinates:\n", | |
" foo 1 0 \n", | |
" numbers 0 \n", | |
" abc 0 " | |
] | |
} | |
], | |
"prompt_number": 57 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 4, | |
"metadata": {}, | |
"source": [ | |
"Calling `.select()` removes these linked variables:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"foo_selected = ds['foo'].select()\n", | |
"foo_selected" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 63, | |
"text": [ | |
"<xray.DataArray 'foo' (time: 3, space: 4)>\n", | |
"array([[ 0.5488135 , 0.71518937, 0.60276338, 0.54488318],\n", | |
" [ 0.4236548 , 0.64589411, 0.43758721, 0.891773 ],\n", | |
" [ 0.96366276, 0.38344152, 0.79172504, 0.52889492]])\n", | |
"Coordinates:\n", | |
" time: <class 'pandas.tseries.index.DatetimeIndex'>\n", | |
" [2000-01-01, ..., 2000-01-03]\n", | |
" Length: 3, Freq: D, Timezone: None\n", | |
" space: Int64Index([0, 1, 2, 3], dtype='int64')" | |
] | |
} | |
], | |
"prompt_number": 63 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 4, | |
"metadata": {}, | |
"source": [ | |
"A few more examples:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"foo_selected.attrs = {}\n", | |
"foo_selected" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 64, | |
"text": [ | |
"<xray.DataArray 'foo' (time: 3, space: 4)>\n", | |
"array([[ 0.5488135 , 0.71518937, 0.60276338, 0.54488318],\n", | |
" [ 0.4236548 , 0.64589411, 0.43758721, 0.891773 ],\n", | |
" [ 0.96366276, 0.38344152, 0.79172504, 0.52889492]])\n", | |
"Coordinates:\n", | |
" time: <class 'pandas.tseries.index.DatetimeIndex'>\n", | |
" [2000-01-01, ..., 2000-01-03]\n", | |
" Length: 3, Freq: D, Timezone: None\n", | |
" space: Int64Index([0, 1, 2, 3], dtype='int64')" | |
] | |
} | |
], | |
"prompt_number": 64 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"foo_selected[0, 0]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 65, | |
"text": [ | |
"<xray.DataArray 'foo' ()>\n", | |
"array(0.5488135039273248)\n", | |
"Linked dataset variables:\n", | |
" time, space" | |
] | |
} | |
], | |
"prompt_number": 65 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"foo_selected[0, 0].select()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 66, | |
"text": [ | |
"<xray.DataArray 'foo' ()>\n", | |
"array(0.5488135039273248)" | |
] | |
} | |
], | |
"prompt_number": 66 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment