Instant ebooks textbook Python Programming for Data Analysis 1st Edition José Unpingco download all chapters

Download as pdf or txt
Download as pdf or txt
You are on page 1of 62

Download the full version of the textbook now at textbookfull.

com

Python Programming for Data Analysis 1st


Edition José Unpingco

https://textbookfull.com/product/python-
programming-for-data-analysis-1st-edition-jose-
unpingco/

Explore and download more textbook at https://textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Python for Probability Statistics and Machine Learning


José Unpingco

https://textbookfull.com/product/python-for-probability-statistics-
and-machine-learning-jose-unpingco/

textbookfull.com

Python for Probability, Statistics, and Machine Learning


2nd Edition José Unpingco

https://textbookfull.com/product/python-for-probability-statistics-
and-machine-learning-2nd-edition-jose-unpingco/

textbookfull.com

Vertically Integrated Architectures: Versioned Data


Models, Implicit Services, and Persistence-Aware
Programming 1st Edition Jos Jong
https://textbookfull.com/product/vertically-integrated-architectures-
versioned-data-models-implicit-services-and-persistence-aware-
programming-1st-edition-jos-jong/
textbookfull.com

The Rabbis Donald Trump and the Third Temple 1st Edition
Thomas R. Horn

https://textbookfull.com/product/the-rabbis-donald-trump-and-the-
third-temple-1st-edition-thomas-r-horn/

textbookfull.com
Inter-Regional Relations and the Asia-Europe Meeting
(ASEM) 1st Edition Bart Gaens

https://textbookfull.com/product/inter-regional-relations-and-the-
asia-europe-meeting-asem-1st-edition-bart-gaens/

textbookfull.com

The Philosophical Thought of Wang Chong Alexus Mcleod

https://textbookfull.com/product/the-philosophical-thought-of-wang-
chong-alexus-mcleod/

textbookfull.com

IB English A Language Literature Course Book Oxford IB


Diploma Program Course Book 2nd Edition Rob Allison

https://textbookfull.com/product/ib-english-a-language-literature-
course-book-oxford-ib-diploma-program-course-book-2nd-edition-rob-
allison/
textbookfull.com

Just Enough R An Interactive Approach to Machine Learning


and Analytics 1st Edition Richard J. Roiger

https://textbookfull.com/product/just-enough-r-an-interactive-
approach-to-machine-learning-and-analytics-1st-edition-richard-j-
roiger/
textbookfull.com

Broke In Magic 1st Edition Winnie Winkle Winkle Winnie

https://textbookfull.com/product/broke-in-magic-1st-edition-winnie-
winkle-winkle-winnie/

textbookfull.com
Evidential Pluralism in the Social Sciences Philosophy and
Method in the Social Sciences 1st Edition Shan

https://textbookfull.com/product/evidential-pluralism-in-the-social-
sciences-philosophy-and-method-in-the-social-sciences-1st-edition-
shan/
textbookfull.com
José Unpingco

Python
Programming
for Data
Analysis
Python Programming for Data Analysis
José Unpingco

Python Programming
for Data Analysis
José Unpingco
University of California
San Diego
CA, USA

ISBN 978-3-030-68951-3 ISBN 978-3-030-68952-0 (eBook)


https://doi.org/10.1007/978-3-030-68952-0

© The Editor(s) (if applicable) and The Author(s), under exclusive license to Springer Nature Switzerland
AG 2021
This work is subject to copyright. All rights are solely and exclusively licensed by the Publisher, whether
the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse
of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and
transmission or information storage and retrieval, electronic adaptation, computer software, or by similar
or dissimilar methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication
does not imply, even in the absence of a specific statement, that such names are exempt from the relevant
protective laws and regulations and therefore free for general use.
The publisher, the authors, and the editors are safe to assume that the advice and information in this book
are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or
the editors give a warranty, expressed or implied, with respect to the material contained herein or for any
errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional
claims in published maps and institutional affiliations.

This Springer imprint is published by the registered company Springer Nature Switzerland AG
The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland
To Irene, Nicholas, and Daniella, for all their
patient support.
Preface

This book grew out of notes for the ECE143 Programming for Data Analysis class
that I have been teaching at the University of California, San Diego, which is a
requirement for both graduate and undergraduate degrees in Machine Learning and
Data Science. The reader is assumed to have some basic programming knowledge
and experience using another language, such as Matlab or Java. The Python idioms
and methods we discuss here focus on data analysis, notwithstanding Python’s
usage across many other topics. Specifically, because raw data is typically a mess
and requires much work to prepare, this text focuses on specific Python language
features to facilitate such cleanup, as opposed to only focusing on particular Python
modules for this.
As with ECE143, here we discuss why things are the way they are in Python
instead of just that they are this way. I have found that providing this kind of
context helps students make better engineering design choices in their codes, which
is especially helpful for newcomers to both Python and data analysis. The text is
sprinkled with little tricks of the trade to make it easier to create readable and
maintainable code suitable for use in both production and development.
The text focuses on using the Python language itself effectively and then moves
on to key third-party modules. This approach improves effectiveness in different
environments, which may or may not have access to such third-party modules. The
Numpy numerical array module is covered in depth because it is the foundation
of all data science and machine learning in Python. We discuss the Numpy array
data structure in detail, especially its memory aspects. Next, we move on to Pandas
and develop its many features for effective and fluid data processing. Because data
visualization is key to data science and machine learning, third-party modules such
as Matplotlib are developed in depth, as well as web-based modules such as Bokeh,
Holoviews, Plotly, and Altair.
On the other hand, I would not recommend this book to someone with no
programming experience at all, but if you can do a little Python already and want to
improve by understanding how and why Python works the way it does, then this is
a good book for you.

vii
viii Preface

To get the most out of this book, open a Python interpreter and type-along with
the many code samples. I worked hard to ensure that all of the given code samples
work as advertised.

Acknowledgments I would like to acknowledge the help of Brian Granger and Fernando Perez,
two of the originators of the Jupyter Notebook, for all their great work, as well as the Python
community as a whole, for all their contributions that made this book possible. Hans Petter
Langtangen was the author of the Doconce [1] document preparation system that was used to
write this text. Thanks to Geoffrey Poore [2] for his work with PythonTeX and LATEX, both key
technologies used to produce this book.

San Diego, CA, USA José Unpingco


February, 2020

References

1. H.P. Langtangen, DocOnce markup language. https://github.com/hplgit/doconce


2. G.M. Poore, Pythontex: reproducible documents with latex, python, and more. Comput. Sci.
Discov. 8(1), 014010 (2015)
Contents

1 Basic Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Basic Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.2 Reserved Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.3 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.4 Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1.5 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.1.6 Loops and Conditionals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.1.7 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.1.8 File Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
1.1.9 Dealing with Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
1.1.10 Power Python Features to Master . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
1.1.11 Generators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
1.1.12 Decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
1.1.13 Iteration and Iterables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
1.1.14 Using Python Assertions to Pre-debug Code . . . . . . . . . . . . . . . 60
1.1.15 Stack Tracing with sys.settrace . . . . . . . . . . . . . . . . . . . . . . 61
1.1.16 Debugging Using IPython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
1.1.17 Logging from Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
2 Object-Oriented Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
2.1 Properties/Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
2.2 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
2.3 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
2.4 Class Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
2.5 Class Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
2.6 Static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
2.7 Hashing Hides Parent Variables from Children . . . . . . . . . . . . . . . . . . . . . . 76
2.8 Delegating Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
2.9 Using super for Delegation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
2.10 Metaprogramming: Monkey Patching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

ix
x Contents

2.11 Abstract Base Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80


2.12 Descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
2.13 Named Tuples and Data Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
2.14 Generic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
2.15 Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
2.15.1 Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
2.15.2 Singleton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
2.15.3 Observer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
2.15.4 Adapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
3 Using Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
3.1 Standard Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
3.2 Writing and Using Your Own Modules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
3.2.1 Using a Directory as a Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
3.3 Dynamic Importing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
3.4 Getting Modules from the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
3.5 Conda Package Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
4 Numpy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
4.1 Dtypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
4.2 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
4.3 Reshaping and Stacking Numpy Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
4.4 Duplicating Numpy Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
4.5 Slicing, Logical Array Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
4.6 Numpy Arrays and Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
4.7 Numpy Memory Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
4.8 Array Element-Wise Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
4.9 Universal Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
4.10 Numpy Data Input/Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
4.11 Linear Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
4.12 Broadcasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
4.13 Masked Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
4.14 Floating Point Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
4.15 Advanced Numpy dtypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
5 Pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
5.1 Using Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
5.2 Using DataFrame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
5.3 Reindexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
5.4 Deleting Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
5.5 Advanced Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
5.6 Broadcasting and Data Alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
5.7 Categorical and Merging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Contents xi

5.8 Memory Usage and dtypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143


