More on this book
Community
Kindle Notes & Highlights
by
Wes McKinney
Read between
December 30, 2019 - June 10, 2020
The main restriction on function arguments is that the keyword arguments must follow the positional arguments (if any). You can specify keyword arguments in any order;
I generally discourage use of the global keyword. Typically global variables are used to store some kind of state in a system. If you find yourself using a lot of them, it may indicate a need for object-oriented programming
Python has support for so-called anonymous or lambda functions, which are a way of writing functions consisting of a single statement, the result of which is the return value. They are defined with the lambda keyword, which has no meaning other than “we are declaring an anonymous function”:
One reason lambda functions are called anonymous functions is that , unlike functions declared with the def keyword, the function object itself is never given an explicit __name__ attribute.
An iterator is any object that will yield objects to the Python interpreter when used in a context like a for loop.
Most methods expecting a list or list-like object will also accept any iterable object.
A generator is a concise way to construct a new iterable object. Whereas normal functions execute and return a single result at a time, generators return a sequence of multiple results lazily, pausing after each one until the next one is requested. To create a generator, use the yield keyword instead of return in a function:
When you actually call the generator, no code is immediately executed:
It is not until you request elements from the generator that it begins executing its code:
An ndarray is a generic multidimensional container for homogeneous data; that is, all of the elements must be the same type.
empty creates an array without initializing its values to any particular value.
It’s not safe to assume that np.empty will return an array of all zeros. In some cases, it may return uninitialized “garbage” values.
arange is an array-valued version of the built-in Python range function:
It’s important to be cautious when using the numpy.string_ type, as string data in NumPy is fixed size and may truncate input without warning. pandas has more intuitive out-of-the-box behavior on non-numeric data.
Calling astype always creates a new array (a copy of the data), even if the new dtype is the same as the old dtype.
Arithmetic operations with scalars propagate the scalar argument to each element in the array:
Comparisons between arrays of the same size yield boolean arrays:
Evaluating operations between differently sized arrays is called broadcasting and will be discussed in more detail in Appendix A
An important first distinction from Python’s built-in lists is that array slices are views on the original array. This means that the data is not copied, and any modifications to the view will be reflected in the source array.
As NumPy has been designed to be able to work with very large arrays, you could imagine performance and memory problems if NumPy insisted on always copying data.
If you want a copy of a slice of an ndarray instead of a view, you will need to explicitly copy the array — for example, arr[5:8].copy().
you can pass a comma-separated list of indices to select individual elements.
Selecting data from an array by boolean indexing always creates a copy of the data, even if the returned array is unchanged.
The Python keywords and and or do not work with boolean arrays. Use & (and) and | (or) instead.
Passing multiple index arrays does something slightly different; it selects a one-dimensional array of elements corresponding to each tuple of indices:
Keep in mind that fancy indexing, unlike slicing, always copies the data into a new array.
swapaxes similarly returns a view on the data without making a copy.
A universal function, or ufunc, is a function that performs element-wise operations on data in ndarrays.
While not common, a ufunc can return multiple arrays. modf is one example, a vectorized version of the built-in Python divmod; it returns the fractional and integral parts of a floating-point array:
Ufuncs accept an optional out argument that allows them to operate in-place on arrays:
In general, vectorized array operations will often be one or two (or more) orders of magnitude faster than their pure Python equivalents, with the biggest impact in any kind of numerical computations.
Python’s built-in random module, by contrast, only samples one value at a time. As you can see from this benchmark, numpy.random is well over an order of magnitude faster for generating very large samples:
Note that using argmax here is not always efficient because it always makes a full scan of the array.
While pandas adopts many coding idioms from NumPy, the biggest difference is that pandas is designed for working with tabular or heterogeneous data. NumPy, by contrast, is best suited for working with homogeneous numerical array data.
Using NumPy functions or NumPy-like operations, such as filtering with a boolean array, scalar multiplication, or applying math functions, will preserve the index-value link:
Another way to think about a Series is as a fixed-length, ordered dict, as it is a mapping of index values to data values. It can be used in many contexts where you might use a dict:
Both the Series object itself and its index have a name attribute, which integrates with other key areas of pandas functionality:
There are many ways to construct a DataFrame, though one of the most common is from a dict of equal-length lists or NumPy arrays:
If you specify a sequence of columns, the DataFrame’s columns will be arranged in that order:
If you pass a column that isn’t contained in the dict, it will appear with missing values in the result:
Attribute-like access (e.g., frame2.year) and tab completion of column names in IPython is provided as a convenience. frame2[column] works for any column name, but frame2.column only works when the column name is a valid Python variable name.
New columns cannot be created with the frame2.eastern syntax.
The column returned from indexing a DataFrame is a view on the underlying data, not a copy. Thus, any in-place modifications to the Series will be reflected in the DataFrame. The column can be explicitly copied with the Series’s copy method.
You can transpose the DataFrame (swap rows and columns) with similar syntax to a NumPy array:
Index objects are immutable and thus can’t be modified by the user:
Immutability makes it safer to share Index objects among data structures:
In addition to being array-like, an Index also behaves like a fixed-size set:
Unlike Python sets, a pandas Index can contain duplicate labels:

