Skip to content

Commit bf26d68

Browse files
mmckyHumphreyYang
andauthored
Update lectures/polars.md
Co-authored-by: Humphrey Yang <[email protected]>
1 parent 0704681 commit bf26d68

File tree

1 file changed

+3
-47
lines changed

1 file changed

+3
-47
lines changed

lectures/polars.md

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -545,56 +545,12 @@ For example, suppose that we are interested in the [unemployment rate](https://f
545545

546546
Alternatively, we can access the CSV file from within a Python program.
547547

548-
This can be done with a variety of methods.
549548

550-
We start with a relatively low-level method and then return to polars.
549+
In {doc}`pandas`, we studied how to use `requests` and `pandas` to access API data.
551550

552-
### Accessing Data with {index}`requests <single: requests>`
551+
Here polars' `read_csv` function provides the same functionality.
553552

554-
```{index} single: Python; requests
555-
```
556-
557-
One option is to use [requests](https://requests.readthedocs.io/en/latest/), a standard Python library for requesting data over the Internet.
558-
559-
To begin, try the following code on your computer
560-
561-
```{code-cell} ipython3
562-
r = requests.get('https://fred.stlouisfed.org/graph/fredgraph.csv?bgcolor=%23e1e9f0&chart_type=line&drp=0&fo=open%20sans&graph_bgcolor=%23ffffff&height=450&mode=fred&recession_bars=on&txtcolor=%23444444&ts=12&tts=12&width=1318&nt=0&thu=0&trc=0&show_legend=yes&show_axis_titles=yes&show_tooltip=yes&id=UNRATE&scale=left&cosd=1948-01-01&coed=2024-06-01&line_color=%234572a7&link_values=false&line_style=solid&mark_type=none&mw=3&lw=2&ost=-99999&oet=99999&mma=0&fml=a&fq=Monthly&fam=avg&fgst=lin&fgsnd=2020-02-01&line_index=1&transformation=lin&vintage_date=2024-07-29&revision_date=2024-07-29&nd=1948-01-01')
563-
```
564-
565-
If there's no error message, then the call has succeeded.
566-
567-
If you do get an error, then there are two likely causes
568-
569-
1. You are not connected to the Internet --- hopefully, this isn't the case.
570-
1. Your machine is accessing the Internet through a proxy server, and Python isn't aware of this.
571-
572-
In the second case, you can either
573-
574-
* switch to another machine
575-
* solve your proxy problem by reading [the documentation](https://requests.readthedocs.io/en/latest/)
576-
577-
Assuming that all is working, you can now proceed to use the `source` object returned by the call `requests.get('https://research.stlouisfed.org/fred2/series/UNRATE/downloaddata/UNRATE.csv')`
578-
579-
```{code-cell} ipython3
580-
url = 'https://fred.stlouisfed.org/graph/fredgraph.csv?bgcolor=%23e1e9f0&chart_type=line&drp=0&fo=open%20sans&graph_bgcolor=%23ffffff&height=450&mode=fred&recession_bars=on&txtcolor=%23444444&ts=12&tts=12&width=1318&nt=0&thu=0&trc=0&show_legend=yes&show_axis_titles=yes&show_tooltip=yes&id=UNRATE&scale=left&cosd=1948-01-01&coed=2024-06-01&line_color=%234572a7&link_values=false&line_style=solid&mark_type=none&mw=3&lw=2&ost=-99999&oet=99999&mma=0&fml=a&fq=Monthly&fam=avg&fgst=lin&fgsnd=2020-02-01&line_index=1&transformation=lin&vintage_date=2024-07-29&revision_date=2024-07-29&nd=1948-01-01'
581-
source = requests.get(url).content.decode().split("\n")
582-
source[0]
583-
```
584-
585-
```{code-cell} ipython3
586-
source[1]
587-
```
588-
589-
```{code-cell} ipython3
590-
source[2]
591-
```
592-
593-
We could now write some additional code to parse this text and store it as an array.
594-
595-
But this is unnecessary --- polars' `read_csv` function can handle the task for us.
596-
597-
We use `try_parse_dates=True` so that polars recognizes our dates column, allowing for simple date filtering
553+
We use `try_parse_dates=True` so that polars recognizes our dates column
598554

599555
```{code-cell} ipython3
600556
data = pl.read_csv(url, try_parse_dates=True)

0 commit comments

Comments
 (0)