5.9 Common Operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
5.10 Displaying DataFrames. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
5.11 Multi-index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
5.12 Pipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
5.13 Data Files and Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
5.14 Customizing Pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
5.15 Rolling and Filling Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
6 Visualizing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
6.1 Matplotlib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
6.1.1 Setting Defaults . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
6.1.2 Legends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
6.1.3 Subplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
6.1.4 Spines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
6.1.5 Sharing Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
6.1.6 3D Surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
6.1.7 Using Patch Primitives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
6.1.8 Patches in 3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
6.1.9 Using Transformations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
6.1.10 Annotating Plots with Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
6.1.11 Annotating Plots with Arrows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
6.1.12 Embedding Scalable and Non-scalable Subplots . . . . . . . . . . . 173
6.1.13 Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
6.1.14 Using Paths Directly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
6.1.15 Interacting with Plots Using Sliders . . . . . . . . . . . . . . . . . . . . . . . . 179
6.1.16 Colormaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
6.1.17 Low-Level Control Using setp and getp . . . . . . . . . . . . . . . 183
6.1.18 Interacting with Matplotlib Figures . . . . . . . . . . . . . . . . . . . . . . . . . 183
6.1.19 Keyboard Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
6.1.20 Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
6.2 Seaborn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
6.2.1 Automatic Aggregation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
6.2.2 Multiple Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
6.2.3 Distribution Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
6.3 Bokeh. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
6.3.1 Using Bokeh Primitives. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
6.3.2 Bokeh Layouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
6.3.3 Bokeh Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
6.4 Altair . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
6.4.1 Detailing Altair. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
6.4.2 Aggregations and Transformations. . . . . . . . . . . . . . . . . . . . . . . . . . 217
6.4.3 Interactive Altair . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
6.5 Holoviews . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
6.5.1 Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
xii Contents

6.5.2 Image Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232


6.5.3 Tabular Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
6.5.4 Customizing Interactivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
6.5.5 Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
6.5.6 Pandas Integration with hvplot . . . . . . . . . . . . . . . . . . . . . . . . . . 237
6.5.7 Network Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
6.5.8 Holoviz Panel for Dashboards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
6.6 Plotly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Chapter 1
Basic Programming

1.1 Basic Language

Before we get into the details, it is a good idea to get a high-level orientation to
Python. This will improve your decision-making later regarding software develop-
ment for your own projects, especially as these get bigger and more complex. Python
grew out of a language called ABC, which was developed in the Netherlands in the
1980s as an alternative to BASIC to get scientists to utilize microcomputers, which
were new at the time. The important impulse was to make non-specialist scientists
able to productively utilize these new computers. Indeed, this pragmatic approach
continues to this day in Python which is a direct descendent of the ABC language.
There is a saying in Python—come for the language, stay for the community.
Python is an open source project that is community driven so there is no corporate
business entity making top-down decisions for the language. It would seem that such
an approach would lead to chaos but Python has benefited over many years from
the patient and pragmatic leadership of Guido van Rossum, the originator of the
language. Nowadays, there is a separate governance committee that has taken over
this role since Guido’s retirement. The open design of the language and the quality
of the source code has made it possible for Python to enjoy many contributions from
talented developers all over the world over many years, as embodied by the richness
of the standard library. Python is also legendary for having a welcoming community
for newcomers so it is easy to find help online for getting started with Python.
The pragmatism of the language and the generosity of the community have long
made Python a great way to develop web applications. Before the advent of data
science and machine learning, upwards of 80% of the Python community were web
developers. In the last five years (at the time of this writing), the balance is tilted to
an almost even split between web developers and data scientists. This is the reason
you find a lot of web protocols and technology in the standard library.
Python is an interpreted language as opposed to a compiled language like C
or FORTRAN. Although both cases start with a source code file, the compiler

© The Editor(s) (if applicable) and The Author(s), under exclusive license 1
to Springer Nature Switzerland AG 2021
J. Unpingco, Python Programming for Data Analysis,
https://doi.org/10.1007/978-3-030-68952-0_1
2 1 Basic Programming

examines the source code end-to-end and produces an executable that is linked to
system-specific library files. Once the executable is created, there is no longer any
need for the compiler. You can just run the executable on the system. On the other
hand, an interpreted language like Python you must always have a running Python
process to execute the code. This is because the Python process is an abstraction on
the platform it is running on and thus must interpret the instructions in the source
code to execute them on the platform. As the intermediary between the source
code on the platform, the Python interpreter is responsible for the platform specific
issues. The advantage of this is that source code can be run on different platforms as
long as there is a working Python interpreter on each platform. This makes Python
source codes portable between platforms because the platform specific details are
handled by the respective Python interpreters. Portability between platforms was
a key advantage of Python, especially in the early days. Going back to compiled
languages, because the platform specific details are embedded in the executable, the
executable is tied to a specific platform and to those specific libraries that have been
linked into the executable. This makes these codes are less portable than Python,
but because the compiler is able to link to the specific platform it has the option
to exploit platform- specific level optimizations and libraries. Furthermore, the
compiler can study the source code file and apply compiler-level optimizations that
accelerate the resulting executable. In broad strokes, those are the key differences
between interpreted and compiled languages. We will later see that there are many
compromises between these two extremes for accelerating Python codes.
It is sometimes said that Python is slow as compared to compiled languages,
and pursuant to the differences we discussed above, that may be expected, but it
is really a question of where the clock starts. If you start the clock to account for
developer time, not just code runtime, then Python is clearly faster, just because
the development iteration cycle does not require a tedious compile and link step.
Furthermore, Python is just simpler to use than compiled languages because so
many tricky elements like memory management are handled automatically. Pythons
quick turnaround time is a major advantage for product development, which requires
rapid iteration. On the other hand, codes that are compute-limited and must run
on specialized hardware are not good use-cases for Python. These include solving
systems of parallel differential equations simulating large-scale fluid mechanics or
other large-scale physics calculations. Note that Python is used in such settings but
mainly for staging these computations or postprocessing the resulting data.

1.1.1 Getting Started

Your primary interface to the Python interpreter is the commandline. You can type
in python in your terminal you should see something like the following,
Python 3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0] :: Anaconda, Inc. on linux
1.1 Basic Language 3

Type "help", "copyright", "credits" or "license" for more


→ information.
>>>

There is a lot of useful information including the version of Python and its
provenance. This matters because sometimes the Python interpreter is compiled to
allow fast access to certain preferred modules (i.e., math module). We will discuss
this more when we talk about Python modules.

1.1.2 Reserved Keywords

Although Python will not stop you, do not use these as variable or function names.
and del from not while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue finally is return
def for lambda try

nor these neither


abs all any ascii bin bool breakpoint bytearray bytes callable
chr classmethod compile complex copyright credits delattr
dict dir display divmod enumerate eval exec filter float
format frozenset getattr globals hasattr hash help hex id
input int isinstance issubclass iter len list locals map max
memoryview min next object oct open ord pow print property
range repr reversed round set setattr slice sorted
staticmethod str sum super tuple type vars zip

For example, a common mistake is to assign sum=10, not realizing that now the
sum() Python function is no longer available.

1.1.3 Numbers

Python has common-sense number-handling capabilities. The comment character is


