Skip to content

Commit 42f44de

Browse files
committed
Update for Python 3.x
1 parent 0eb2c7a commit 42f44de

File tree

103 files changed

+6795
-6794
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+6795
-6794
lines changed

Examples/Basic/numpy-tutorial.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,33 @@
3535
#
3636
#
3737
## Lets get started!
38-
print "Importing numpy"
38+
print("Importing numpy")
3939
import numpy as np
4040

4141
## This loads the numpy library and lets us refer to it by the shorthand "np",
4242
## which is the convention used in the numpy documentation and in many
4343
## online tutorials/examples
4444

45-
print "Creating arrays"
45+
print("Creating arrays")
4646
## Now lets make an array to play around with. You can make numpy arrays in
4747
## a number of ways,
4848
## Filled with zeros:
4949
zeroArray = np.zeros( (2,3) ) # [[ 0. 0. 0.]
50-
print zeroArray # [ 0. 0. 0.]]
50+
print(zeroArray) # [ 0. 0. 0.]]
5151

5252
## Or ones:
5353
oneArray = np.ones( (2,3) ) # [[ 1. 1. 1.]
54-
print oneArray # [ 1. 1. 1.]]
54+
print(oneArray) # [ 1. 1. 1.]]
5555

5656
## Or filled with junk:
5757
emptyArray = np.empty( (2,3) )
58-
print emptyArray
58+
print(emptyArray)
5959

6060
## Note, emptyArray might look random, but it's just uninitialized which means
6161
## you shouldn't count on it having any particular data in it, even random
6262
## data! If you do want random data you can use random():
6363
randomArray = np.random.random( (2,3) )
64-
print randomArray
64+
print(randomArray)
6565

6666
## If you're following along and trying these commands out, you should have
6767
## noticed that making randomArray took a lot longer than emptyArray. That's
@@ -74,29 +74,29 @@
7474
[4,5,6]]
7575

7676
myArray = np.array(foo) # [[1 2 3]
77-
print myArray # [4 5 6]]
77+
print(myArray) # [4 5 6]]
7878

7979

80-
print "Reshaping arrays"
80+
print("Reshaping arrays")
8181
## Of course, if you're typing out a range for a larger matrix, it's easier to
8282
## use arange(...):
8383
rangeArray = np.arange(6,12).reshape( (2,3) ) # [[ 6 7 8]
84-
print rangeArray # [ 9 10 11]]
84+
print(rangeArray) # [ 9 10 11]]
8585

8686
## there's two things going on here. First, the arange(...) function returns a
8787
## 1D array similar to what you'd get from using the built-in python function
8888
## range(...) with the same arguments, except it returns a numpy array
8989
## instead of a list.
90-
print np.arange(6,12) # [ 6 7 8 9 10 11 12]
90+
print(np.arange(6,12)) # [ 6 7 8 9 10 11 12]
9191

9292
## the reshape method takes the data in an existing array, and stuffs it into
9393
## an array with the given shape and returns it.
94-
print rangeArray.reshape( (3,2) ) # [[ 6 7]
94+
print(rangeArray.reshape( (3,2) )) # [[ 6 7]
9595
# [ 8 9]
9696
# [10 11]]
9797

9898
#The original array doesn't change though.
99-
print rangeArray # [[ 6 7 8]
99+
print(rangeArray) # [[ 6 7 8]
100100
# [ 9 10 11]
101101

102102
## When you use reshape(...) the total number of things in the array must stay
@@ -106,69 +106,69 @@
106106
squareArray = np.arange(1,10).reshape( (3,3) ) #this is fine, 9 elements
107107

108108

109-
print "Accessing array elements"
109+
print("Accessing array elements")
110110
## Accessing an array is also pretty straight forward. You access a specific
111111
## spot in the table by referring to its row and column inside square braces
112112
## after the array:
113-
print rangeArray[0,1] #7
113+
print(rangeArray[0,1]) #7
114114

