Skip to content

Commit

Permalink
Update timeseries.md
Browse files Browse the repository at this point in the history
  • Loading branch information
doctor-phil committed Oct 31, 2024
1 parent 5cef199 commit 92240db
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions lectures/pandas/timeseries.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ kernelspec:

```{code-cell} python
import os
# see section on API keys at end of lecture!
os.environ["NASDAQ_DATA_LINK_API_KEY"] = "jEKP58z7JaX6utPkkpEp"
import pandas as pd
import matplotlib.pyplot as plt
import nasdaqdatalink as ndl
# see section on API keys at end of lecture!
os.environ["NASDAQ_DATA_LINK_API_KEY"] = "jEKP58z7JaX6utPkkpEp"
start_date = "2014-05-01"
%matplotlib inline
Expand Down Expand Up @@ -197,17 +198,28 @@ The flexibility of these features is best understood through example,
so let's load up some data and take a look.

```{code-cell} python
btc_usd = ndl.get_table("QDL/BCHAIN")
btc_usd = ndl.get_table("QDL/BCHAIN", paginate=True)
btc_usd.info()
btc_usd.head()
```

Here, we have the Bitcoin (BTC) to US dollar (USD) exchange rate from
March 2014 until today.
2009 until today, as well as other variables relevant to the Bitcoin ecosystem, in long ("melted") form.

```{code-cell} python
print(btc_usd.code.unique())
btc_usd.dtypes
```

Notice that the type of `date` is `datetime`. We would like this to be the index, and we want to drop the long form. We'll also select only a couple of columns of interest. (The column descriptions can be found [here](https://data.nasdaq.com/databases/BCHAIN)). We'll choose Market Price (in USD) (`MKPRU`), Total Market Cap (`MKTCP`), and Estimated Transaction Volume in USD (`ETRVU`).

Notice that the type of index is `DateTimeIndex`.
```{code-cell} python
btc_usd = btc_usd.pivot_table(index='date', columns='code', values='value')
btc_usd = btc_usd[["MKPRU", "MKTCP", "ETRVU"]]
btc_usd.head()
```

This is the key that enables things like...
Now that we have a datetime index, it enables things like...

Extracting all data for the year 2015 by passing `"2015"` to `.loc`.

Expand Down Expand Up @@ -289,11 +301,11 @@ btc_date_column.head()
```

```{code-cell} python
btc_date_column["Date"].dt.year.head()
btc_date_column["date"].dt.year.head()
```

```{code-cell} python
btc_date_column["Date"].dt.month.head()
btc_date_column["date"].dt.month.head()
```

## Leads and Lags: `df.shift`
Expand Down Expand Up @@ -379,8 +391,8 @@ window for the whole dataset.

```{code-cell} python
fig, ax = plt.subplots(figsize=(10, 4))
btc_usd["Open"].plot(ax=ax, linestyle="--", alpha=0.8)
btc_usd.rolling("21d").max()["Open"].plot(ax=ax, alpha=0.8, linewidth=3)
btc_usd["MKPRU"].plot(ax=ax, linestyle="--", alpha=0.8)
btc_usd.rolling("21d").max()["MKPRU"].plot(ax=ax, alpha=0.8, linewidth=3)
ax.legend(["Original", "21 day max"])
```

Expand Down

0 comments on commit 92240db

Please sign in to comment.