<jupyter_start><jupyter_text>Times and placesElements of Data Scienceby [<NAME>](https://allendowney.com)[MIT License](https://opensource.org/licenses/MIT) GoalsIn the previous notebook, you learned about variables and two kinds of values: integers and floating-point numbers.In this notebook, you'll see some additional types:* Strings, which contain sequences of letters, are used to represent text data.* Timestamp values are used to represent dates and times.* And several ways to represent and display geographical locations.Not every data science project uses all of these types, but many projects use at least one.  A noteIn these notebooks, I generally try to define terms when they are first used, and to explain the code examples as we go along.However, there are a few places where I provide code with the expectation that you will not understand all of the details yet.  When that happens, I'll explain what you should or should not understand.If you are able to do the exercises, that probably means you are understanding what you need to understand.  StringsA **string** is a sequence of letters, numbers, and punctuation marks.In Python you can create a string by typing letters between single or double quotation marks.<jupyter_code>'Data'
"Science"<jupyter_output><empty_output><jupyter_text>And you can assign string values to variables.<jupyter_code>first = 'Data'
last = "Science"<jupyter_output><empty_output><jupyter_text>Some arithmetic operators work with strings, but they might no do what you expect.  For example, the `+` operator "concatenates" two strings; that is, it creates a new string that contains the first string followed by the second string:<jupyter_code>first + last<jupyter_output><empty_output><jupyter_text>If you want to put a space between the words, you can use a string that contains a space:<jupyter_code>first + ' ' + last<jupyter_output><empty_output><jupyter_text>Strings are used to store text data like names, addresses, titles, etc.When you read data from a file, you might see values that look like numbers, but they are actually strings, like this:<jupyter_code>not_actually_a_number = '123'<jupyter_output><empty_output><jupyter_text>If you try to do math with these strings, you might get an error:<jupyter_code>not_actually_a_number + 1<jupyter_output><empty_output><jupyter_text>Or you might get a surprising result:<jupyter_code>not_actually_a_number + '1'<jupyter_output><empty_output><jupyter_text>Fortunately, you can convert strings to numbers.If you have a string that contains only digits, you can convert it to an integer using the `int` function:<jupyter_code>int('123')<jupyter_output><empty_output><jupyter_text>Or you can convert it to a floating-point number using `float`:<jupyter_code>float('123')<jupyter_output><empty_output><jupyter_text>But if the string contains a decimal point, you can't convert it to an `int`:<jupyter_code>int('12.3')<jupyter_output><empty_output><jupyter_text>Going in the other direction, you can convert almost any type of value to a string using `str`:<jupyter_code>str(123)
str(12.3)<jupyter_output><empty_output><jupyter_text>**Exercise**: When personal names are stored in a database, they are usually stored in three variables: a given name, a family name, and sometimes a middle name.  For example, a list of great rock drummers might include:<jupyter_code>given = 'Neil'
middle = 'Ellwood'
family = 'Peart'<jupyter_output><empty_output><jupyter_text>But names are often displayed different ways in different contexts.  For example, the first time you mention someone in an article, you might give all three names, like "<NAME>".  But in the index of a book, you might put the family name first, like "<NAME>".Write Python expressions that use the variables `given`, `middle`, and `family` to display <NAME>'s name in these two formats.<jupyter_code># Solution goes here
# Solution goes here<jupyter_output><empty_output><jupyter_text>Dates and times If you read data from a file, you might also find that dates and times are represented with strings.<jupyter_code>not_really_a_date = 'June 4, 1989'<jupyter_output><empty_output><jupyter_text>To confirm that this value is a string, we can use the `type` function, which takes a value and reports its type.<jupyter_code>type(not_really_a_date)<jupyter_output><empty_output><jupyter_text>`str` indicates that the value of `not_really_a_date` is a string.We get the same result with `not_really_a_time`, below:<jupyter_code>not_really_a_time = '6:30:00'
type(not_really_a_time)<jupyter_output><empty_output><jupyter_text>Representing dates and times using strings provides human-readable values, but they are not useful for doing computation.Fortunately, Python provides libraries for working with date and time data; the one we'll use is called Pandas.As always, we have to import a library before we use it; it is conventional to import Pandas with the abbreviated name `pd`:<jupyter_code>import pandas as pd<jupyter_output><empty_output><jupyter_text>Pandas provides a type called `Timestamp`, which represents a date and time.It also provides a function called `Timestamp`, which we can use to convert a string to a `Timestamp`:<jupyter_code>pd.Timestamp('6:30:00')<jupyter_output><empty_output><jupyter_text>Or we can do the same thing using the variable defined above.<jupyter_code>pd.Timestamp(not_really_a_time)<jupyter_output><empty_output><jupyter_text>In this example, the string specifies a time but no date, so Pandas fills in today's date.A `Timestamp` is a value, so you can assign it to a variable.<jupyter_code>date_of_birth = pd.Timestamp('June 4, 1989')
date_of_birth<jupyter_output><empty_output><jupyter_text>If the string specifies a date but no time, Pandas fills in midnight as the default time.If you assign the `Timestamp` to a variable, you can use the variable name to get the year, month, and day, like this:<jupyter_code>date_of_birth.year, date_of_birth.month, date_of_birth.day<jupyter_output><empty_output><jupyter_text>You can also gets the name of the month and the day of the week.<jupyter_code>date_of_birth.day_name(), date_of_birth.month_name()<jupyter_output><empty_output><jupyter_text>`Timestamp` provides a function called `now` that returns the current date and time.<jupyter_code>now = pd.Timestamp.now()
now<jupyter_output><empty_output><jupyter_text>**Exercise:** Use the value of `now` to display the name of the current month and day of the week.<jupyter_code># Solution goes here<jupyter_output><empty_output><jupyter_text>Timedelta`Timestamp` values support some arithmetic operations.  For example, you can compute the difference between two `Timestamps`:<jupyter_code>age = now - date_of_birth
age<jupyter_output><empty_output><jupyter_text>The result is a `Timedelta` that represents the current age of someone born on `date_of_birth`.The `Timedelta` contains `components` that store the number of days, hours, etc. between the two `Timestamp` values.<jupyter_code>age.components<jupyter_output><empty_output><jupyter_text>You can get one of the components like this:<jupyter_code>age.days<jupyter_output><empty_output><jupyter_text>The biggest component of `Timedelta` is days, not years, because days are well defined and years are problematic.Most years are 365 days, but some are 366.  The average calendar year is 365.24 days, which is a very good approximation of a solar year, [but it is not exact](https://pumas.jpl.nasa.gov/files/04_21_97_1.pdf).One way to compute age in years is to divide age in days by 365.24:<jupyter_code>age.days / 365.24<jupyter_output><empty_output><jupyter_text>But people usually report their ages in integer years.  We can use the Numpy `floor` function to round down:<jupyter_code>import numpy as np

np.floor(age.days / 365.24)<jupyter_output><empty_output><jupyter_text>Or the `ceil` function (which stands for "ceiling") to round up:<jupyter_code>np.ceil(age.days / 365.24)<jupyter_output><empty_output><jupyter_text>We can also compare `Timestamp` values to see which comes first.For example, let's see if a person with a given birthdate has already had a birthday this year.We can create a new `Timestamp` with the year from `now` and the month and day from `date_of_birth`.<jupyter_code>bday_this_year = pd.Timestamp(now.year, date_of_birth.month, date_of_birth.day)
bday_this_year<jupyter_output><empty_output><jupyter_text>The result represents the person's birthday this year.  Now we can use the `>` operator to check whether `now` is later than the birthday:<jupyter_code>now > bday_this_year<jupyter_output><empty_output><jupyter_text>The result is either `True` or `False`, which are special values in Python used to represent results from this kind of comparison.These values belong to a type called `bool`, short for "Boolean algebra", which is a branch of algebra where all values are either true or false.<jupyter_code>type(True)
type(False)<jupyter_output><empty_output><jupyter_text>**Exercise:** Any two people with different birthdays have a "Double Day" when one is twice as old as the other.Suppose you are given two `Timestamp` values, `d1` and `d2`, that represent birthdays for two people.  Use `Timestamp` arithmetic to compute their double day.Here are two example dates; with these dates, the result should be December 19, 2009.<jupyter_code>d1 = pd.Timestamp('2003-07-12')
d2 = pd.Timestamp('2006-09-30')
# Solution goes here<jupyter_output><empty_output><jupyter_text>LocationThere are many ways to represent geographical locations, but the most common, at least for global data, is latitude and longitude.When stored as strings, latitude and longitude are expressed in degrees with compass directions N, S, E, and W.  For example, this string represents the location of Boston, MA, USA:<jupyter_code>lat_lon_string = '42.3601° N, 71.0589° W'<jupyter_output><empty_output><jupyter_text>When we compute with location information, we use floating-point numbers, with * Positive latitude for the northern hemisphere, negative latitude for the southern hemisphere, and * Positive longitude for the eastern hemisphere and negative latitude for the western hemisphere.Of course, the choice of the origin and the orientation of positive and negative are arbitrary choices that were made for historical reasons.  We might not be able to change conventions like these, but we should be aware that they are conventions.Here's how we might represent the location of Boston with two variables.<jupyter_code>lat = 42.3601
lon = -71.0589<jupyter_output><empty_output><jupyter_text>It is also possible to combine two numbers into a composite value and assign it to a single variable:<jupyter_code>boston = lat, lon
boston<jupyter_output><empty_output><jupyter_text>The type of this variable is `tuple`, which is a mathematical term for a value that contains a sequence of elements.  Math people pronounce it "tuh' ple", but computational people usually say "too' ple".  Take your pick.<jupyter_code>type(boston)<jupyter_output><empty_output><jupyter_text>If you have a tuple with two elements, you can assign them to two variables, like this:<jupyter_code>y, x = boston
y
x<jupyter_output><empty_output><jupyter_text>Notice that I assigned latitude to `y` and longitude to `x`, because a `y` coordinate usually goes up and down like latitude, and an `x` coordinate usually goes side-to-side like longitude. **Exercise:** Find the latitude and longitude of the place you were born or someplace you think of as your "home town".  [You can use this web page to look it up](https://www.latlong.net/), among others.Make a tuple of floating-point numbers that represents this location.<jupyter_code># Solution goes here<jupyter_output><empty_output><jupyter_text>DistanceIf you are given two tuples that represent locations, you can compute the approximate distance between them, along the surface of the globe, using the haversine function.If you are curious about it, [you can read an explanation in this article](https://janakiev.com/blog/gps-points-distance-python/).To estimate a haversine distance, we have to compute the haversine function, which is defined:$\mathrm{haversine}(θ)=\sin^2(θ/2)$Where θ is an angle in radians.We can compute this function in Python like this:<jupyter_code>import numpy as np

θ = 1
np.sin(θ/2)**2<jupyter_output><empty_output><jupyter_text>You can use Greek letters in variable names, but there is currently no way to type them in Jupyter/Colab, so I usually copy them from a web page and paste them in.To avoid the inconvenience, it is more common to write out letter names, like this:<jupyter_code>theta = 1
np.sin(theta/2)**2<jupyter_output><empty_output><jupyter_text>**Exercise:** This is a good time to remind you that the operator for exponentiation is `**`.  In some other languages the operator for exponentiation is `^`.  That is also an operator in Python, but it performs another operation altogether.Try out the previous expression, replacing `**` with `^`, and see what error message you get.  Remember this message in case you see it in the future!<jupyter_code># Solution goes here<jupyter_output><empty_output><jupyter_text>Defining functionsAt this point you don't have to understand how to define a new function.  But you will see function defininitions, so I want to explain the basics now. If we are planning to use an expression like `np.sin(theta/2)**2` more than a few times, we can define a new function that compute it, like this:<jupyter_code>def haversine(theta):
    """Compute the haversine function of theta."""
    return np.sin(theta/2)**2<jupyter_output><empty_output><jupyter_text>On the first line, `def` indicates that we are defining a function.The second line is a "triple-quoted string", which describes what the function does, but it has no effect when the program runs.On the third line, `return` indicates the result of the function.When you run the previous cell, it creates a new variable called `haversine`.  You can display its value like this:<jupyter_code>haversine<jupyter_output><empty_output><jupyter_text>And you can display its type like this:<jupyter_code>type(haversine)<jupyter_output><empty_output><jupyter_text>So `haversin` is a variable that refers to a function.To run the function and compute a result, we have to "call" the function and provide a value for `theta`:<jupyter_code>haversine(1)<jupyter_output><empty_output><jupyter_text>When you define a function, you create a new variable.  But the function doesn't actually run until you call it.  Haversine distanceNow we can use `haversine` as part of a function that computes haversine distances.I won't explain this function in as much detail, but if you read through it, you might get a sense of how it works.<jupyter_code>def haversine_distance(coord1, coord2):
    """Haversine distance between two locations.
    
    coord1: lat-lon as tuple of float 
    coord2: lat-lon as tuple of float
    
    returns: distance in km
    """
    R = 6372.8  # Earth radius in km
    lat1, lon1 = coord1
    lat2, lon2 = coord2
    
    phi1, phi2 = np.radians(lat1), np.radians(lat2) 
    dphi       = np.radians(lat2 - lat1)
    dlambda    = np.radians(lon2 - lon1)
    
    a = haversine(dphi) + np.cos(phi1)*np.cos(phi2)*haversine(dlambda)
    
    distance = 2*R*np.arctan2(np.sqrt(a), np.sqrt(1 - a))
    
    return distance<jupyter_output><empty_output><jupyter_text>When we call this function, we provide two tuples, each representing a latitude and a longitude.We already have a tuple that represents the location of Boston.Now here's a tuple that represents the location of London, England, UK:<jupyter_code>london = 51.5074, -0.1278<jupyter_output><empty_output><jupyter_text>And here's the haversine distance between Boston and London.<jupyter_code>haversine_distance(boston, london)<jupyter_output><empty_output><jupyter_text>The actual geographic distance is slightly different because Earth is not a perfect sphere.  But the error of this estimate is less than 1%. **Exercise:** Use `haversine_distance` to compute the distance between Boston and your "home town" from the previous exercise.If possible, use an online map to check the result.<jupyter_code># Solution goes here<jupyter_output><empty_output><jupyter_text>GeopandasPython provides libraries for working with geographical data.  One of the most popular is Geopandas, which is based on another library called Shapely.  I'll introduce these libraries here, and we'll come back to them later.Shapely provides `Point` and `LineString` values, which we'll use to represent geographic locations and lines between locations.<jupyter_code>from shapely.geometry import Point, LineString<jupyter_output><empty_output><jupyter_text>We can use the tuples we defined in the previous section to create Shapely `Point` values, but we have to reverse the order of the coordinates, providing them in x-y order rather than lat-lon order, because that's the order the `Point` function expects.<jupyter_code>lat, lon = boston
p1 = Point(lon, lat)
lat, lon = london
p2 = Point(lon, lat)<jupyter_output><empty_output><jupyter_text>If we display a `Point` value, we get a graphical representation, but not a very useful one.<jupyter_code>p1<jupyter_output><empty_output><jupyter_text>Soon we will see how to plot a `Point`, more usefully, on a map. We can use the points we just defined to create a `LineString`:<jupyter_code>line = LineString([p1, p2])<jupyter_output><empty_output><jupyter_text>If we display the result, we get another not very useful graphical representation.<jupyter_code>line<jupyter_output><empty_output><jupyter_text>However, now we can use Geopandas to show these points and lines on a map.If you are running this notebook on Colab, the following cell will install Geopandas, which should only take a few seconds.  It uses features we have not seen yet; you might be able to read it and guess how it works, but you don't have to.<jupyter_code>import sys
IN_COLAB = 'google.colab' in sys.modules

if IN_COLAB:
    !pip install geopandas<jupyter_output><empty_output><jupyter_text>Now the following import statement should work.<jupyter_code>import geopandas as gpd<jupyter_output><empty_output><jupyter_text>If you are running this notebook on Colab, the libraries you install disappear when you shut down the notebook.  When you start the notebook again, you have to install them again. The following code loads a map of the world and plots it.<jupyter_code>path = gpd.datasets.get_path('naturalearth_lowres')
world = gpd.read_file(path)
world.plot(color='white', edgecolor='gray');<jupyter_output><empty_output><jupyter_text>Here's a version that just plots North America and Europe:<jupyter_code>north_america = world.continent == 'North America'
europe = world.continent == 'Europe'
region = world[north_america | europe]
region.plot(color='white', edgecolor='gray');<jupyter_output><empty_output><jupyter_text>Notice:* By default, Geopandas uses the political definition of "Europe", which includes the part of Russia that is on the Asian continent.* It also uses an [equirectangular projection](https://en.wikipedia.org/wiki/Equirectangular_projection), which provides a misleading picture of relative land areas.You can't make a map without making visualization decisions.Now let's put dots on the map for Boston and London.  We have to put the `Point` values and the `LineString` into a `GeoSeries`, which provides a `plot` function:<jupyter_code>t = [p1, p2, line]
series = gpd.GeoSeries(t)
series.plot();<jupyter_output><empty_output><jupyter_text>Here's a first attempt to plot the maps and the lines together:<jupyter_code># plot the map
region.plot(color='white', edgecolor='gray')

# plot Boston, London, and the line
series.plot();<jupyter_output><empty_output><jupyter_text>GeoPandas puts the two plots on different axes, which is not what we want in this case.To get the points and the map on the same axes, we have to use a function from Matplotlib, which is a visualization library we will use extensively.<jupyter_code>import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>The function is `gca`, which stands for "get current axes".  We can use the result to tell `plot` to put the points and lines on the current axes, rather than create a new one.<jupyter_code>ax = plt.gca()

# plot the map
region.plot(color='white', edgecolor='gray', ax=ax)

# plot Boston, London, and the line
series.plot(ax=ax);<jupyter_output><empty_output><jupyter_text>We can also use `plt.figure` to set the figure size.<jupyter_code>plt.figure(figsize=(12, 12))
ax = plt.gca()

# plot the map
world.plot(color='white', edgecolor='gray', ax=ax)

# plot Boston, London, and the line
series.plot(ax=ax);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07912246865959499, 'fraction_numerical': 0.009257473481195757, 'mean_word_length': 6.018950930626057, 'pattern_counts': {'":': 0, '<': 265, '<?xml version=': 0, '>': 267, 'https://': 6, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '65', 'dir': 'jupyter-structured-clean-dedup', 'id': '267124', 'license': 'MIT', 'n_tokens_mistral': 6337, 'n_tokens_neox': 6066, 'n_words': 2467, 'path': '02_times.ipynb', 'repo_name': 'KwekuYamoah/ElementsOfDataScience', 'size': '20775'}