115115
## Note that row and column numbers start from 0, not 1! Numpy also lets you
116116
## refer to ranges inside an array:
117-
print rangeArray[0,0:2] #[6 7]
118-
print squareArray[0:2,0:2] #[[1 2] # the top left corner of squareArray
117+
print(rangeArray[0,0:2]) #[6 7]
118+
print(squareArray[0:2,0:2]) #[[1 2] # the top left corner of squareArray
119119
# [4 5]]
120120

121121
## These ranges work just like slices and python lists. n:m:t specifies a range
122122
## that starts at n, and stops before m, in steps of size t. If any of these
123123
## are left off, they're assumed to be the start, the end+1, and 1 respectively
124-
print squareArray[:,0:3:2] #[[1 3] #skip the middle column
124+
print(squareArray[:,0:3:2]) #[[1 3] #skip the middle column
125125
# [4 6]
126126
# [7 9]]
127127

128128
## Also like python lists, you can assign values to specific positions, or
129129
## ranges of values to slices
130-
squareArray[0,:] = np.array(range(1,4)) #set the first row to 1,2,3
130+
squareArray[0,:] = np.array(list(range(1,4))) #set the first row to 1,2,3
131131
squareArray[1,1] = 0 # set the middle spot to zero
132132
squareArray[2,:] = 1 # set the last row to ones
133-
print squareArray # [[1 2 3]
133+
print(squareArray) # [[1 2 3]
134134
# [4 0 6]
135135
# [1 1 1]]
136136

137137
## Something new to numpy arrays is indexing using an array of indices:
138138
fibIndices = np.array( [1, 1, 2, 3] )
139139
randomRow = np.random.random( (10,1) ) # an array of 10 random numbers
140-
print randomRow
141-
print randomRow[fibIndices] # the first, first, second and third element of
140+
print(randomRow)
141+
print(randomRow[fibIndices]) # the first, first, second and third element of
142142
# randomRow
143143

144144
## You can also use an array of true/false values to index:
145145
boolIndices = np.array( [[ True, False, True],
146146
[False, True, False],
147147
[ True, False, True]] )
148-
print squareArray[boolIndices] # a 1D array with the selected values
148+
print(squareArray[boolIndices]) # a 1D array with the selected values
149149
# [1 3 0 1 1]
150150

151151
## It gets a little more complicated with 2D (and higher) arrays. You need
152152
## two index arrays for a 2D array:
153153
rows = np.array( [[0,0],[2,2]] ) #get the corners of our square array
154154
cols = np.array( [[0,2],[0,2]] )
155-
print squareArray[rows,cols] #[[1 3]
155+
print(squareArray[rows,cols]) #[[1 3]
156156
# [1 1]]
157157
boolRows = np.array( [False, True, False] ) # just the middle row
158158
boolCols = np.array( [True, False, True] ) # Not the middle column
159-
print squareArray[boolRows,boolCols] # [4 6]
159+
print(squareArray[boolRows,boolCols]) # [4 6]
160160

161-
print "Operations on arrays"
161+
print("Operations on arrays")
162162
## One useful trick is to create a boolean matrix based on some test and use
163163
## that as an index in order to get the elements of a matrix that pass the
164164
## test:
165165
sqAverage = np.average(squareArray) # average(...) returns the average of all
166166
# the elements in the given array
167167
betterThanAverage = squareArray > sqAverage
168-
print betterThanAverage #[[False False True]
168+
print(betterThanAverage) #[[False False True]
169169
# [ True False True]
170170
# [False False False]]
171-
print squareArray[betterThanAverage] #[3 4 6]
171+
print(squareArray[betterThanAverage]) #[3 4 6]
172172

