{ "metadata": { "name": "", "signature": "sha256:96746f6bc03e6fec9c102fd49c4dffc99ad3edefff7266618321c39d3d020abb" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\"Blaze\n", "\n", "# Getting Started with Blaze\n", "\n", "* Full tutorial available at http://github.com/ContinuumIO/blaze-tutorial\n", "* Install software with `conda install -c blaze blaze`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Storing results with `into`\n", "\n", "We just played with some interesting queries on baseball statistics" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from blaze import Data, into, by, join\n", "db = Data('sqlite:///data/lahman2013.sqlite')\n", "joined = join(db.Salaries, db.Teams)\n", "result = by(joined[['name', 'yearID']], avg=joined.salary.mean())\n", "result" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How do we now store this result or use it with other libraries?\n", "\n", "The result itself is a Blaze expression, not terribly useful if we're not using Blaze." ] }, { "cell_type": "code", "collapsed": false, "input": [ "type(result)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Use normal Python collections like `list` or `np.array`\n", "\n", "Blaze follows normal conventions and so can be converted by standard constructors" ] }, { "cell_type": "code", "collapsed": false, "input": [ "list(result)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "np.array(result)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Use `into`\n", "\n", "Alternatively, Blaze has registered itself into the `into` project and so can migrate its results to any of those formats." ] }, { "cell_type": "code", "collapsed": false, "input": [ "into('salaries.csv', result)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "!head salaries.csv" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exercise\n", "\n", "Dump `results` into the following formats" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Dump results into a Python set\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Dump results into a Pandas DataFrame\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Dump results into a JSON file, inspect the file to make sure that it came out ok\n" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }