Using Arrays for Analyses

Introduction to Python for Finance

Adina Howe

Instructor

Indexing Arrays

import numpy as np

months_array = np.array(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'])

indexing_array = np.array([1, 3, 5])
months_subset = months_array[indexing_array]
print(months_subset)
['Feb' 'Apr' 'Jun']
Introduction to Python for Finance

More on indexing arrays

import numpy as np

months_array = np.array(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'])

negative_index = np.array([-1, -2])

print(months_array[negative_index])
['Jun' 'May']
Introduction to Python for Finance

Boolean arrays

import numpy as np

months_array = np.array(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'])

boolean_array = np.array([True, True, True, False, False, False])

print(months_array[boolean_array])
['Jan' 'Feb' 'Mar']
Introduction to Python for Finance

More on Boolean arrays

prices_array = np.array([238.11, 237.81, 238.91])

# Create a Boolean array boolean_array = (prices_array > 238) print(boolean_array)
[ True False  True]
print(prices_array[boolean_array])
[ 238.11  238.91]
Introduction to Python for Finance

Let's practice!

Introduction to Python for Finance

Preparing Video For Download...