# Guide NumPy 1.5 Beginners Guide

However, nothing beats spending time with his brilliant 10 year old son Zsombor for him. Nikolay Karelin holds a PhD degree in optics and used various methods of numerical simulations and analysis for nearly 20 years, first in academia and then in the industry simulation of fiber optics communication links. After initial learning curve with Python and NumPy, these excellent tools became his main choice for almost all numerical analysis and scripting, since past five years.

You might want to visit www. You can upgrade to the eBook version at www. Get in touch with us at for more details. At www. This action might not be possible to undo. Are you sure you want to continue? Upload Sign In Join. Home Books Technology. Save For Later. Create a List. Summary In Detail NumPy is an extension to, and the fundamental package for scientific computing with Python.

Approach The book is written in beginner's guide style with each aspect of NumPy demonstrated with real world examples and required screenshots. Who this book is for If you are a programmer, scientist, or engineer who has basic Python knowledge and would like to be able to do numerical computations with Python, this book is for you. Read on the Scribd mobile app Download the free Scribd mobile app to read anytime, anywhere. History Why use NumPy? Building from source Arrays Time for action — adding vectors What just happened?

Pop quiz Functioning of the arange function Have a go hero — continue the analysis IPython—an interactive shell Online resources and help Summary 2. Pop quiz — the shape of ndarray Have a go hero — create a three-by-three matrix Selecting elements NumPy numerical types Data type objects Character codes dtype constructors dtype attributes Time for action — creating a record data type What just happened? One-dimensional slicing and indexing Time for action — slicing and indexing multidimensional arrays What just happened?

Time for action — manipulating array shapes What just happened? Stacking Time for action — stacking arrays What just happened? Splitting Time for action — splitting arrays What just happened? Array attributes Time for action — converting arrays What just happened? Summary 3. Volume-weighted average price Time for action — calculating volume-weighted average price What just happened? The mean function Time-weighted average price Pop quiz — computing the weighted average Have a go hero — calculating other averages Value range Time for action — finding highest and lowest values What just happened?

Statistics Time for action — doing simple statistics What just happened? Stock returns Time for action — analyzing stock returns What just happened? Dates Time for action — dealing with dates What just happened? Have a go hero — improving the code Average true range Time for action — calculating the average true range What just happened?

Have a go hero — taking the minimum function for a spin Simple moving average Time for action — computing the simple moving average What just happened? Exponential moving average Time for action — calculating the exponential moving average What just happened? Bollinger bands Time for action — enveloping with Bollinger bands What just happened?

Have a go hero — switching to exponential moving average Linear model Time for action — predicting price with a linear model What just happened? Trend lines Time for action — drawing trend lines What just happened? Methods of ndarray Time for action — clipping and compressing arrays What just happened? Factorial Time for action — calculating the factorial What just happened?

Summary 4. Pop quiz — calculating covariance Polynomials Time for action — fitting to polynomials What just happened? Have a go hero — improving the fit On-balance volume Time for action — balancing volume What just happened? Simulation Time for action — avoiding loops with vectorize What just happened?

Have a go hero — analyzing consecutive wins and losses Smoothing Time for action — smoothing with the hanning function What just happened? Have a go hero — smoothing variations Summary 5. Working with Matrices and ufuncs Matrices Time for action — creating matrices What just happened? Creating a matrix from other matrices Time for action — creating a matrix from other matrices What just happened?

Pop quiz — defining a matrix with a string Universal functions Time for action — creating universal function What just happened? Universal function methods Time for action — applying the ufunc methods on add What just happened? Arithmetic functions Time for action — dividing arrays What just happened? Fibonacci numbers Time for action — computing Fibonacci numbers What just happened?

Have a go hero — timing the calculations Lissajous curves Time for action — drawing Lissajous curves What just happened? Square waves Time for action — drawing a square wave What just happened? Have a go hero — getting rid of the loop Sawtooth and triangle waves Time for action — drawing sawtooth and triangle waves What just happened? Have a go hero — getting rid of the loop Bitwise and comparison functions Time for action — twiddling bits What just happened? Summary 6. Search Blogs. Mark Forums Read. User Name. Remember Me? View Public Profile. View Review Entries. View HCL Entries. Find More Posts by LXer.

