{ "metadata": { "name": "", "signature": "sha256:cec8b0fbd38df9f351277c944929b461623f8c0563e7b83b42bb32f769841bfa" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Standard data types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The python standard library supports many data types. The ones you find most important will depend on the data you work with most often. Nevertheless, most introductory Python texts list the primary standard data types as **numbers, strings, lists, tuples and dictionaries**. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are several standard numerical data types. The ones I encounter most often are floats and integers:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x_int = 20 #this is an integer\n", "x_float = 20.0 #this is a floating point number" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you are using Python 2.x, the important thing to note is that arthimetic operations involving only integers will produce integers:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 5\n", "y = 3\n", "z = x/y\n", "print z" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "If one of the numbers involved in the operation is a float then the result is a float:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "z = 10/3.\n", "print z" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3.33333333333\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ " In Python 3, `/` represents floating point division. You can import this behavior into python 2.x, by doing:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from __future__ import division\n", "\n", "print 5/2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2.5\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Other numerical data types are complex numbers and long integers." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings may be defined using single, double or triple quotes. In most cases, you can use single and double quotes interchangeably:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "string1 = 'datatypes'\n", "string2 = \"Don't stop believin'\"\n", "print string1\n", "print string2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "datatypes\n", "Don't stop believin'\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can access parts of a string by using the slicing syntax:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print string1[0] #first character\n", "print string1[0:2] #first two characters\n", "print string1[2:len(string1)] #everything after second character\n", "print string1[2:5]\n", "print string1[-2:] #last two characters\n", "print string1[0:len(string1)]\n", "print string1[:]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "d\n", "da\n", "tatypes\n", "tat\n", "es\n", "datatypes\n", "datatypes\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Triple quotes are normally reserved for text spanning several lines and convenient for writing doc strings:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def my_function():\n", " \"\"\" This a simple function to demonstrate\n", " the use of triple quotes. You would normally\n", " put some a brief description of your function in this space.\"\"\"\n", " pass #Tells interpreter: nothing to see here, carry on." ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The important thing to note with strings is that they are **immutable**. That is, you can't change any character of a string after it's created:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 'asasa'\n", "#a[2] = 'd' #throws error" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A Python list is exactly what you think it is; it is a list or sequence of variables. A list Python list is kinda like a MATLAB cell array. Here are some lists:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "num_list = [2, 3, 210, 4]\n", "str_list = ['a', 'sdff', 'asd']\n", "rand_list = [[12, 10], 'a', 1, [10, 'x', 12]]\n", "\n", "print \"num_list: %s\" %num_list\n", "print \"str_list: %s\" %str_list\n", "print \"rand_list: %s\" %rand_list" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "num_list: [2, 3, 210, 4]\n", "str_list: ['a', 'sdff', 'asd']\n", "rand_list: [[12, 10], 'a', 1, [10, 'x', 12]]\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can slice into list the same you would a string. To access a list within a list, you would use an extra set of brackets:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print rand_list[0][1]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10\n" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Unlike strings, lists are mutable." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### List comprehensions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[List comprehension](https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions) is a convenient and powerful short hand for generating lists. Here are some examples:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#generate a list that contains the square of each number in num_list:\n", "num_list_sq = [num**2 for num in num_list ]\n", "print num_list_sq\n", "\n", "#generate a list that contains the square of each number in num_list\n", "#if the number is exactly divisible by 3\n", "num_list_sq2 = [num**2 if num%3==0 else num for num in num_list ]\n", "print num_list_sq2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[4, 9, 44100, 16]\n", "[2, 9, 44100, 4]\n" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The last list comprehensions is basically a short hand for the code below:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "num_list_sq3 = []\n", "for num in num_list:\n", " if num%3==0:\n", " num_list_sq3.append(num**2)\n", " else: \n", " num_list_sq3.append(num)\n", "\n", "print num_list_sq3" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[2, 9, 44100, 4]\n" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tuples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tuples are like lists except that they are immutable. Here is a tuple:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "yr_strs = ('year1', 'year2', 'year3')\n", "yr_nums = 2009, 2010, 2011\n", "tupl = ('a',)\n", "\n", "tup_num = tuple(num_list)\n", "print yr_nums\n", "print tup_num" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(2009, 2010, 2011)\n", "(2, 3, 210, 4)\n" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tuples are useful as argument specifiers:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"%s: %s\" %(yr_strs[0], yr_nums[1])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "year1: 2010\n" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dictionaries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dictionaries are **mutable** containers for Python objects. Dictionaries consist of `keys` and their contents. Here is a simple dictionary:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "contacts = {'John': 6462100, 'Adam': 6461870}" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "print contacts" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'John': 6462100, 'Adam': 6461870}\n" ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dictionaries are a good way to organize data:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "mooring1 = {'Date': '2010-09-13', 'Lat': 10.1,'Lon': 78.5, 'salinity': [28.8, 31.3, 34.5, 35.1]}\n", "print mooring1['Lon']" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "78.5\n" ] } ], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternatively, one may use the `dict` function:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "dict_ex = dict(zip(['a','b', 'c', 'd', 'e'], range(5)))\n", "print dict_ex" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'a': 0, 'c': 2, 'b': 1, 'e': 4, 'd': 3}\n" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "print dict_ex.keys()\n", "print dict_ex.values()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['a', 'c', 'b', 'e', 'd']\n", "[0, 2, 1, 4, 3]\n" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "dict_ex['a']" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 20, "text": [ "0" ] } ], "prompt_number": 20 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python dictionaries are similar to MATLAB structs. In fact, when you import a MATLAB structure into Python, the matlab structs are converted to Python dictionaries." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# NumPy datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### N-dimensional arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The fundamental NumPy data type is the ndarray:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "\n", "x = np.array([12, 10, 3])\n", "print x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[12 10 3]\n" ] } ], "prompt_number": 21 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here are some ways to create numpy arrays:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = np.arange(5,10,0.5) #note: endpoint is excluded\n", "print a" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[ 5. 5.5 6. 6.5 7. 7.5 8. 8.5 9. 9.5]\n" ] } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "print np.linspace(0,20,6) #note: endpoint is included" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[ 0. 4. 8. 12. 16. 20.]\n" ] } ], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "b = np.zeros((3,3))\n", "print b" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[ 0. 0. 0.]\n", " [ 0. 0. 0.]\n", " [ 0. 0. 0.]]\n" ] } ], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "ones3d = np.ones((2,5,2))\n", "print ones3d" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[[ 1. 1.]\n", " [ 1. 1.]\n", " [ 1. 1.]\n", " [ 1. 1.]\n", " [ 1. 1.]]\n", "\n", " [[ 1. 1.]\n", " [ 1. 1.]\n", " [ 1. 1.]\n", " [ 1. 1.]\n", " [ 1. 1.]]]\n" ] } ], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "rand_array = np.random.randint(3,10, (4,5))\n", "print rand_array\n", "print rand_array.shape" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[6 9 7 9 7]\n", " [6 4 4 3 4]\n", " [8 5 3 3 3]\n", " [4 9 3 9 4]]\n", "(4, 5)\n" ] } ], "prompt_number": 26 }, { "cell_type": "code", "collapsed": false, "input": [ "a = np.arange(10)\n", "b = np.ones((10,1))\n", "print a*b.T" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]]\n" ] } ], "prompt_number": 27 }, { "cell_type": "markdown", "metadata": {}, "source": [ "*comment on 1d vs 2d \"vectors\"*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Masked arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another important numpy data type is the masked array. This is useful when working with array that contain \"bad\" data. Here is how you would create a masked array:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = np.arange(10)\n", "print x\n", "x_ma = np.ma.masked_where(x>5, x)\n", "print x_ma\n", "print x_ma.mask" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[0 1 2 3 4 5 6 7 8 9]\n", "[0 1 2 3 4 5 -- -- -- --]\n", "[False False False False False False True True True True]\n" ] } ], "prompt_number": 28 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Masked arrays arrays are more convenient that using nans because all the standard numpy operations automatically ignore masked values:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x_ma_mean = np.mean(x_ma)\n", "print x_ma_mean" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2.5\n" ] } ], "prompt_number": 29 }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you were using nans, you would need to use the nanmean function to ignore the nans:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "y = np.array([1, 2, 3, np.nan, 5])\n", "print np.mean(y)\n", "print np.nanmean(y)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "nan\n", "2.75\n" ] } ], "prompt_number": 30 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is how you would convert an array that has Nans into a masked array:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "y_ma = np.ma.masked_where(np.isnan(y), y, copy=True)\n", "y_ma2 = np.ma.masked_invalid(y)\n", "print y_ma.mean()\n", "y[2] = 10 #if copy is set to False, then this modifies y_ma as well\n", "print y\n", "print y_ma\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2.75\n", "[ 1. 2. 10. nan 5.]\n", "[1.0 2.0 3.0 -- 5.0]\n" ] } ], "prompt_number": 31 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Structured arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Structured arrays](http://docs.scipy.org/doc/numpy/user/basics.rec.html) are essentially numpy arrays that allow you to store data under field names rather than numerical indices. These are convenient for storing a set of data array that are of the same length or have a common axis (e.g. time, depth, etc.). Structured arrays are defined using a `dtype` object. There are a [several ways](http://docs.scipy.org/doc/numpy/user/basics.rec.html#defining-structured-arrays) to this. In the example below, I define the dtype object using the list method. The list consists of tuples and each tuple specifies a field name, datatype and shape (optional)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Simple example:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "dtype_list1 = [('x','f4'), ('y','f4'), ('z','f4')]\n", "coords = np.zeros((5,1), dtype=dtype_list1)\n", "print coords" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[(0.0, 0.0, 0.0)]\n", " [(0.0, 0.0, 0.0)]\n", " [(0.0, 0.0, 0.0)]\n", " [(0.0, 0.0, 0.0)]\n", " [(0.0, 0.0, 0.0)]]\n" ] } ], "prompt_number": 32 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another example:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "dtype_list2 = [('temp','f4'), ('sal','f4'), ('lon','f4'), ('lat','f4')]\n", "gridded_data = np.zeros((4,5), dtype=dtype_list2)\n", "print gridded_data" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[(0.0, 0.0, 0.0, 0.0) (0.0, 0.0, 0.0, 0.0) (0.0, 0.0, 0.0, 0.0)\n", " (0.0, 0.0, 0.0, 0.0) (0.0, 0.0, 0.0, 0.0)]\n", " [(0.0, 0.0, 0.0, 0.0) (0.0, 0.0, 0.0, 0.0) (0.0, 0.0, 0.0, 0.0)\n", " (0.0, 0.0, 0.0, 0.0) (0.0, 0.0, 0.0, 0.0)]\n", " [(0.0, 0.0, 0.0, 0.0) (0.0, 0.0, 0.0, 0.0) (0.0, 0.0, 0.0, 0.0)\n", " (0.0, 0.0, 0.0, 0.0) (0.0, 0.0, 0.0, 0.0)]\n", " [(0.0, 0.0, 0.0, 0.0) (0.0, 0.0, 0.0, 0.0) (0.0, 0.0, 0.0, 0.0)\n", " (0.0, 0.0, 0.0, 0.0) (0.0, 0.0, 0.0, 0.0)]]\n" ] } ], "prompt_number": 33 }, { "cell_type": "markdown", "metadata": {}, "source": [ "With the above lines, I created a 4x5 structured array of zeros that has three fields: `temp`, `sal` and `lonlat`. Each field is preallocated to store 32-bit floating point numbers, specified by the `f4` parameter. Each field is a scalar by default, but they could be n-d arrays. Let's fill the structed array with some arbitrary values:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "gridded_data['temp'] = np.random.randint(20,30,(4,5))\n", "gridded_data['sal'] = np.random.randint(30,35,(4,5))\n", "lon = np.linspace(0,20,4)\n", "lat = np.linspace(10,30,5)\n", "lon,lat = np.meshgrid(lat,lon)\n", "gridded_data['lon'] = np.round(lon) \n", "gridded_data['lat'] = np.round(lat)\n", "print gridded_data" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[(24.0, 30.0, 10.0, 0.0) (29.0, 34.0, 15.0, 0.0) (22.0, 33.0, 20.0, 0.0)\n", " (28.0, 34.0, 25.0, 0.0) (22.0, 33.0, 30.0, 0.0)]\n", " [(29.0, 31.0, 10.0, 7.0) (27.0, 30.0, 15.0, 7.0) (20.0, 30.0, 20.0, 7.0)\n", " (28.0, 31.0, 25.0, 7.0) (27.0, 31.0, 30.0, 7.0)]\n", " [(20.0, 33.0, 10.0, 13.0) (24.0, 32.0, 15.0, 13.0)\n", " (24.0, 33.0, 20.0, 13.0) (20.0, 34.0, 25.0, 13.0)\n", " (26.0, 33.0, 30.0, 13.0)]\n", " [(20.0, 33.0, 10.0, 20.0) (29.0, 30.0, 15.0, 20.0)\n", " (27.0, 31.0, 20.0, 20.0) (24.0, 31.0, 25.0, 20.0)\n", " (23.0, 30.0, 30.0, 20.0)]]\n" ] } ], "prompt_number": 34 }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can access a structured array either by using field names, indices or both:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print gridded_data['sal'] " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[ 30. 34. 33. 34. 33.]\n", " [ 31. 30. 30. 31. 31.]\n", " [ 33. 32. 33. 34. 33.]\n", " [ 33. 30. 31. 31. 30.]]\n" ] } ], "prompt_number": 35 }, { "cell_type": "code", "collapsed": false, "input": [ "print gridded_data['lon']" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[ 10. 15. 20. 25. 30.]\n", " [ 10. 15. 20. 25. 30.]\n", " [ 10. 15. 20. 25. 30.]\n", " [ 10. 15. 20. 25. 30.]]\n" ] } ], "prompt_number": 36 }, { "cell_type": "code", "collapsed": false, "input": [ "print gridded_data[['temp', 'lat']]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[(24.0, 0.0) (29.0, 0.0) (22.0, 0.0) (28.0, 0.0) (22.0, 0.0)]\n", " [(29.0, 7.0) (27.0, 7.0) (20.0, 7.0) (28.0, 7.0) (27.0, 7.0)]\n", " [(20.0, 13.0) (24.0, 13.0) (24.0, 13.0) (20.0, 13.0) (26.0, 13.0)]\n", " [(20.0, 20.0) (29.0, 20.0) (27.0, 20.0) (24.0, 20.0) (23.0, 20.0)]]\n" ] } ], "prompt_number": 37 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Also note the shape of each field:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "gridded_data.shape" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 38, "text": [ "(4, 5)" ] } ], "prompt_number": 38 }, { "cell_type": "code", "collapsed": false, "input": [ "np.mean(gridded_data['temp'], axis=0)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 39, "text": [ "array([ 23.25, 27.25, 23.25, 25. , 24.5 ], dtype=float32)" ] } ], "prompt_number": 39 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 39 }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Miscellany" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Datetime objects " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The [datetime](https://docs.python.org/2/library/datetime.html) module provides functions and classes for working dates and times. There is the standard, built-in Python version and NumPy version called [datetime64](http://docs.scipy.org/doc/numpy/reference/arrays.datetime.html)." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 39 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 39 } ], "metadata": {} } ] }