173173
## Indexing like this can also be used to assign values to elements of the
174174
## array. This is particularly useful if you want to filter an array, say by
@@ -188,32 +188,32 @@
188188
# truncate them down to integers.
189189
clampedSqArray[ (squareArray-sqAverage) > sqStdDev ] = sqAverage+sqStdDev
190190
clampedSqArray[ (squareArray-sqAverage) < -sqStdDev ] = sqAverage-sqStdDev
191-
print clampedSqArray # [[ 1. 2. 3. ]
191+
print(clampedSqArray) # [[ 1. 2. 3. ]
192192
# [ 3.90272394 0.31949828 3.90272394]
193193
# [ 1. 1. 1. ]]
194194

195195

196196
## Multiplying and dividing arrays by numbers does what you'd expect. It
197197
## multiples/divides element-wise
198-
print squareArray * 2 # [[ 2 4 6]
198+
print(squareArray * 2) # [[ 2 4 6]
199199
# [ 8 0 12]
200200
# [ 2 2 2]]
201201

202202
## Addition works similarly:
203-
print squareArray + np.ones( (3,3) ) #[[2 3 4]
203+
print(squareArray + np.ones( (3,3) )) #[[2 3 4]
204204
# [5 1 7]
205205
# [2 2 2]]
206206

207207
## Multiplying two arrays together (of the same size) is also element wise
208-
print squareArray * np.arange(1,10).reshape( (3,3) ) #[[ 1 4 9]
208+
print(squareArray * np.arange(1,10).reshape( (3,3) )) #[[ 1 4 9]
209209
# [16 0 36]
210210
# [ 7 8 9]]
211211

212212
## Unless you use the dot(...) function, which does matrix multiplication
213213
## from linear algebra:
214214
matA = np.array( [[1,2],[3,4]] )
215215
matB = np.array( [[5,6],[7,8]] )
216-
print np.dot(matA,matB) #[[19 22]
216+
print(np.dot(matA,matB)) #[[19 22]
217217
# [43 50]]
218218

219219
## And thats it! There's a lot more to the numpy library, and there are a few

Examples/Basic/pandas-tutorial.py

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,67 +20,67 @@
2020
for i in range(1, 6):
2121
ldt_timestamps.append(dt.datetime(2011, 1, i, 16))
2222

23-
print "The index we created has the following dates : "
24-
print ldt_timestamps
25-
print
23+
print("The index we created has the following dates : ")
24+
print(ldt_timestamps)
25+
print()
2626

27-
## TimeSeries
28-
ts_single_value = pd.TimeSeries(0.0, index=ldt_timestamps)
29-
print "A timeseries initialized to one single value : "
27+
## Series
28+
ts_single_value = pd.Series(0.0, index=ldt_timestamps)
29+
print("A timeseries initialized to one single value : ")
3030

3131
na_vals = np.arange(len(ldt_timestamps))
32-
print "Dummy initialized array : "
33-
print na_vals
34-
print
32+
print("Dummy initialized array : ")
33+
print(na_vals)
34+
print()
3535

36-
ts_array = pd.TimeSeries(na_vals, index=ldt_timestamps)
37-
print "A timeseries initialized using a numpy array : "
38-
print ts_array
39-
print
36+
ts_array = pd.Series(na_vals, index=ldt_timestamps)
37+
print("A timeseries initialized using a numpy array : ")
38+
print(ts_array)
39+
print()
4040

41-
print "Reading the timeseries for a particular date"
42-
print "Date : ", ldt_timestamps[1]
43-
print "Value : ", ts_array[ldt_timestamps[1]]
44-
print
41+
print("Reading the timeseries for a particular date")
42+
print("Date : ", ldt_timestamps[1])
43+
print("Value : ", ts_array[ldt_timestamps[1]])
44+
print()
4545

46-
print "Initializing a list of symbols : "
46+
print("Initializing a list of symbols : ")
4747
ls_symbols = ['AAPL', 'GOOG', 'MSFT', 'IBM']
48-
print ls_symbols
49-
print
48+
print(ls_symbols)
49+
print()
5050

51-
print "Initializing a dataframe with one value : "
51+
print("Initializing a dataframe with one value : ")
5252
df_single = pd.DataFrame(index=ldt_timestamps, columns=ls_symbols)
5353
df_single = df_single.fillna(0.0)
54-
print df_single
55-
print
54+
print(df_single)
55+
print()
5656

57-
print "Initializing a dataframe with a numpy array : "
57+
print("Initializing a dataframe with a numpy array : ")
5858
na_vals_2 = np.random.randn(len(ldt_timestamps), len(ls_symbols))
5959
df_vals = pd.DataFrame(na_vals_2, index=ldt_timestamps, columns=ls_symbols)
60-
print df_vals
61-
print
60+
print(df_vals)
61+
print()
6262

63-
print "Access the timeseries of a particular symbol : "
64-
print df_vals[ls_symbols[1]]
65-
print
63+
print("Access the timeseries of a particular symbol : ")
64+
print(df_vals[ls_symbols[1]])
65+
print()
6666

67-
print "Access the timeseries of a particular date : "
68-
print df_vals.ix[ldt_timestamps[1]]
69-
print
67+
print("Access the timeseries of a particular date : ")
68+
print(df_vals.ix[ldt_timestamps[1]])
69+
print()
7070

71-
print "Access the value for a specific symbol on a specific date: "
72-
print df_vals[ls_symbols[1]].ix[ldt_timestamps[1]]
73-
print
71+
print("Access the value for a specific symbol on a specific date: ")
72+
print(df_vals[ls_symbols[1]].ix[ldt_timestamps[1]])
73+
print()
7474

75-
print "Reindexing the dataframe"
75+
print("Reindexing the dataframe")
7676
ldt_new_dates = [dt.datetime(2011, 1, 3, 16),
7777
dt.datetime(2011, 1, 5, 16),
7878
dt.datetime(2011, 1, 7, 16)]
7979
ls_new_symbols = ['AAPL', 'IBM', 'XOM']
8080
df_new = df_vals.reindex(index=ldt_new_dates, columns=ls_new_symbols)
81-
print df_new
82-
print "Observe that reindex carried over whatever values it could find and set the rest to NAN"
83-
print
81+
print(df_new)
82+
print("Observe that reindex carried over whatever values it could find and set the rest to NAN")
83+
print()
8484

85-
print "For pandas rolling statistics please refer : http://pandas.pydata.org/pandas-docs/dev/computation.html#moving-rolling-statistics-moments"
85+
print("For pandas rolling statistics please refer : http://pandas.pydata.org/pandas-docs/dev/computation.html#moving-rolling-statistics-moments")
8686

Examples/Basic/tutorial1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import matplotlib.pyplot as plt
2222
import pandas as pd
2323

24-
print "Pandas Version", pd.__version__
24+
print("Pandas Version", pd.__version__)
2525

2626

2727
def main():
@@ -49,7 +49,7 @@ def main():
4949
# Reading the data, now d_data is a dictionary with the keys above.
5050
# Timestamps and symbols are the ones that were specified before.
5151
ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
52-
d_data = dict(zip(ls_keys, ldf_data))
52+
d_data = dict(list(zip(ls_keys, ldf_data)))
5353

5454
# Filling the data for NAN
5555
for s_key in ls_keys:

Examples/Basic/tutorial2.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ def main():
3333
ls_symbols = ['$SPX', 'XOM', 'GOOG', 'GLD']
3434

3535
# Printing the first 5 rows
36-
print "First 5 rows of Price Data:"
37-
print na_price[:5, :]
38-
print
39-
print "First 5 rows of Dates:"
40-
print na_dates[:5, :]
36+
print("First 5 rows of Price Data:")
37+
print(na_price[:5, :])
38+
print()
39+
print("First 5 rows of Dates:")
40+
print(na_dates[:5, :])
4141

4242
# Creating the timestamps from dates read
4343
ldt_timestamps = []

0 commit comments

Comments
 (0)