Posting Rules. Similar Threads. Robert King. LXer: Beginner's Guide to Nmap. Main Menu. Without this switch, we would have to import every package we need ourselves. IPython 0. Welcome to pylab, a matplotlib-based Python environment. For more information, type 'help pylab '. We might want to be able to go back to our experiments. In IPython, it is easy to save a session for later.

Current session state plus future input saved. Typing quit at the ipdb prompt exits the debugger. Profiler' This gives us a bit more insight in the workings of our program. In addition, we can now identify performance bottlenecks. Online resources and help When we are in IPython's pylab mode, we can open manual pages for NumPy functions with the help command. It is not necessary to know the name of a function. We can type a few characters and then let tab completion do its work. Let's, for instance, browse the available information for the arange function.

In [3]: arange? The popular Stack Overflow software development forum has hundreds of questions tagged "numpy". If you are really stuck with a problem or you want to be kept informed of NumPy development, you can subscribe to the NumPy discussion mailing list. Most importantly, developers actively involved with NumPy also answer questions asked on the discussion group. There are at least 50 members on the scipy channel at all times. Summary In this chapter, we installed NumPy. We got a vector addition program working and convinced ourselves that NumPy has superior performance.

We were introduced to the IPython interactive shell. In addition, we explored the available NumPy documentation and online resources. In the next chapter, we will take a look under the hood and explore some fundamental concepts including arrays and data types. The code snippets in this chapter show input and output from several IPython sessions. Recall that IPython was introduced in the previous chapter as the interactive Python shell of choice for scientific computing.

The advantages of IPython are pylab switch of many scientific computing Python packages, including NumPy, and the fact that it is not necessary to explicitly call the print function to display variable values. However, the source code delivered alongside the book is regular Python code that uses imports and print statements. It consists of two parts: 1. The actual data 2. Some metadata describing the data The majority of array operations leave the raw data untouched. The only aspect that changes is the metadata.

We have already learned, in the previous chapter, how to create an array using the arange function. Actually, we created a one-dimensional array that contained a set of numbers. The NumPy array is homogeneous—the items in the array have to be of the same type. The advantage is that, if we know that the items in the array are of the same type, then it is easy to determine the storage size required for the array.

NumPy arrays are indexed just like in Python, starting from 0. Data types are represented by special objects. These objects will be discussed comprehensively in this chapter. We will create an array with the arange function again. In both cases, we are dealing with integers bit or bit. Besides the data type of an array, it is important to know its shape. The following diagram will give us a better understanding of a NumPy array object: Dtype ndarray Shape The example in Chapter 1, NumPy Quick Start, demonstrated how to create a vector actually, a one-dimensional NumPy array.

A vector is commonly used in mathematics but, most of the time, we need higher-dimensional objects. Let's determine the shape of the vector we created a few minutes ago: In [4]: a Out[4]: array [0, 1, 2, 3, 4] In: a. The shape attribute of the array is a tuple, in this case a tuple of 1 element, which contains the length in each dimension. Time for action — creating a multidimensional array Now that we know how to create a vector, we are ready to create a multidimensional NumPy array.

After we create the matrix, we would again want to display its shape and data type. Create a multidimensional array.

We created a 2-by-2 array with the arange function we have come to trust and love. Without any warning, the array function appeared on the stage. The array function creates an array from an object that you give to it. The object needs to be array-like, for instance, a Python list. In the preceding example, we passed in a list of arrays.

The object is the only required argument of the array function. NumPy functions tend to have a lot of optional arguments with predefined defaults. Pop quiz — the shape of ndarray 1. How is the shape of an ndarray stored? It is stored in a comma-separated string. It is stored in a list. It is stored in a tuple. Give it a go and check whether the array shape is as expected. Selecting elements From time to time, we will want to select a particular element of an array. We will now select each item of the matrix one-by-one. Remember, the indices are numbered starting from 0. For the array a, we just use the notation a[m,n] , where m and n are the indices of the item in the array.

In practice, we need even more types with varying precision and, therefore, different memory size of the type. The majority of the NumPy numerical types end with a number. This number indicates the number of bits associated with the type. Trying to do that triggers a TypeError. This is shown as follows: In: int By the way, the. See the following code: In: float Once again, arrays have a data type.

To be precise, every element in a NumPy array has the same data type.

## Book review: NumPy Beginner’s Guide – Stefan Scherfke

