Table Of ContentIntroduction to Python for
Econometrics, Statistics and Data Analysis
3rdEdition
KevinSheppard
UniversityofOxford
Tuesday3rd January,2017
2
-
©2017KevinSheppard
rd
Notes to the 3 Edition
Thiseditionincludesthefollowingchangesfromthefirstedition(August2014):
• RewritteninstallationsectionfocusedexclusivelyonusingContinuum’sAnaconda.
• Python3.5isthedefaultversionofPythoninsteadof2.7. Python3.5(ornewer)iswellsupportedby
thePythonpackagesrequiredtoanalyzedataandperformstatisticalanalysis,andbringsomenew
usefulfeatures,suchasanewoperatorformatrixmultiplication(@).
• Removeddistinctionbetweenintegersandlongsinbuilt-indatatypeschapter. Thisdistinctionis
onlyrelevantforPython2.7.
• dothasbeenremovedfrommostexamplesandreplacedwith@toproducemorereadablecode.
• SplitCythonandNumbaintoseparatechapterstohighlighttheimprovedcapabilitiesofNumba.
• VerifiedallcodeworkingoncurrentversionsofcorelibrariesusingPython3.5.
• pandas
– Updatedsyntaxofpandasfunctionssuchasresample.
– AddedpandasCategorical.
– Expandedcoverageofpandasgroupby.
– Expandedcoverageofdateandtimedatatypesandfunctions.
• Newchapterintroducingstatsmodels,apackagethatfacilitatesstatisticalanalysisofdata.statsmod-
els includes regression analysis, Generalized Linear Models (GLM) and time-series analysis using
ARIMAmodels.
ii
Changes since the Second Edition
• Fixedtyposreportedbyareader–thankstoIlyaSorvachev
• CodeverifiedagainstAnaconda2.0.1.
• AddeddiagnostictoolsandasimplemethodtouseexternalcodeintheCythonsection.
• UpdatedtheNumbasectiontoreflectrecentchanges.
• FixedsometyposinthechapteronPerformanceandOptimization.
• AddedexamplesofjoblibandIPython’sclustertothechapteronrunningcodeinparallel.
• Newchapterintroducingobject-orientedprogrammingasamethodtoprovidestructureandorga-
nizationtorelatedcode.
• Addedseaborntotherecommendedpackagelist, andhaveincludeditbedefaultinthegraphics
chapter.
• Based on experience teaching Python to economics students, the recommended installation has
beensimplifiedbyremovingthesuggestiontousevirtualenvironment. Thediscussionofvirtual
environmentsasbeenmovedtotheappendix.
• Rewrotepartsofthepandaschapter.
• ChangedtheAnacondainstalltousebothcreateandinstall,whichshowshowtoinstalladditional
packages.
• Fixedsomemissingpackagesinthedirectinstall.
• ChangedtheconfigurationofIPythontoreflectbestpractices.
• AddedsubsectioncoveringIPythonprofiles.
• SmallsectionaboutSpyderasagoodstartingIDE.
iv
nd
Notes to the 2 Edition
Thiseditionincludesthefollowingchangesfromthefirstedition(March2012):
• ThepreferredinstallationmethodisnowContinuumAnalytics’Anaconda.Anacondaisacomplete
scientificstackandisavailableforallmajorplatforms.
• Newchapteronpandas. pandasprovidesasimplebutpowerfultooltomanagedataandperform
preliminaryanalysis. Italsogreatlysimplifiesimportingandexportingdata.
• Newchapteronadvancedselectionofelementsfromanarray.
• Numbaprovidesjust-in-timecompilationfornumericPythoncodewhichoftenproduceslargeper-
formancegainswhenpureNumPysolutionsarenotavailable(e.g. loopingcode).
• Dictionary,setandtuplecomprehensions
• Numeroustypos
• AllcodehasbeenverifiedworkingagainstAnaconda1.7.0.
vi
Contents
1 Introduction 1
1.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Conventions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 ImportantComponentsofthePythonScientificStack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 UsingPython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.6 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.A AdditionalInstallationIssues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2 Python2.7vs. 3(andtherest) 19
2.1 Python2.7vs. 3.x . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.2 IntelMathKernelLibraryandAMD’sGPUOpenLibraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.3 OtherVariants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.A RelevantDifferencesbetweenPython2.7and3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3 Built-inDataTypes 23
3.1 VariableNames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.2 CoreNativeDataTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.3 AdditionalContainerDataTypesintheStandardLibrary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.4 PythonandMemoryManagement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.5 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
4 ArraysandMatrices 41
4.1 Array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.2 Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.3 1-dimensionalArrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
4.4 2-dimensionalArrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.5 MultidimensionalArrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.6 Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.7 AccessingElementsofanArray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
4.8 SlicingandMemoryManagement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
4.9 importandModules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
viii CONTENTS
4.10 CallingFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.11 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
5 BasicMath 59
5.1 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
5.2 Broadcasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
5.3 Addition(+)andSubtraction(-). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
5.4 Multiplication(*) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
5.5 MatrixMultiplication(@) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
5.6 ArrayandMatrixDivision(/) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
5.7 Exponentiation(**) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
5.8 Parentheses. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
5.9 Transpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
5.10 OperatorPrecedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
5.11 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
6 BasicFunctionsandNumericalIndexing 65
6.1 GeneratingArraysandMatrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
6.2 Rounding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
6.3 Mathematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
6.4 ComplexValues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
6.5 SetFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
6.6 SortingandExtremeValues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
6.7 NanFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
6.8 FunctionsandMethods/Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
6.9 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
7 SpecialArrays 79
7.1 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
8 ArrayandMatrixFunctions 81
8.1 Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
8.2 ShapeInformationandTransformation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
8.3 LinearAlgebraFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
8.4 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
9 ImportingandExportingData 95
9.1 ImportingDatausingpandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
9.2 ImportingDatawithoutpandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
9.3 SavingorExportingDatausingpandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
9.4 SavingorExportingDatawithoutpandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
9.5 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Description:New chapter introducing object-oriented programming as a method to in its infancy and a bridge to Python is used to provide important missing features. phones apps (iOS and Android) where x.y.z will depend on the version being installed and ISA will be either x86 or more likely x86_64.