the hash (#) symbol.
>>> 2+2
4
>>> 2+2 # and a comment on the same line as code
4
>>> (50-5*6)/4
5.0

Note that division in Python 2 is integer-division and in Python 3 it is floating


point division with the // symbol providing integer-division. Python is dynamically
4 1 Basic Programming

typed, so we can do the following assignments without declaring the types of


width and height.
>>> width = 20
>>> height = 5*9
>>> width * height
900
>>> x = y = z = 0 # assign x, y and z to zero
>>> x
0
>>> y
0
>>> z
0
>>> 3 * 3.75 / 1.5
7.5
>>> 7.0 / 2 # float numerator
3.5
>>> 7/2
3.5
>>> 7 // 2 # double slash gives integer division
3

It is best to think of assignments as labeling values in memory. Thus, width is a


label for the number 20. This will make more sense later.1 Since Python 3.8, the
walrus assignment operator allows the assignment itself to have the value of the
assignee, as in the following,
>>> print(x:=10)
10
>>> print(x)
10

The operator has many other subtle uses and was introduced to improve readability
in certain situations. You can also cast among the numeric types in a common-sense
way:
>>> int(1.33333)
1
>>> float(1)
1.0
>>> type(1)
<class 'int'>
>>> type(float(1))
<class 'float'>

Importantly, Python integers are of arbitrary length and can handle extremely large
integers. This is because they are stored as a list of digits internally. This means that
they are slower to manipulate than Numpy integers which have fixed bit-lengths,
provided that the integer can fit into allocated memory.

1 Notehttp://www.pythontutor.com is a great resource for exploring how variables are assigned in


Python.
1.1 Basic Language 5

Programming Tip: IPython


The default Python interpreter is useful and you should be familiar with it
but the IPython interpretera is a useful extension that provides features like
sophisticated tab-completion and many other conveniences that make it easier
to work with Python in the terminal.
a
See http://github.com/ipython/ipython for the latest on how to get started with IPython.

1.1.4 Complex Numbers

Python has rudimentary support for complex numbers.


>>> 1j * 1J
(-1+0j)
>>> 1j * complex(0,1)
(-1+0j)
>>> 3+1j*3
(3+3j)
>>> (3+1j)*3
(9+3j)
>>> (1+2j)/(1+1j)
(1.5+0.5j)
>>> a=1.5+0.5j
>>> a.real # the dot notation gets an attribute
1.5
>>> a.imag
0.5
>>> a=3.0+4.0j
>>> float(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't convert complex to float
>>> a.real
3.0
>>> a.imag
4.0
>>> abs(a) # sqrt(a.real**2 + a.imag**2)
5.0
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> # the underscore character is the last evaluated result
>>> round(_, 2) # the underscore character is the last evaluated
→ result
113.06
6 1 Basic Programming

We typically use Numpy for complex numbers, though.

1.1.5 Strings

String-handling is very well developed and highly optimized in Python. We just


cover the main points here. First, single or double quotes define a string and there is
no precedence relationship between single and double quotes.
>>> 'spam eggs'
'spam eggs'
>>> 'doesn\'t' # backslash defends single quote
"doesn't"
>>> "doesn't"
"doesn't"
>>> '"Yes," he said.'
'"Yes," he said.'
>>> "\"Yes,\" he said."
'"Yes," he said.'
>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'

Python strings have C-style escape characters for newlinewsnewlines, tabs, etc.
String literals defined this way are by default encoded using UTF-8 instead of
ASCII, as in Python 2. The triple single (’) or triple double quote (") denotes a
block with embedded newlines or other quotes. This is particularly important for
function documentation docstrings that we will discussed later.
>>> print( '''Usage: thingy [OPTIONS]
... and more lines
... here and
... here
... ''')
Usage: thingy [OPTIONS]
and more lines
here and
here

Strings can be controlled with a character before the single or double quote. For
example, see the comments (#) below for each step,
>>> # the 'r' makes this a 'raw' string
>>> hello = r"This long string contains newline characters \n, as
→ in C"
>>> print(hello)
This long string contains newline characters \n, as in C
>>> # otherwise, you get the newline \n acting as such
>>> hello = "This long string contains newline characters \n, as
→ in C"
>>> print(hello)
This long string contains newline characters
, as in C
1.1 Basic Language 7

>>> u'this a unicode string μ ±' # the 'u' makes it a unicode


→ string for Python2
'this a unicode string μ ±'
>>> 'this a unicode string μ ±' # no 'u' in Python3 is still
→ unicode string
'this a unicode string μ ±'
>>> u'this a unicode string \xb5 \xb1' # using hex-codes
'this a unicode string μ ±'

Note that a f-string evaluates (i.e., interpolates) the Python variables in the
current scope,
>>> x = 10
>>> s = f'{x}'
>>> type(s)
<class 'str'>
>>> s
'10'

Beware that an f-string is not resolved until run-time because it has to resolve
the embedded variables. This means that you cannot use f-strings as docstrings.
Importantly, Python strings are immutable which means that once a string is created,
it cannot be changed in-place. For example,
>>> x = 'strings are immutable '
>>> x[0] = 'S' # not allowed!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

This means you have to create new strings to make this kind of change.
Strings vs Bytes In Python 3, the default string encoding for string literals is UTF-
8. The main thing to keep in mind is that bytes and strings are now distinct objects,
as opposed to both deriving from basestring in Python 2. For example, given
the following unicode string,
>>> x='Ø'
>>> isinstance(x,str) # True
True
>>> isinstance(x,bytes) # False
False
>>> x.encode('utf8') # convert to bytes with encode
b'\xc3\x98'

Note the distinction between bytes and strings. We can convert bytes to strings using
decode,
>>> x=b'\xc3\x98'
>>> isinstance(x,bytes) # True
True
>>> isinstance(x,str) # False
False
>>> x.decode('utf8')
'Ø'
8 1 Basic Programming

An important consequence is that you cannot append strings and bytes as in the
following: u"hello"+b"goodbye". This used to work fine in Python 2 because
bytes would automatically be decoded using ASCII, but this no longer works in
Python 3. To get this behavior, you have to explicitly decode/encode. For
example,
>>> x=b'\xc3\x98'
>>> isinstance(x,bytes) # True
True
>>> y='banana'
>>> isinstance(y,str) # True
True
>>> x+y.encode()
b'\xc3\x98banana'
>>> x.decode()+y
'Øbanana'

Slicing Strings Python is a zero-indexed language (like C). The colon (:) character
denotes .
>>> word = 'Help' + 'A'
>>> word
'HelpA'
>>> '<' + word*5 + '>'
'<HelpAHelpAHelpAHelpAHelpA>'
>>> word[4]
'A'
>>> word[0:2]
'He'
>>> word[2:4]
'lp'
>>> word[-1] # The last character
'A'
>>> word[-2] # The last-but-one character
'p'
>>> word[-2:] # The last two characters
'pA'
>>> word[:-2] # Everything except the last two characters
'Hel'

String Operations Some basic numerical operations work with strings.


>>> 'hey '+'you' # concatenate with plus operator
'hey you'
>>> 'hey '*3 # integer multiplication duplicates strings
'hey hey hey '
>>> ('hey ' 'you') # using parentheses without separating comma
'hey you'

Python has a built-in and very powerful regular expression module (re) for string
manipulations. String substitution creates new strings.
>>> x = 'This is a string'
>>> x.replace('string','newstring')
'This is a newstring'
>>> x # x hasn't changed
'This is a string'
1.1 Basic Language 9

Formatting Strings There are so many ways to format strings in Python, but here
is the simplest that follows C-language sprintf conventions in conjunction with
the modulo operator %.
>>> 'this is a decimal number %d'%(10)
'this is a decimal number 10'
>>> 'this is a float %3.2f'%(10.33)
'this is a float 10.33'
>>> x = 1.03
>>> 'this is a variable %e' % (x) # exponential format
'this is a variable 1.030000e+00'

Alternatively, you can just join them using +,


>>> x = 10
>>> 'The value of x = '+str(x)
'The value of x = 10'

You can format using dictionaries as in the following,


>>> data = {'x': 10, 'y':20.3}
>>> 'The value of x = %(x)d and y = %(y)f'%(data)
'The value of x = 10 and y = 20.300000'

You can use the format method on the string,


>>> x = 10
>>> y = 20
>>> 'x = {0}, y = {1}'.format(x,y)
'x = 10, y = 20'

The advantage of format is you can reuse the placeholders as in the following,
>>> 'x = {0},{1},{0}; y = {1}'.format(x,y)
'x = 10,20,10; y = 20'

And also the f-string method we discussed above.

Programming Tip: Python 2 Strings


In Python 2, the default string encoding was 7-bit ASCII. There was no
distinction between bytes and strings. For example, you could read from a
binary-encoded JPG file as in the following,
with open('hour_1a.jpg','r') as f:
x = f.read()

This works fine in Python 2 but throws a UnicodeDecodeError error in


Python 3. To fix this in Python 3, you have to read using the rb binary mode
instead of just the r file mode.

Basic Data Structures Python provides many powerful data structures. The two
most powerful and fundamental are the list and dictionary. Data structures and
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
10 1 Basic Programming

algorithms go hand-in-hand. If you do not understand data structures, then you


cannot effectively write algorithms and vice versa. Fundamentally, data structures
provide guarantees to the programmer that will be fulfilled if the data structures are
used in the agreed-upon manner. These guarantees are known as the invariants for
the data structure.
Lists The list is an order-preserving general container that implements the
sequence data structure. The invariant for the list is that indexing a non-empty list
will always give you the next valid element in order. Indeed, the list is Python’s
primary ordered data structure. This means that if you have a problem where order
is important, then you should be thinking about the list data structure. This will
make sense with following examples.
>>> mix = [3,'tree',5.678,[8,4,2]] # can contain sublists
>>> mix
[3, 'tree', 5.678, [8, 4, 2]]
>>> mix[0] # zero-indexed Python
3
>>> mix[1] # indexing individual elements
'tree'
>>> mix[-2] # indexing from the right, same as strings
5.678
>>> mix[3] # get sublist
[8, 4, 2]
>>> mix[3][1] # last element is sublist
4
>>> mix[0] = 666 # lists are mutable
>>> mix
[666, 'tree', 5.678, [8, 4, 2]]
>>> submix = mix[0:3] # creating sublist
>>> submix
[666, 'tree', 5.678]
>>> switch = mix[3] + submix # append two lists with plus
>>> switch
[8, 4, 2, 666, 'tree', 5.678]
>>> len(switch) # length of list is built-in function
6
>>> resize=[6.45,'SOFIA',3,8.2E6,15,14]
>>> len(resize)
6
>>> resize[1:4] = [55] # assign slices
>>> resize
[6.45, 55, 15, 14]
>>> len(resize) # shrink a sublist
4
>>> resize[3]=['all','for','one']
>>> resize
[6.45, 55, 15, ['all', 'for', 'one']]
>>> len(resize)
4
>>> resize[4]=2.87 # cannot append this way!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> temp = resize[:3]
1.1 Basic Language 11

>>> resize = resize + [2.87] # add to list


>>> resize
[6.45, 55, 15, ['all', 'for', 'one'], 2.87]
>>> len(resize)
5
>>> del resize[3] # delete item
>>> resize
[6.45, 55, 15, 2.87]
>>> len(resize) # shorter now
4
>>> del resize[1:3] # delete a sublist
>>> resize
[6.45, 2.87]
>>> len(resize) # shorter now
2

Programming Tip: Sorting Lists


The built-in function sorted sorts lists,
>>> sorted([1,9,8,2])
[1, 2, 8, 9]

Lists can also be sorted in-place using the sort() list method,
>>> x = [1,9,8,2]
>>> x.sort()
>>> x
[1, 2, 8, 9]

Both of these use the powerful Timsort algorithm. Later, we will see more
variations and uses for these sorting functions.

Now that we have a feel for how to index and use lists, let us talk about the invariant
that it provides: as long as you index a list within its bounds, it provides the next
ordered element of the list. For example,
>>> x = ['a',10,'c']
>>> x[1] # return 10
10
>>> x.remove(10)
>>> x[1] # next element
'c'

Notice that the list data structure filled in the gap after removing 10. This is extra
work that the list data structure did for you without explicitly programming. Also,
list elements are accessible via integer indices and integers have a natural ordering
and thus so does the list. The work of maintaining the invariant does not come for
free, however. Consider the following,
>>> x = [1,3,'a']
>>> x.insert(0,10) # insert at beginning
12 1 Basic Programming

>>> x
[10, 1, 3, 'a']

Seem harmless? Sure, for small lists, but not so for large lists. This is because
to maintain the invariant the list has to scoot (i.e., memory copy) the remaining
elements over to the right to accommodate the new element added at the beginning.
Over a large list with millions of elements and in a loop, this can take a substantial
amount of time. This is why the default append() and pop() list methods work
at the tail end of the list, where there is no need to scoot items to the right.
Tuples Tuples are another general purpose sequential container in Python, very
similar to lists, but these are immutable. Tuples are delimited by commas (parenthe-
ses are grouping symbols). Here are some examples,
>>> a = 1,2,3 # no parenthesis needed!
>>> type(a)
<class 'tuple'>
>>> pets=('dog','cat','bird')
>>> pets[0]
'dog'
>>> pets + pets # addition
('dog', 'cat', 'bird', 'dog', 'cat', 'bird')
>>> pets*3
('dog', 'cat', 'bird', 'dog', 'cat', 'bird', 'dog', 'cat', 'bird')
>>> pets[0]='rat' # assignment not work!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

It may seem redundant to have tuples which behave in terms of their indexing like
lists, but the key difference is that tuples are immutable, as the last line above shows.
The key advantage of immutability is that it comes with less overhead for Python
memory management. In this sense, they are lighter weight and provide stability for
codes that pass tuples around. Later, we will see this for function signatures, which
is where the major advantages of tuples arise.

Programming Tip: Understanding List Memory


Python’s id function shows an integer corresponding to the internal reference
for a given variable. Earlier, we suggested considering variable assignment as
labeling because internally Python works with a variable’s id, not its variable
name/label.
>>> x = y = z = 10.1100
>>> id(x) # different labels for same id
140271927806352
>>> id(y)
140271927806352
>>> id(z)
140271927806352

(continued)
1.1 Basic Language 13

This is more consequential for mutable data structures like lists. Consider the
following,
>>> x = y = [1,3,4]
>>> x[0] = 'a'
>>> x
['a', 3, 4]
>>> y
['a', 3, 4]
>>> id(x),id(y)
(140271930505344, 140271930505344)

Because x and y are merely two labels for the same underlying list, changes to
one of the labels affects both lists. Python is inherently stingy about allocating
new memory so if you want to have two different lists with the same content,
you can force a copy as in the following,
>>> x = [1,3,4]
>>> y = x[:] # force copy
>>> id(x),id(y) # different ids now!
(140271930004160, 140271929640448)
>>> x[1] = 99
>>> x
[1, 99, 4]
>>> y # retains original data
[1, 3, 4]

Tuple Unpacking Tuples unpack assignments in order as follows:,


>>> a,b,c = 1,2,3
>>> a
1
>>> b
2
>>> c
3

Python 3 can unpack tuples in chunks using the * operator,


>>> x,y,*z = 1,2,3,4,5
>>> x
1
>>> y
2
>>> z
[3, 4, 5]

Note how the z variable collected the remaining items in the assignment. You can
also change the order of the chunking,
>>> x,*y,z = 1,2,3,4,5
>>> x
1
14 1 Basic Programming

>>> y
[2, 3, 4]
>>> z
5

This unpacking is sometimes called de-structuring, or splat, in case you read this
term elsewhere.
Dictionaries Python dictionaries are central to Python because many other ele-
ments (e.g., functions, classes) are built around them. Effectively programming
Python means using dictionaries effectively. Dictionaries are general containers that
implement the mapping data structure, which is sometimes called a hash table or
associative array. Dictionaries require a key/value pair, which maps the key to the
value.
>>> x = {'key': 'value'}

The curly braces and the colon make the dictionary. To retrieve the value from the
x dictionary, you must index it with the key as shown,
>>> x['key']
'value'

Let us get started with some basic syntax.


>>> x={'play':'Shakespeare','actor':'Wayne','direct':'Kubrick',
... 'author':'Hemmingway','bio':'Watson'}

>>> len(x) # number of key/value pairs


5
>>> x['pres']='F.D.R.' # assignment to key 'pres'
>>> x
{'play': 'Shakespeare', 'actor': 'Wayne', 'direct': 'Kubrick',
→ 'author': 'Hemmingway', 'bio': 'Watson', 'pres': 'F.D.R.'}
>>> x['bio']='Darwin' # reassignment for 'bio' key
>>> x
{'play': 'Shakespeare', 'actor': 'Wayne', 'direct': 'Kubrick',
→ 'author': 'Hemmingway', 'bio': 'Darwin', 'pres': 'F.D.R.'}
>>> del x['actor'] # delete key/value pair
>>> x
{'play': 'Shakespeare', 'direct': 'Kubrick', 'author':
→ 'Hemmingway', 'bio': 'Darwin', 'pres': 'F.D.R.'}

Dictionaries can also be created with the dict built-in function,


>>> # another way of creating a dict
>>> x=dict(key='value',
... another_key=1.333,
... more_keys=[1,3,4,'one'])
>>> x
{'key': 'value', 'another_key': 1.333, 'more_keys': [1, 3, 4,
→ 'one']}
>>> x={(1,3):'value'} # any immutable type can be a valid key
>>> x
{(1, 3): 'value'}
>>> x[(1,3)]='immutables can be keys'
Random documents with unrelated
content Scribd suggests to you:
NARRATIVE
OF AN
EXPEDITION TO SURINAM.
CHAP. I.

Introduction.—Revolt among the Negroes in Dutch Guiana—An Expedition


sets out from the Texel—Short Account of the Voyage—The Fleet arrives in
the River Surinam—Reception of the Troops in that Colony—Sketch of the
Inhabitants, &c.

The exploring of foreign countries having of late years, and


particularly since the recent discoveries of the immortal Captain Cook,
so generally been the object of persons both in private and public
situations; and the histories of their labours and pursuits being so
interesting to the curiosity of the public, I have ventured to offer
such observations as I have had an opportunity of making in a very
singular part of the globe, on which few Englishmen have been
thrown, either by accident [2]or curiosity. The colony of Surinam, in
Dutch Guiana, so far as it is inhabited and cultivated by Europeans
near the sea-coast, has indeed been known for many years past. But
the deep inundations, with the impenetrable thickness of the woods,
have been such constant discouragements and obstructions to
discovery, that but very little true information concerning that
country has as yet been obtained, except what relates to such
objects of commerce as are common to most of the tropical
settlements. This publication, therefore, is chiefly intended to
particularize such circumstances and events as the necessity of
penetrating into the interior parts of the country have enabled me to
make, and forced on my observation.

The feeling part of my readers, I must hope, will receive with some
indulgence a work proceeding from an officer, who, from his early
youth, was debarred in acquiring perfection, either as a writer or a
painter, by his military and maritime profession. I nevertheless
humbly flatter myself that whatever may be found wanting in style
and elegance, is in some degree compensated by that fidelity and
correctness, which can alone be the work of a pen and pencil
employed on the spot. With respect to a few quaint expressions, and
even oaths, as spoken by common soldiers, sailors, &c. that
sometimes unavoidably occur in the narrative of this wonderful
expedition, I must humbly request the world not to be startled at
them, not only because the shades of black and white [3]enliven the
picture, but because I am determined to write truth only, and expose
vice and folly in their native colours.—Come then, my friends—

“Together let us beat this ample field,


Try what the open, what the covert yield;
The latent tracts, the giddy heights, explore
Of all who blindly creep, or sightless soar:
Eye Nature’s walks, shoot Folly as it flies,
And catch the manners living as they rise;
Laugh where we must, be candid where we can;
But vindicate the ways of God to man.”

Pope.

I will now boldly launch out on the difficult task.—As the nature,
however, of these transactions can only be understood by a
reference to the occasion which called me thither, I feel myself under
the necessity of still premising a few words upon that subject.

Every part of the world, where domestic slavery is established, may


be occasionally liable to insurrection and disquiet, more especially
where the slaves constitute the majority of the inhabitants; but the
colony of Surinam, in Dutch Guiana, has been peculiarly unfortunate
in this respect. Whether from the shelter which is afforded to the
fugitives by the immense forests which overspread the most
considerable part of this country, or whether the government of this
settlement be radically defective, it is a certain fact, that its
European settlers [4]are constantly exposed to the most violent
ravages, and the most desperate outrage. Of these circumstances
this is not however the place for a minute detail. Let it suffice
therefore for the present, only to observe, that these repeated
revolts and insurrections demanded at length the most vigorous
measures for the restoration of a general peace; and that the
accounts transmitted to Holland, in the year 1772, that a
considerable body of armed people of this description had
assembled in the forests, and became extremely formidable to the
colony, determined their High Mightinesses the States of the United
Provinces to send out a sufficient maritime force to oppose the
insurgents, and, if possible, to quell the insurrection.

The British navy had ever been my choice and ambition, in which I
was well recommended; but the small hopes of preferment I had
naturally to expect in time of peace, and my paternal estate being
lost just after my birth by accidental misfortunes, induced me to
relinquish the hopes of advancement in the sea service, and to
accept an ensign’s commission, presented me without purchase, in
one of the Scots brigade regiments in the pay of Holland, where Sir
Joseph York (late Lord Dover) at that period was ambassador from
the British court; before which nobleman I had the honour to take
the usual oaths of abjuration and allegiance to my King and Country,
as registered at the British war-office.—This point I have also
thought it right to premise, as [5]a duty owing to myself, to shew the
world in general that it was necessity not choice that compelled me
to enter into a foreign service; though perhaps a more ancient and
distinguished corps does not exist than the above brigade has
proved to be, both in this island and on the continent, for above two
hundred years.

At the time of the above insurrection I was Lieutenant in the


Honourable General John Stuart’s regiment; when, impressed by the
hopes of traversing the sea, my favourite element, and in some
measure gratifying my curiosity, in exploring a part of the world not
generally known; still more by the prospect of that preferment which
might be consequent on so dangerous an expedition; I instantly
solicited admission into a corps of volunteers which was preparing to
sail for Guiana, and had the honour, by his Serene Highness William
V. Prince of Orange, to be advanced to the rank of Captain by brevet 1,
under Colonel Louïs Henry Fourgeoud, a Swiss gentleman, from the
Alpine Mountains, who was appointed our commander in chief.

Having taken the oaths of fidelity on the 12th of November to the


new corps, and prepared what was necessary for the voyage, I bade
farewell to my old regiment, and immediately sailed to the island of
Texel, where several of our gentlemen were already assembled;
[6]and where, on going ashore, I had nearly perished by the boat’s
shipping a sea, and sinking in the surf.

The island of Wieringen was however the spot of general


rendezvous: here Colonel Fourgeoud arriving on the 7th of
December, the volunteers were all assembled, to the number of five
hundred fine young men; and on the morning of the 8th we were
formed into seven companies, and embodied as a regiment of
marines. Besides the Boreas and Westellingwerf men-of-war,
commanded by Captains Van de Velde and Crass, three new frigate-
built transports were put in commission, carrying ensign, jack, and
pennant, and armed with from ten to sixteen guns, as sloops of war;
on board these vessels we embarked the same afternoon under a
general salute, then took the command, and did the duty as in the
navy.

Our departure was not however immediately consequent on our


embarkation. We lay wind-bound in the Texel roads for many days,
during which time one of our young officers, a Mr. Hesseling, was
unfortunately seized with the small-pox: this gentleman, in order to
prevent his infecting the ship’s company, was ordered on shore to a
town on the land’s end, called the Helder, where I conducted him in
a pinnace, and where we left him behind us; but on my return, the
surgeon declaring he saw the symptoms of the same disorder on
myself, I was also immediately ordered to the island of Texel. Having
passed a most anxious quarantine in this place, I had [7]the good
fortune however to escape the loathsome malady, and to the
Doctor’s surprize appeared once more on board perfectly well, just
before the signal gun was fired for the fleet to weigh anchor. This
circumstance cannot but induce me to wish that those in particular
who are destined for a military or a naval life would avail themselves
of the art of inoculation, in order to avoid a painful anxiety to
themselves, and a most dangerous infection to their fellow-
creatures.

On Christmas-day our small fleet put to sea, at eight o’clock, A. M.


with a fresh breeze from E. N. E. in company with above one
hundred vessels bound for different parts of the globe, and the most
beautiful clear weather. Having safely got without the soundings,
and saluted each other with nine guns, we kept our course down
channel, and soon passed the North Foreland, the Isle of Wight, and
Portland Point; but here the Westellingwerf, having sprung a leak,
was obliged to part company, and run into Plymouth for repair.

The wind now freshened as we approached the Bay of Biscay, where


the mate of the vessel directed my particular attention to a kind of
sea-swallow, commonly distinguished by the name of the Storm-
bird 2, from its supposed property of foretelling an impending
tempest. The colour of this bird is a very deep blue approaching to
black, and enlivened by some variegated tints; its size is about
[8]that of a large martin or swallow; it is web-footed; the bill is very
long and sharp, and the wings of an extraordinary length, which
enable it to fly very fast, and for a considerable time, skimming with
incredible velocity around the horizon: it subsists entirely upon fish,
which is probably the cause of its being sensible of the first
indications of whatever may prevent its usual supply of food: it then
flies along with extreme swiftness, in order to avoid the storm; but if
overtaken by it, drops its wings, and floats upon the surface of the
waves.

On the following day, January 2d, 1773, the predictions of the


storm-bird were verified: a heavy gale sprung up from N. N. E. by
which, off Cape Finisterre, the Boreas and Vigilance were separated
from us. We kept our course during the night with double reefed
topsails, and all the hatches laid, which made our men in general
very sick. Here I ought not to forget that by way of experiment we
had slung the hammocks athwart ships, and not as usual fore and
aft; which method we found however to be both so roomy and
convenient, that it has been since adopted by several other vessels.

On the morning of the 4th we spied a stout ship to windward in the


offing, bearing streight down upon us. Conjecturing she might be an
Algerine pirate from the African coast, and now but two ships out of
five, we prepared to engage her; she however soon proved to be the
Boreas man-of-war, which had parted company on the 2d. From this
date the men were daily exercised at [9]the great guns, and by firing
at a target suspended from the yard-arm.

On the 14th, in the morning-watch, we passed the Tropic, when the


usual ceremony of ducking the fresh-water sailors was ransomed by
tipping the foremast men with some silver. About this time the
Boreas most unluckily lost one of her best seamen, the boatswain’s
mate, whose hand slipping by the wet, he pitched from the fore-
yard-arm into the sea. His presence of mind in calling to the captain,
as he floated alongside, “Be not alarmed for me, sir,” in the
confidence of meeting with relief, attracted peculiar compassion, and
even caused some murmuring, as no assistance was offered him; in
consequence of which, after swimming a considerable time within
view, the unfortunate young man went to the bottom.

We now were got in the tract of the trade winds, which blowing
continually east, and the weather becoming from day to day more
temperate, made the voyage exceedingly pleasant; more so by the
many dolphins or dorados, which beautiful fish seem to take peculiar
delight in sporting around the vessels. The real dolphin, which is of
the cetaceous kind, was anciently celebrated in poetic story on
account of its philanthropy and other supposed virtues: but to the
dorado or dolphin of the moderns, this character is far from being
applicable, this fish being extremely voracious and destructive, and
is known to follow the ships, and exhibit his sports and gambols, not
from attachment to mankind, but from the more selfish [10]motive of
procuring food, particularly on the eve of an approaching storm, of
which he appears perfectly sensible. The circumstance which chiefly
entitles the dorado to our attention is, the unrivalled and dazzling
brilliancy of its colours in the water, the whole of its back being
enamelled with spots between azure blue and a reflecting light sea-
green, on a very dark ground, which appears as bespangled all over
with jewels, and forms a most beautiful contrast to the belly, which
is of a whitish cast; the fins and tail are of a golden dye: the length
of this animal is from five to six feet, and its form tapers from the
head towards the tail, which is divided, and terminates not unlike
the shape of a crescent. The head is round, and preceded by a kind
of snout; the jaws are armed with several sharp teeth, and the eyes
are remarkably large. The scales of the dorado are uncommonly
small, and it is furnished with a fin, which runs along its back from
the one extremity to the other.
Our progress was now daily marked by increasing warm weather,
which released me from the confinement of a disagreeable cabin
crowded with officers, most of whom had never been to sea, and
enabled me to pursue my favourite amusements, whether of reading
above deck, or exercise in the rigging. Thus circumstanced I, on the
17th, had the happiness of rendering a most important service to
one of our young officers, a Mr. du Moulin, who by a sudden roll of
the vessel was actually thrown over the gunwale; at that moment
happening to stand [11]without-board in the main-chains, I
fortunately grasped hold of him in his fall, which saved him, as he
could not swim, from inevitable death.
The Harangus Volans, or Flying Fish.
The Dorado, or Dolphin of the Moderns.

London. Published, Decr. 1st 1791 by J. Johnson, St. Paul’s Church


Yard.
The entrance into warmer regions gave occasion to an observation
perhaps not generally known, which (though uncouth) must be of
great importance to sailors; namely, that between the Tropics, while
vermin may remain in the head, none can possibly continue to exist
in the bedding, cloaths, linen, &c. Having humbly apologized for the
above remark to my delicate readers, I will endeavour to describe a
curious animal with which these seas abound, and which appears to
sail on the surface of the waves with a side-wind, while by the
sailors it is vulgarly called a Portuguese man-of-war, and is probably
either the Nautilus or the Argonauta of Linnæus. This wonderful
creature, when above water, assumes the shape of an expanded fan,
decorated with a beautiful red border, while the lower extremity is
fixed to a shell as thin as paper, or rather a kind of boat, which is
sunk below or raised above the surface of the sea, and guided in any
direction, at the pleasure of the animal, by means of six tentacula or
limbs, which it uses as oars. When these creatures are touched by
the hand, they occasion, like the sea blubber or jelly fish, a painful
tinkling sensation, which continues for several minutes.

The two following days it blew very fresh, and heavy seas washed
over the vessel; during which, while helping to put a reef in the
main-top-sail for a little exercise, I lost [12]every one of my keys,
which dropped from the yardarm into the sea. This trifling accident I
should never have related, had it not proved a very great
inconvenience, by debarring me from coming at my private property,
particularly since the whole ship’s company, officers included, lived
on salt provision alone, a pig and a couple of lean sheep excepted,
whose legs had been broken by the rolling and pitching of the
vessel. This manner of living on salt-beef, pork, and peas, like
common sailors, was introduced by our commander in chief, in order
to enure us (he said) to such food as we were likely to be alone
supplied with in the woods of Surinam; and from the generous
motive of regaling his American friends with European refreshments
—such as live sheep, hogs, fowls, ducks, bacon hams, bullocks
tongues, preserved vegetables, pickles, spices, &c. all of which were
provided by the town of Amsterdam in profusion. But good
intentions do not always meet with their rewards; since the worms,
without any one’s permission, laid hold of the greatest part of the
dead stock for themselves; who were, for a punishment, together
with their plunder, thrown overboard into the ocean. Let me add,
that instead of plate, our meals were frequently served up in small
wooden tubs of not the most cleanly appearance, and only once a
day; which negligence, however, I am willing to impute to Monsieur
Laurant, the colonel’s French valet-de-chambre. In short, the scurvy
and other loathsome disorders began to make their appearance;
dejection and low-spirits took place throughout the [13]ship, while I
complained aloud, and from that moment date the good-will which
Colonel Fourgeoud manifested towards me in particular, as will be
seen throughout the expedition. It is with pain that I relate this
passage—but no consideration shall prevent me from bringing to
light particular foibles, as it will ever give me the greatest pleasure
to render virtue conspicuous.

About the 20th of January, we observed great numbers of flying fish,


the exocœtus volitans of Linnæus, which is about the size of a
herring. The back of this animal is flat, and of an olive-brown colour;
the sides and belly of a resplendent silvery white; the mouth small;
the eyes large; the tail bifurcated; and the scales hard, smooth, and
silvery. Upon occasion the pectoral fins are used as wings by this
fish, yet no longer than while they continue wet, for as soon as the
moisture is dried, the animal drops back into the sea. The surface of
these fins is of a golden hue, beautifully variegated near the edges
with spots of azure blue: their length is equal to that of the body of
the fish; and its flight, which is undertaken in order to avoid the
pursuit of the dorado, and other large fish, is always straight
forward, and of short duration, on account of the necessity of
repeatedly wetting its wings 3. These animals are frequently [14]found
on board vessels, and sticking in the shrouds, which is probably to
be ascribed, not, according to the opinion of some, to their seeking a
refuge there from the attacks of fish or sea-birds, but simply to their
flight being obstructed by an object, which, as they always fly in a
direct line, they have not the power to avoid. The fate of this animal
seems peculiarly severe, at it is the prey both of the scaly and
feathered creation, and frequently meets its doom in that element to
which but a moment before it had committed itself for protection.

Becoming extremely low-spirited towards the close of our voyage, I


now had recourse to daily sea-bathing, and to a chearing glass of
claret, two ankers of which had been provided for each officer,
independantly of his own stock. These means proved efficacious,
and I found myself in a few days perfectly recovered from my
complaint. On the 30th the weather became hazy, when the ships
brought-to and hove the lead in thirteen fathom foul water. The
following day we passed several large black rocks to windward,
called the Constables, and cast anchor near the Euripice, or Devil’s
Islands, off the coast of South America. The Euripice Islands are
situated about twenty-four miles from the French settlement of
Cayenne, bearing N. N. W. in north latitude, five degrees twenty
minutes, and consist of a ridge of small uninhabited and very
dangerous rocks for shipping. Here the current runs constantly from
the S. E. to the N. W. at the rate of sixty English miles in twenty-four
hours: [11*]consequently every vessel which happens to pass the
mouth of the river Surinam must make a considerable circuit in order
to regain the possibility of entering that river.

While we remained in this situation, we observed the narwhal, or


sea-unicorn, and one or two large turtles, floating past the ship’s
side. The former of these is a large fish, and very distinguishable by
a long spiral excrescence on its nose, like a tapering twisted rope.
The one we saw at this time (though some are said to be forty or
fifty) appeared but six or eight feet in length, and its horn about
four, which weapon is dreadfully offensive to many fishes, especially
to the whale; and when polished (either in hardness or whiteness) is
considered not to be inferior to ivory. The narwhal, which is of the
cetaceous kind, and consequently viviparous, is more frequently
found in cold than warm climates. The female is said to be
unprovided with that protuberance so remarkable in the male. It
appears that some authors have confounded this animal with the
sword-fish, to which however it does not prove to have the very
smallest resemblance.

Another animal, which is called the saw-fish, carries also an


offensive weapon. The projecting bone of this is three or four feet
long, flat, and both sides armed with strong sharp-pointed spikes,
which give it somewhat the form of a saw; this saw, which is
covered over with the same rough, slimy, darkish-coloured skin that
covers [12*]the whole animal, begins to spread itself near the eyes,
and thus continues spreading till it forms the head of a flattish
triangular appearance, close to which are the two pectoral fins.
Above the eyes are two large holes, which I apprehend to be the
organs of hearing, and not, as some suppose, intended by nature for
the purpose of spouting water. Almost directly under them is the
mouth, which is something in the form of an half-moon, apparently
without teeth, and between that and the under part of the spiked
saw are the nostrils. The body of the saw-fish is not much larger
than the head, with two strong dorsal fins, the one near the middle,
the other near the tail, which is partly bifurcated, and raised
perpendicular, the largest part upwards, without rays. The back is
covered over with a dark slimy skin; the whole forming a very
hideous appearance. This fish fights with the largest whales, till the
sea all around is died with blood, seldom quitting its adversary till it
has vanquished and killed it. I have seen this monster out of the
water, and its whole length measured about fourteen feet.

View of the Constable Rocks off Cayenne, from N. E.


The Saw-Fish, & part of the Head reversed.
London, Published Decr. 1st. 1791, by J. Johnson, St. Paul’s Church
Yard.

The turtles are divided into two species, and are generally
distinguished in Surinam by the names of calapee or green turtle,
and carett. The former of these sometimes weighs four hundred
pounds, and has a flattish shell; but the carett is inferior both in size
and quality, except with respect to its shell, which is more valuable,
and of a more convex form. Both the calapee and carett deposit
their eggs, which are very excellent food, in the sand, [13*]where
they are hatched by the heat of the sun. The manner of taking these
animals is by turning them on their backs with a hand-spike when
they are discovered on shore, and leaving them in this situation till a
convenient opportunity occurs for carrying them away; for such is
the heaviness of their structure, or so languid are their powers, that
they are utterly unable to turn themselves, and effect their escape.
They are publickly exposed to sale by the butchers in Surinam, like
the shambles meat in the European markets, and are esteemed the
most delicate food between the months of February and May.

On the morning of the 1st of February we now once more went


under way, and kept course in shore till the evening, when we came
to an anchor off the mouth of the river Marawina; this river has
occasioned the loss of many ships, by seamen fatally mistaking it for
the river Surinam, to which its entry bears indeed a very great
resemblance. What renders the first so dangerous, are the numerous
rocks, small islands, and quick-sands with which it is crouded;
besides its being so shallow at high-water mark (and even with
spring-tides) that all ships of any considerable burthen immediately
run a-ground, and go to pieces.

On the 2d, having got our anchor a-peak by daybreak, we again set
sail, keeping course along the coast; when having doubled Braam’s
Point with a light breeze, under top and top-gallant sails, we finally
entered the beautiful river Surinam; and at three o’clock, P.M.
dropped anchor before the new fortress called Amsterdam; [14*]and
here we were extremely happy to meet with our friends in the
Vigilance, which vessel (as I have mentioned) had parted company
with us in a gale of wind on the 2d of January, off Cape Finisterre,
and arrived two days before us in this river.

Our ships crews now were in the highest flow of spirits, seeing
themselves surrounded by the most delightful verdure, while the
river seemed alive by the many boats and barges passing and re-
passing to see us, while groups of naked boys and girls were
promiscuously playing and flouncing, like so many Tritons and
Mermaids, in the water. The scene was new to all, and nothing was
heard but music, singing, and cheering on deck, as well as in the
rigging, from the ideas of happiness which each individual now
promised himself in this luxuriant flourishing spot, while between
decks the heat was become insupportable: but how miserably these
poor fellows were mistaken in their reckoning shall soon be seen.

I must indeed acknowledge that nothing could equal the delicious


sensations with which we seemed intoxicated by the fragrance of the
lemons, limes, oranges, and flowers, wafted over from the adjoining
plantations that line the banks of all the rivers in this ever-blooming
settlement, and of which charming fruit, &c. large clusters were sent
on board our ships by Colonel de Ponchera of the colonial troops;
this gentleman, being the commandant of Fort Amsterdam, also
saluted the vessels with nine guns from the batteries, while with an
equal number we returned him the compliment [15]from the ships. A
long-boat, with one of our captains, was afterwards dispatched to
Paramaribo, to announce to the governor the arrival of the troops in
the colony.
Bartolozzi Sculpt.
A Female Negro Slave, with a Weight chained to her Ancle.

London, Published Decr. 1st, 1795, by J. Johnson, St. Paul’s


Church Yard.
During our stay in this place the companies frequently walked on
shore, and I accompanied them in their excursions; but the pleasure
I had flattered myself with, from exchanging the confinement of a
ship for the liberty of ranging over a delicious country, was damped
by the first object which presented itself after my landing. This was
a young female slave, whose only covering was a rag tied round her
loins, which, like her skin, was lacerated in several places by the
stroke of the whip. The crime which had been committed by this
miserable victim of tyranny, was the non-performance of a task to
which she was apparently unequal, for which she was sentenced to
receive two hundred lashes, and to drag, during some months, a
chain several yards in length, one end of which was locked round
her ancle, and to the other was affixed a weight of at least a
hundred pounds. Strongly affected with this shocking circumstance,
I took a draft of the unhappy sufferer, and retained a dreadful idea
of the inhumanity of the planters towards these miserable subjects
to their power.

The grass in this part of the country was very long and coarse, and
afforded a harbour to two species of very disagreeable insects,
termed Pattat and Scrapat lice by the colonists, which settled on
every part of our persons. [16]The former of these is so small as to
be scarcely visible; the latter is something larger, and formed like a
crab, and both agree in adhering closely to the skin, and occasioning
an intolerable itching. These insects abound most during the rainy
season, when the best means of avoiding their attacks is supposed
to be by walking barefoot, as they are believed to fasten more easily,
and consequently in greater numbers, upon the cloaths, whence
however they very speedily find their way to the skin. We did not get
rid of our disagreeable companions till our return to the ship, when
we washed the affected parts with the juice of limes or lemons,
which considerably alleviated our troublesome sensations.
On the 3d of March we received a visit from several officers of the
Society, or West India Company’s troops, accompanied by a number
of other gentlemen, to welcome our arrival in the colony. Nor were
they satisfied with paying us merely a compliment in words, but
regaled us with a large quantity of excellent fruits and other
refreshments. They came in very elegant barges or tent-boats,
adorned with flags, and attended by small bands of music. The
vessels were rowed by six or eight negroes, who were entirely
without cloaths, except a small stripe of check or other linen cloth,
which was passed between their thighs, and fastened before and
behind to a thin cotton string tied round their loins. As the colonists
generally make choice of their handsomest slaves for this office, and
to attend them at table, &c. [17]the rowers, who were healthy,
young, and vigorous, looked extremely well, and their being naked
gave us a full opportunity of observing their skin, which was shining,
and nearly as black as ebony. This scene was however contrasted by
the arrival of two canoes filled with emaciated starving wretches,
who clamorously solicited relief from the soldiers, and were ready to
fight for the possession of a bone.

The day following our Commander in Chief was visited by a Mr.


Rynsdorp, who introduced to him two black soldiers, manumized
slaves, who composed part of a corps of three hundred which had
been lately formed. These men were exhibited by Mr. Rynsdorp as
specimens of that valiant body, which but a short time before had
most gallantly distinguished itself by the protection it had afforded to
the colony.

Whilst we still remained at anchor before the fortress Amsterdam, I


received a polite invitation from one Mr. Lolkens, a planter, to whom
I had been recommended, to accept the use of his house and table
on our arrival at Paramaribo, the capital of the colony.
On the 8th we once more went under way, and after the usual
ceremonies on both sides on leaving the fortress, sail’d up the river
Surinam with drums beating, colours flying, and a guard of marines
drawn up on the quarterdeck of each vessel. Having at length
reached Paramaribo, we finally came to an anchor within pistol-shot
off the shore, receiving a salute of eleven guns from the citadel
Zealandia, which was returned by all the ships of our small fleet. [18]

After being confined nearly the whole of sixty-three days within the
limits of a small vessel, and upon an element to which few of the
troops had been accustomed, it would not be easy to describe the
pleasure we experienced on finding ourselves once more on land,
and surrounded by a thousand agreeable circumstances.

The town appeared uncommonly neat and pleasing, the shipping


extremely beautiful, the adjacent woods adorned with the most
luxuriant verdure, the air perfumed with the utmost fragrance, and
the whole scene gilded by the rays of an unclouded sun. We did not
however take leave of our wooden habitation at this time, but the
next day were formally disembarked with a general appearance of
rejoicing; all the ships in the roads being in full dress, and the guns
keeping up an incessant fire till the whole of the troops were landed.

All the inhabitants of Paramaribo were collected to behold this


splendid scene, nor were the expectations they had formed
disappointed. The corps consisted of nearly five hundred young
men; (for we had been so fortunate as only to lose one during the
voyage) the oldest of whom was scarcely more than thirty, and the
whole party neatly cloathed in their new uniforms, and in caps
ornamented with twigs of orange-blossom. We paraded on a large
green plain between the town and the citadel, opposite to the
Governor’s palace; during the course of which ceremonies several
soldiers fainted from the excessive heat. The troops then marched
into quarters prepared for their reception, whilst the officers were
regaled with a dinner by the Governor, which would have derived a
[19]considerable relish from its succeeding the salt provisions, to
which we had so long been confined, had any contrast been
necessary to heighten our opinion of its elegance. But the choicest
delicacies of America and Europe were united in this repast, and
served up in silver. A great variety of the richest wines were poured
out with profusion; the desert was composed of the most delicious
fruits, and the company were attended by a considerable number of
extremely handsome negro and mulatto maids, all naked from the
waist upwards, according to the custom of the country; but the
other parts of their persons arrayed in the finest India chintzes, and
the whole adorned with golden chains, medals, beads, bracelets,
and sweet-smelling flowers.

After partaking of this superb entertainment till about seven o’clock,


I set out in search of the house of Mr. Lolkens, the hospitable
gentleman who had so obligingly invited me to make it my own. I
soon discovered the place, but my reception was so ludicrous that I
cannot forbear relating the particulars. On knocking at the door, it
was opened by a young female negro, of a masculine appearance,
whose whole dress consisted of a single petticoat, and who held a
lighted tobacco-pipe in one hand, and a burning candle in the other,
which she brought close to my face, in order to reconnoitre me. I
enquired if her master was at home, to which she replied, but in a
language totally unintelligible to me. I then mentioned his name, on
which she burst into an immoderate fit of laughter, [20]displaying two
rows of very beautiful teeth; and at the same time, laying hold of
the breast-buttons of my coat, she made me a signal to follow her. I
was much at a loss how to act, but went in, and was ushered by the
girl into a very neat apartment, whither she brought some excellent
fruit, and a bottle of Madeira wine, which she placed upon the table.
She then, in the best manner she was able, informed me that her
masera, with the rest of his family, was gone to spend a few days at
his plantation, and that she was left behind to receive an English
Captain, whom she supposed to be me. I signified that I was, and
filled her out a tumbler of wine, which I had the utmost difficulty to
persuade her to accept; for such is the degrading light in which
these unhappy beings are considered, that it is accounted a high
degree of presumption in them to eat or drink in the presence of an
European. I contrived for some time to carry on something like a
conversation with this woman, but was soon glad to put an end to it
by recurring to my bottle.

Tired with the employments of the day, I longed for some rest, and
made a signal to my attendant that I wanted to sleep: but my
motion was strangely misconstrued; for she immediately seized me
by the neck, and imprinted on my lips a most ardent kiss. Heartily
provoked at this unexpected, and (from one of her colour)
unwelcome salutation, I disentangled myself from her embraces, and
angrily flung into the apartment allotted [21]for my place of rest. But
here I was again pursued by my black tormentor, who, in opposition
to all I could say, insisted upon pulling off my shoes and stockings,
and in a moment disencumbered me of that part of my apparel. I
was extremely chagrined at her conduct, though this is an office
commonly performed by the slaves in Surinam to all ranks and sexes
without exception. Nor ought any one to conceive that this
apparently extraordinary conduct resulted from any peculiarity of
disposition in the girl; her behaviour was only such as would have
been practised by the generality of female negro slaves, and what
will be found, by all who visit the West India settlements, to be
characteristic of the whole dark sisterhood.

Finding in the morning that my friend the planter was not returned, I
took leave of his mansion, and very hospitable servant; and after
visiting the soldiers in their new abodes, was conducted by the
quarter-master to a neat habitation appropriated to my use. I found
the house entirely unfurnished, though not destitute of inhabitants;
for, leaving my Captain’s commission, which was of parchment, in
the window, the first night, I had the mortification to find in the
morning that it was devoured by the rats.

Having taken possession of my habitation, my next wish was to


furnish it properly; but all cares of this nature were rendered
unnecessary by the generous hospitality of the inhabitants; the
ladies supplied me with tables, chairs, [22]glasses, and even plate
and china, in great abundance; and the gentlemen loaded me with
presents of Madeira wine, porter, cyder, rum and sugar, besides a
quantity of the most exquisite fruits. Amongst the latter I was
particularly struck with the shaddock and awara; the former of
these, which is of a very agreeable flavour, between a sweet and an
acid, is produced from a tree supposed to be transplanted from the
coast of Guinea 4, by a Captain Shaddock, whose name it still retains
throughout the English West India islands, but is called
pompelmoose in Surinam. This fruit appears to be of the orange
species, but is as large as the head of a child of eight or ten years
old; the skin is extremely thick, of a bitterish taste, and a pale yellow
or citron colour. There are two species of the shaddock, of which the
pulp of the one is white, and that of the other a beautiful pale red,
which may be safely eaten in considerable quantities: indeed it is
esteemed by the natives, who are in general remarkably fond of it,
as very salubrious.
The Fruit called Avoira.
The Shaddock Apple.

London Published Decr. 1st 1791, by J. Johnson, St. Paul’s Church


Yard.
The awara, or avoira, which is less remarkable for the excellence of
its flavour than its beautiful appearance, grows upon a species of
palm-tree, and is of an oval form, about the size of an Orlean plumb,
and of a rich deep orange colour, nearly approaching to red. It is
much esteemed by the negroes, who exercise their ingenuity [23]in
forming rings out of the stones, which they decorate with cyphers,
initial letters, and other devices, then dispose of them to the
Europeans, who mount them in gold. These stones are large,
extremely hard, and as black as jet or ebony, but the pulp which
surrounds them is very thin.

This day, on examining into the state of our remaining live stock,
such as hogs, sheep, ducks, geese, fowls, and turkies, we found
them nearly as many in number as when we first sailed from
Holland: these were all sent to the colonel’s poultry-yard at the
head-quarters, while we had the additional mortification of seeing
above sixty large kegs with preserved vegetables, &c. and just as
many fine Westphalia hams (being perfectly rotten) thrown into the
river Surinam to feed the sharks.

I now observed, on the second morning after our landing, that my


face, my breast, and hands were entirely spotted over like the skin
of a leopard, occasioned by myriads of gnats or musquitoes, which
flying in clouds, had kept me company during the night, though the
fatigue from my voyage, and the oppressive heat of the climate, had
sunk me into so profound a sleep, that I was insensible of their
stings, till I perceived the effects. These insects are inconceivably
numerous here during the rainy season, and particularly on the
banks of creeks or rivers. None are secured from their attacks, but
they peculiarly infest strangers in preference to the natives; and
wherever they insert their proboscis, and remain [24]unmolested,
they suck the blood till they are scarcely able to fly. Every puncture
they make is succeeded by a large blotch, or rather tumour,
accompanied with an itching, which is almost intolerable. The
presence of the musquitoes is indicated by their buzzing noise,
which alone is sufficient to make one sweat, and which is so very
disagreeable to those who have suffered from their stings, as to
have obtained for them the name of the Devil’s Trumpeters. They
are indeed inconceivably troublesome in every respect. The candles
are no sooner lighted in an evening, than they are stuck full of them;
all kinds of food and drink are exposed to their disagreeable visits,
from which even the mouth and eyes are not exempted.

The best cure for their stings is an application of the juice of lemons
or limes, mixed with water, which is also a tolerable preservative
against their attacks. Immediately before shutting the windows, the
inhabitants commonly burn tobacco in their apartments, the smoke
of which occasions the insects to fly about the room, when the
negro girls unreservedly throw off their petticoats, which is the
whole of their covering, and running naked about the chamber,
chase the gnats therewith out at the windows, or destroy them. The
more delicate or luxurious amongst the natives still employ their
slaves in fanning them during the whole night, excepting such as
have green gauze doors to their beds or pavilions; but the generality
of the people in Surinam sleep in roomy [25]cotton hammocks, which
are covered with a very large thin sheet, suspended from a tight line
immediately over them, something like the awning of a ship, which
serves in some measure to keep off these troublesome insects, and
the want of which had exposed me to be thus stung all over.

There are also in Surinam a still larger species of gnats or


musquitoes, called mawkers, the stings of which are extremely
painful indeed; but as they are much less numerous than the former,
they are not nearly so troublesome to the inhabitants, and are
consequently less remarked.——But to proceed:
On the morning of the 22d, an elderly negro-woman, with a black
girl about fourteen, entering my apartment, it would be difficult to
express my astonishment when she gravely presented me her
daughter, to become what she was pleased to term my wife. I had
so little gallantry, however, as to reject the offer with a loud laugh;
but at the same time accompanied the refusal with a small but
welcome present, with which they appeared perfectly satisfied, and
departed with every possible demonstration of gratitude and respect.
The girls here, who voluntarily enter into these connections, are
sometimes mulattoes, sometimes Indians, and often negroes. They
all exult in the circumstance of living with an European, whom in
general they serve with the utmost tenderness and fidelity, and
tacitly reprove those numerous fair-ones who break through ties
more sacred [26]and solemn. Young women of this description
cannot indeed be married, or connected in any other way, as most of
them are born or trained up in a state of slavery; and so little is the
practice condemned, that while they continue faithful and constant
to the partner by whom they are chosen, they are countenanced and
encouraged by their nearest relations and friends, who call this a
lawful marriage, nay, even the clergy avail themselves of this custom
without restraint; witness the Rev. Mr. S—dh—s, Mr. T—ll—t, &c.
Many of the sable-coloured beauties will however follow their own
penchant without any restraint whatever, refusing with contempt the
golden bribes of some, while on others they bestow their favours for
a dram or a broken tobacco-pipe, if not for nothing.

The hospitality I had experienced on our first arrival in the colony


was not confined to that time only: I had a general invitation to visit,
besides his excellency the governor, and Colonel Texier, the
commandant, in more than twenty respectable families, whenever it
suited my convenience; so that, though the officers of our corps had
formed a regimental mess, I had seldom the honour of their
company. One gentleman, a Mr. Kennedy, in particular, carried his
politeness so far, as not only to offer me the use of his carriage,
saddle-horses, and table, but even to present me with a fine negro
boy, named Quaco, to carry my umbrella as long as I remained in
Surinam. The other gentlemen of the regiment also [27]met with
great civilities, and the whole colony seemed anxious to testify their
respect, by vying with each other in a constant round of festivity.
Balls, concerts card-assemblies, and every species of amusement in
their power, were constantly contrived for our entertainment. The
spirit of conviviality next reached on board the men-of-war, where
we entertained the ladies with cold suppers and dancing upon the
quarter-deck, under an awning, till six in the morning, generally
concluding the frolic by a cavalcade, or an airing in their carriages.
This constant routine of dissipation, which was rendered still more
pernicious by the enervating effects of an intensely hot climate,
where one is in a perpetual state of perspiration, already threatened
to become fatal to two or three of our officers. Warned by their
example, I retired from all public companies, sensible that by such
means I could alone preserve my health, in a country which has
such a tendency to debilitate the human frame, that an European,
however cautious to avoid excesses, has always reason to
apprehend its dreadful effects.

Dissipation and luxury appear to be congenial to the inhabitants of


this climate, and great numbers must annually fall victims to their
very destructive influence. Their fatal consequences are indeed too
visible in the men, who have indulged themselves in intemperance
and other sensual gratifications, and who appear withered and
enervated in the extreme; nor do the generality of [28]the Creole
females exhibit a more alluring appearance; they are languid, their
complexions are sallow, and the skin even of the young ladies is
frequently shrivelled. This is however not the case with all; and I
have been acquainted with some who, preserving a glow of health
and freshness in their lovely countenance, were entitled to contend
for the prize of beauty with the fairest European. But, alas! the
numbers of this last description are so small, that the colonists in
their amours most usually prefer the Indian negro and mulatto girls,
particularly on account of their remarkable cleanliness, health, and
vivacity. For the excesses of the husbands in this respect, and the
marked neglect which they meet from them, the Creole ladies most
commonly, at a very early period, appear in mourning weeds, with
the agreeable privilege however of making another choice, in the
hopes of a better partner; nor are they long without another mate.
Such indeed is the superior longevity of the fair females of Surinam,
compared to that of the males (owing chiefly, as I said, to their
excesses of all sorts) that I have frequently known wives who have
buried four husbands, but never met a man in this country who had
survived two wives.

The ladies do not, however, always bear with the most becoming
patience the slights and insults they thus meet with, in the
expectation of a sudden release, but mostly persecute their
successful sable rivals (even on suspicion) with implacable hatred
and the most unrelenting [29]barbarity; while they chastise their
partners not only with a shew of ineffable contempt, but with giving
in public the most unequivocal marks of preference towards those
gentlemen who newly arrive from Europe; which occasioned the trite
proverb and observation in the colony, that the Tropical ladies and
the musquitoes have an instinctive preference for a newly-landed
European: this partiality is indeed so very extreme, and the proofs of
it so very apparent and nauseous, that some command of temper is
necessary to prevent the disgust which such behaviour must
naturally excite, particularly where the object is not very inviting;
nay, it was even publicly reported at Paramaribo, that two of these
Tropical Amazons had fought a duel for the sake of one of our
officers.

You might also like