The data type object can tell you the size of the data in bytes. The size in bytes is given by the itemsize attribute of the dtype class: In: a. Numeric is the predecessor of NumPy. Their use is not recommended, but the codes are provided here because they pop up in several places. You should instead use dtype objects. The first character signifies the type; the second character is a number specifying the number of bytes in the type: In: dtype 'f8' Out: dtype 'float64' [ 31 ] Beginning with NumPy Fundamentals A listing of all full data type names can be found in sctypeDict.

It starts with a character representing endianness, if appropriate, then a character code, followed by a number corresponding to the number of bytes that each array item requires. Endianness, here, means the way bytes are ordered within a 32 or bit word. In big-endian order, the most significant byte is stored first. In little-endian order, the least significant byte is stored first. In: t. To give an example of a record data type, we will create a record for a shop inventory. The record contains the name of the item, a character string, the number of items in the store represented by a bit integer and, finally, a price represented by a bit float.

The following steps show how to create a record data type: [ 32 ] Chapter 2 1. View the type we can view the type of a field as well : In: t['name'] Out: dtype ' S40' If you don't give the array function a data type, it will assume that it is dealing with floating point numbers. We created a record data type, which is a heterogeneous data type. The record contained a name as a character string, a number as an integer and a price represented by a float.

One-dimensional slicing and indexing Slicing of one-dimensional NumPy arrays works just like slicing of Python lists. For convenience, we refer to many dimensions at once, with an ellipsis. We can visualize this as a two-story building with 12 rooms on each floor, 3 rows and 4 columns. As you have probably guessed, the reshape function changes the shape of an array. You give it a tuple of integers, corresponding to the new shape. If the dimensions are not compatible with the data, an exception is thrown. Selecting a single cell: We can select a single room by using its three coordinates, namely, the floor, column, and row.

