Instant ebooks textbook Python Programming for Data Analysis 1st Edition José Unpingco download all chapters
Instant ebooks textbook Python Programming for Data Analysis 1st Edition José Unpingco download all chapters
Instant ebooks textbook Python Programming for Data Analysis 1st Edition José Unpingco download all chapters
com
https://textbookfull.com/product/python-
programming-for-data-analysis-1st-edition-jose-
unpingco/
https://textbookfull.com/product/python-for-probability-statistics-
and-machine-learning-jose-unpingco/
textbookfull.com
https://textbookfull.com/product/python-for-probability-statistics-
and-machine-learning-2nd-edition-jose-unpingco/
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
https://textbookfull.com/product/the-philosophical-thought-of-wang-
chong-alexus-mcleod/
textbookfull.com
https://textbookfull.com/product/ib-english-a-language-literature-
course-book-oxford-ib-diploma-program-course-book-2nd-edition-rob-
allison/
textbookfull.com
https://textbookfull.com/product/just-enough-r-an-interactive-
approach-to-machine-learning-and-analytics-1st-edition-richard-j-
roiger/
textbookfull.com
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
© 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.
References
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
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Chapter 1
Basic Programming
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.
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
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.
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
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
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.1.5 Strings
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
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'
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'
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'
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
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.
(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]
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'
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—
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.
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.
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.
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.
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 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.
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.
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.
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.
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.
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.
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.