Arrays

Introduction to Python for Finance

Adina Howe

Instructor

Installing packages

pip3 install package_name_here
pip3 install numpy
Introduction to Python for Finance

Importing packages

import numpy
Introduction to Python for Finance

NumPy and Arrays

import numpy

my_array = numpy.array([0, 1, 2, 3, 4])
print(my_array)
[0, 1, 2, 3, 4]
print(type(my_array))
<class 'numpy.ndarray'>
Introduction to Python for Finance

Using an alias

import package_name
package_name.function_name(...)
import numpy as np
my_array = np.array([0, 1, 2, 3, 4])
print(my_array)
[0, 1, 2, 3, 4]
Introduction to Python for Finance

Why use an array for financial analysis?

  • Arrays can handle very large datasets efficiently
    • Computationally-memory efficient
    • Faster calculations and analysis than lists
    • Diverse functionality (many functions in Python packages)
Introduction to Python for Finance

What's the difference?

NumPy arrays
my_array = np.array([3, 'is', True])

print(my_array)
['3' 'is' 'True']
Lists
my_list = [3, 'is', True]

print(my_list)
[3, 'is', True]
Introduction to Python for Finance

Array operations

Arrays
import numpy as np

array_A = np.array([1, 2, 3])
array_B = np.array([4, 5, 6])

print(array_A + array_B)
[5 7 9]
Lists
list_A = [1, 2, 3]
list_B = [4, 5, 6]

print(list_A + list_B)
[1, 2, 3, 4, 5, 6]
Introduction to Python for Finance

Array indexing

import numpy as np

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

print(months_array[3])
Apr
print(months_array[2:5])
['March' 'Apr' 'May']
Introduction to Python for Finance

Array slicing with steps

import numpy as np

months_array = np.array(['Jan', 'Feb', 'March', 'Apr', 'May'])
print(months_array[0:5:2])
['Jan' 'March' 'May']
Introduction to Python for Finance

Let's practice!

Introduction to Python for Finance

Preparing Video For Download...