For example, the room on the first floor, in the first row, and in the first column you can have floor 0 and room 0—it's just a matter of convention can be represented by: In: b[0,0,0] Out: 0 3. Selecting slices: If we don't care about the floor, but still want the first column and row, we replace the first index by a : colon because we just need to specify the floor number and omit the other indices: In: b[:,0,0] Out: array [ This selects In: b[0] Out: array [[ 0, [ 4, [ 8, 0, 12] the first floor 1, 2, 3], 5, 6, 7], 9, 10, 11]] [ 34 ] Chapter 2 We could also have written: In: b[0, Out: array [[ [ [ :, :] 0, 4, 8, 1, 2, 3], 5, 6, 7], 9, 10, 11]] An ellipsis replaces multiple colons, so, the preceding code is equivalent to: In: b[0, Out: array [[ [ [ Using steps to slice: Furthermore, we can also select each second element of this selection: In: b[0,1,] Out: array [4, 6] 5.

Using ellipsis to slice: If we want to select all the rooms on both floors that are in the second column, regardless of the row, we will type the following code snippet: In: b[ Using negative indices: If we want to select the first floor, last column, then type the following code snippet: In: b[0,:,-1] Out: array [ 3, 7, 11] If we want to select rooms on the ground floor, last column reversed, then type the following code snippet: In: b[0,, -1] Out: array [11, 7, 3] Every second element of that slice: In: b[0,,-1] Out: array [ 3, 11] The command that reverses a one-dimensional array puts the top floor following the ground floor: In: b[] Out: array [[[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]], [[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]] What just happened?

We sliced a multidimensional NumPy array using several different methods. Time for action — manipulating array shapes We already learned about the reshape function. Another recurring task is flattening of arrays. Ravel: We can accomplish this with the ravel function: In: b Out: array [[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]] [ 36 ] Chapter 2 In: b.

In: b. Now, we have a 6-by-4 array. Transpose: In linear algebra, it is common to transpose matrices. We can do that too, by using the following code: In: b. We manipulated the shapes of NumPy arrays using the ravel function, function flatten, the reshape function, and the resize method. Stacking Arrays can be stacked horizontally, depth-wise, or vertically. Horizontal stacking: Starting with horizontal stacking, we will form a tuple of ndarrays and give it to the hstack function.

This time, it is given to the vstack function. This can be seen as follows: In: vstack a, b Out: array [[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]] The concatenate function produces the same result with the axis set to 0. This is the default value for the axis argument.

This means stacking of a list of arrays along the third axis depth. For instance, we could stack 2D arrays of image data on top of each other. Isn't it beautiful? Row stacking: NumPy, of course, also has a function that does row-wise stacking. Yes, exactly the vstack function results. We stacked arrays horizontally, depth-wise, or vertically. Splitting Arrays can be split vertically, horizontally, or depth wise.

The functions involved are hsplit, vsplit, dsplit, and split. We can either split into arrays of the same shape or indicate the position after which the split should occur. Time for action — splitting arrays 1. Horizontal splitting: The ensuing code splits an array along its horizontal axis into three pieces of the same size and shape. Depth-wise splitting: The dsplit function, unsurprisingly, splits depth-wise. We split arrays using the hsplit, vsplit, dsplit, and split functions.

This is shown a follows: In: b. This is just a product of the itemsize and size attributes: In: b. This is the only way to acquire a flatiter—we do not have access to a flatiter constructor. Setting the value of the flat attribute leads to overwriting the values of the whole array: In: b. This is shown as follows: 1. Convert to a list: In: b Out: array [ 1. The astype function also accepts the name of a type as a string. We converted NumPy arrays to a list and to arrays of different data types. Summary We learned a lot in this chapter about the NumPy fundamentals: data types and arrays.

Arrays have several attributes describing them. We learned that one of these attributes is the data type, which, in NumPy, is represented by a full-fledged object. NumPy arrays can be sliced and indexed in an efficient manner, just like Python lists.

NumPy arrays have the added ability of working with multiple dimensions. The shape of an array can be manipulated in many ways—stacking, resizing, reshaping, and splitting. A great number of convenience functions for shape manipulation were demonstrated in this chapter. Having learned about the basics, it's time to move on to the study of commonly-used functions in Chapter 3, Get to terms with commonly used functions.

This includes basic statistical and mathematical functions. In particular, we will learn how to load data from files using a historical stock prices example. Also, we will get to see the basic NumPy mathematical and statistical functions. We will learn how to read from, and write to, files. Also, we will get a taste of the functional programming and linear algebra possibilities in NumPy. Data is usually stored in files. You would not get far if you are not able to read from and write to files. Identity matrix creation 1. Creating an identity matrix: The identty matrix is a square matrix with ones on the diagonal and zeroes for the rest.

The only argument we need to give the eye function is the number of ones. Saving data: Save the data with the savetxt function. We obviously need to specify the name of the file that we want to save the data in and the array containing the data itself: numpy. You can check for yourself whether the contents are as expected.

Reading and writing files is a necessary skill for data analysis. We wrote to a file with savetxt. We made an identity matrix with the eye function. Often, the CSV file is just a dump from a database file.

Usually, each field in the CSV file corresponds to a database table column. As we all know, spreadsheet programs, such as Excel, can produce CSV files as well. Luckily, the loadtxt function can conveniently read CSV files, split up the fields and load the data into NumPy arrays. In the following example, we will load historical price data for Apple the company, not the fruit. The data is in CSV format. The first column contains a symbol that identifies the stock. In our case, it is AAPL, next in our case. Nn is the date in dd-mm-yyyy format. The third column is empty.

Then, in order, we have the open, high, low, and close price. Last, but not least, is the volume of the day. This is what a line looks like: AAPL,, , In the preceding sample, that would be We have set the delimiter to , comma , since we are dealing with a comma separated value file. The usecols parameter is set through a tuple to get the seventh and eighth fields, which correspond to the close price and volume. Unpack is set to True, which means that data will be unpacked and assigned to the c and v variables that will hold the close price and volume, respectively.

CSV files are a special type of file that we have to deal with frequently. We read a CSV file containing stock quotes with the loadtxt function. We indicated to the loadtxt function that the delimiter of our file was a comma. We specified which columns we were interested in, through the usecols argument, and set the unpack parameter to True so that the data was unpacked for further use. The higher the volume, the more significant a price move typically is. VWAP is calculated by using volume values as weights.

Read the data into arrays. That wasn't very hard, was it? We just called the average function and set its weights parameter to use the v array for weights. By the way, NumPy also has a function to calculate the arithmetic mean. The mean function The mean function is quite friendly and not so mean.

This function calculates the arithmetic mean of an array. It is just a variation on a theme really. The idea is that recent price quotes are more important, so we should give recent prices higher weights. The easiest way is to create an array with the arange function of increasing values from zero to the number of elements in the close price array. This is not necessarily the correct way. In fact, most of the examples concerning stock price analysis in this book are only illustrative. Which function returns the weighted average of an array? Calculate the mean for the volume and the other prices.

Value range Usually, we don't only want to know the average or arithmetic mean of a set of values, which are sort of in the middle; we also want the extremes, the full range—the highest and lowest values. The sample data that we are using here already has those values per day—the high and low price. However, we need to know the highest value of the high price and the lowest price value of the low price. After all, how else would we know how much our Apple stocks would gain or lose. Time for action — finding highest and lowest values The min and max functions are the answer to our requirement.

Calculating the spread: NumPy allows us to compute the spread of an array with a function called The ptp function returns the difference between the maximum and minimum values of an array. In other words, it is equal to max array — min array. Call the ptp function: print "Spread high price", numpy. We defined a range of highest to lowest values for the price. The highest value was given by applying the max function to the high price array. Similarly, the lowest value was found by calling the min function to the low price array. We also calculated the peak to peak distance with the ptp function.

Statistics Stock traders are interested in the most probable close price. Common sense says that this should be close to some kind of an average. The arithmetic mean and weighted average are ways to find the center of a distribution of values. However, both are not robust and sensitive to outliers. For instance, if we had a close price value of a million dollars, this would have influenced the outcome of our calculations.

Time for action — doing simple statistics One thing that we can do is use some kind of threshold to weed out outliers, but there is a better way. It is called the median, and it basically picks the middle value of a sorted set of values. For example, if we have the values of 1, 2, 3, 4 and 5. The median would be 3, since it is in the middle.

These are the steps to calculate the median: 1. Determine the median of the close price: Create a new Python script and call it simplestats. You already know how to load the data from a CSV file into an array.

### Read More From Ivan Idris

So, copy that line of code and make sure that it only gets the close price. We will call it and print the result immediately. Not because we are paranoid or anything! Obviously, we could do it by just going through the file and finding the correct value, but that is no fun. Instead we will just mimic the median algorithm by sorting the close price array and printing the middle value of the sorted array. The msort function does the first part for us. Upon further investigation we find that the median function return value doesn't even appear in our file.

That's even stranger! Before filing bugs with the NumPy team, let's have a look at the documentation. This mystery is easy to solve. It turns out that our naive algorithm only works for arrays with odd lengths. For even-length arrays, the median is calculated from the average of the two array values in the middle.

Variance tells us how much a variable varies. In our case, it also tells us how risky an investment is, since a stock price that varies too wildly is bound to get us into trouble. Calculate the variance of the close price: With NumPy, this is just a one liner. Mind you, this definition might be different than the one in your statistics book, but that is quite common in the field of statistics.

The variance is defined as the mean of the square of deviations from the mean, divided by the number of elements in the array. Some books tell us to divide by the number of elements in the array minus one. Maybe you noticed something new. We suddenly called the mean function on the c array.

1. Shop now and earn 2 points per \$1.
3. King Stakh’s Wild Hunt!

Yes, this is legal, because the ndarray object has a mean method. This is for your convenience. For now, just keep in mind that this is possible. Stock returns In academic literature it is more common to base analysis on stock returns and log returns of the close price. Simple returns are just the rate of change from one value to the next. Logarithmic returns or log returns are determined by taking the log of all the prices and calculating the differences between them.

In high school, we learned that the difference between the log of "a" and the log of "b" is equal to the log of "a divided by b". Log return, therefore, also measures rate of change. Returns are dimensionless, since, in the act of dividing, we divide dollar by dollar or some other currency. Anyway, investors are most likely to be interested in the variance or standard deviation of the returns, as this represents risk.

• [Book Review] Numpy Beginner's Guide - Christopher Felton;
• Schizophrenia: A Case Study of the Movie A BEAUTIFUL MIND - Second Edition.
• Book Preview: Numpy Beginner's Guide - The Mouse Vs. The Python.
• Graham Park.
• Simple returns: First, let's calculate simple returns. NumPy has the diff function that returns an array built up of the difference between two consecutive array elements. This is sort of like differentiation in calculus. To get the returns, we also have to divide by the value of the previous day. We must be careful though. The array returned by diff is one element shorter than the close prices array.

Logarithmic returns: The log return is even easier to calculate. We use the log function to get the log of the close price and then unleash the diff function on the result. If it did we would have gotten an error. Stock prices are, however, always positive, so we didn't have to check. Selecting positive returns: Quite likely, we will be interested in days when the return is positive.

In the current setup, we can get the next best thing with the where function, which returns the indices of an array that satisfies a condition. Annualized and monthly volatilities: In investing, volatility measures price variation of a financial security. Historical volatility is calculated from historical price data.

The logarithmic returns are interesting if you want to know the historical volatility—for instance, the annualized or monthly volatility. The annualized volatility is equal to the standard deviation of the log returns as a ratio of its mean, divided by one over the square root of the number of business days in a year, usually one assumes Calculate it with the std and mean functions. Since, in Python, integer division works differently than float division, we needed to use floats to make sure that we get the proper results.

We calculated the simple stock returns with the diff function, which calculates differences between sequential elements. The log function computes the natural logarithms of array elements. We used it to calculate the logarithmic returns. At the end of the tutorial we calculated the annual and monthly volatility. Dates Do you sometimes have the Monday blues or the Friday fever? Ever wondered whether the stock market suffers from said phenomena?

Well, I think this certainly warrants extensive research. Time for action — dealing with dates First, we will read the close price data. Second, we will split the prices according to the day of the week. Third, for each weekday, we will calculate the average price. Finally, we will find out which day of the week has the highest average and which has the lowest average. A health warning before we commence: you might be tempted to use the result to buy stock on one day and sell on the other. However, we don't have enough data to make these kind of decisions. Please consult a professional statistician first!

Coders hate dates because they are so complicated! NumPy is very much oriented towards floating point operations. For that reason, we need to take extra effort to process dates. Converter function: Obviously, NumPy tried to convert the dates into floats. What we have to do is explicitly tell NumPy how to convert the dates. The loadtxt function has a special parameter for this purpose. The parameter is called converters and is a dictionary that links columns with so-called converter functions. It is our responsibility to write the converter function. Let's write the function down: Monday 0 Tuesday 1 Wednesday 2 Thursday 3 Friday 4 Saturday 5 Sunday 6 def datestr2num s : return datetime.

This is, by the way, standard Python and is not related to NumPy itself. Second, the datetime object is turned into a day. Finally the weekday method is called on the date to return a number. As you can read in the comments, the number is between 0 and 6. The actual number, of course, is not important for our algorithm; it is only used as identification. No Saturdays and Sundays, as you can see. Exchanges are closed over the weekend. Initialize the averages array: We will now make an array that has five elements for each day of the week. Calculate the averages: We already learned about the where function that returns indices of the array for elements that conform to a specified condition.

The take function can use these indices and takes the values of the corresponding array items. We will use the take function to get the close prices for each week day. In the following loop we go through the date values which are 0 to 4, better known as Monday to Friday. We get the indices with the where function for each day and store it in the indices array. Then, we retrieve the values corresponding to the indices, using the take function.

Find the maxima and minima: If you want, you can go ahead and find out which day has the highest, and which the lowest, average. The argmin function returned the index of the lowest value in the averages array. The index returned was 4, which corresponds to Friday.

## NumPy 1.5 Beginner's Guide

The argmax function returned the index of the highest value in the averages array. The index returned was 2, which corresponds to Wednesday. For the sample data, it appears that Friday is the cheapest day and Wednesday is the day when your Apple stock will be worth the most. Ignoring the fact that we have very little data, is there a better method to compute the averages? Shouldn't we involve volume data as well?

Maybe it makes more sense to you to do a time-weighted average. Give it a go! You can find some hints on how to go about doing this at the beginning of this chapter. Weekly summary The data that we used in the previous Time for action tutorials is end-of-day data. In essence, it is summarized data compiled from trade data for a certain day.

If you are interested in the cotton market and have decades of data, you might want to summarize and compress the data even further. Let's do that. Let's summarize the data of Apple stocks to give us weekly summaries. Time for action — summarizing data The data we will summarize will be for a whole business week from Monday to Friday. During the period covered by the data, there was one holiday on February 21st, President's Day. This happened to be a Monday and the US stock exchanges were closed on this day.

As a consequence, there is no entry for this day, in the sample. The first day in the sample is a Friday, which is inconvenient. Use the following instructions to summerize data: 1. Finding the first Monday: Commencing, we will find the first Monday in our sample data. Recall that Mondays have the code 0 in Python. This is what we will put in the condition of a where function.