Skip to content

Commit

Permalink
Added cookbook
Browse files Browse the repository at this point in the history
  • Loading branch information
anandology committed May 16, 2024
1 parent c519e47 commit 5e08f0c
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 1 deletion.
6 changes: 5 additions & 1 deletion book/_quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ book:
- writing-custom-modules.qmd
- organizing-python-code.qmd
- testing.qmd

- part: Cookbook
href: cookbook/index.qmd
chapters:
- cookbook/dates.qmd
- cookbook/tabulate.qmd
- part: Live Notes
chapters:
- live-notes/day1.ipynb
Expand Down
95 changes: 95 additions & 0 deletions book/cookbook/dates.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
sidebar: cookbook
execute:
enabled: true
cache: true
---

# Working with Dates

Python has a `datetime` module to work with date, time and datetimes. This recipe focuses only on working with dates.

We start with importing the `datetime` module.

```{python}
import datetime
```

For all the examples below, please assume that `datetime` is already imported.

## Common Operations

Creating a date:

```{python}
datetime.date(2020, 10, 20)
```

Today's date:

```{python}
datetime.date.today()
```

Accessing year, month and day.

```{python}
d = datetime.date(2020, 10, 20)
print(d.year, d.month, d.day)
```

Parsing a date:

```{python}
datetime.date.fromisoformat("2020-02-26")
```

## Date Arithmatic

The datetime module has `timedelta` to add or subtract dates.

The date of yesterday:

```{python}
datetime.date.today() - datetime.timedelta(days=1)
```

10 days from `2020-02-25`:

```{python}
date = datetime.date(2020, 2, 25)
for i in range(10):
d = date + datetime.timedelta(days=i)
print(d)
```

Or the same thing as a list comprehension:

```{python}
def date_range(start_date, ndays):
return [start_date + datetime.timedelta(days=i) for i in range(ndays)]
date = datetime.date(2020, 2, 25)
date_range(date, 10)
```

## Formatting Dates

To format the date in YYYY-MM-DD format, we could just convert the date into string or use `isoformat` method.

```{python}
d = datetime.date(2020, 2, 25)
print(d)
print(str(d))
print(d.isoformat())
```

The `strftime` method provides a flexible way to format a date.

```{python}
d = datetime.date(2020, 2, 25)
print(d.strftime("%Y-%m-%d"))
print(d.strftime("%a %b %d, %Y"))
```

Please refer to [strftime.org](https://strftime.org/) to know the for the availble options.
16 changes: 16 additions & 0 deletions book/cookbook/index.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
sidebar: cookbook
listing:
contents: "*.qmd"
type: table
id: sample-listings
fields:
- title
---
# Python Cookbook


This is a collection of recipies to solve various tasks in Python.

::: {#sample-listings}
:::
72 changes: 72 additions & 0 deletions book/cookbook/tabulate.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
sidebar: cookbook
execute:
enabled: true
cache: true
---

# Printing Tables with Tabulate

The `tabulate` is a python library with a very simple API to format data as tables.

## Importing `tabulate`

Start with importing the `tabulate` funtion from the `tabulate` module.

```{python}
from tabulate import tabulate
```

## Usage

We just need to pass the tabular data to tabulate and it takes care of formatting it as a nice table.

```{python}
from tabulate import tabulate
data =[
["2023-10-18", "1", "83.25"],
["2023-10-17", "1", "83.22"],
["2023-10-16", "1", "83.25"],
["2023-10-13", "1", "83.27"],
["2023-10-12", "1", "83.24"]]
print(tabulate(data))
```

We can optionally provide headers as well.

```{python}
from tabulate import tabulate
headers = ["Date", "USD", "INR"]
data =[
["2023-10-18", "1", "83.25"],
["2023-10-17", "1", "83.22"],
["2023-10-16", "1", "83.25"],
["2023-10-13", "1", "83.27"],
["2023-10-12", "1", "83.24"]]
print(tabulate(data, headers=headers))
```

It is also possible to provide data a list of dictionaries instead of a list of lists.


```{python}
from tabulate import tabulate
data =[
{"Date": "2023-10-18", "USD": "1", "INR": "83.25"},
{"Date": "2023-10-17", "USD": "1", "INR": "83.22"},
{"Date": "2023-10-16", "USD": "1", "INR": "83.25"},
{"Date": "2023-10-13", "USD": "1", "INR": "83.27"},
{"Date": "2023-10-12", "USD": "1", "INR": "83.24"}]
print(tabulate(data, headers="keys"))
```

## References

* [Tabulate Documentation](https://pypi.org/project/tabulate/)

0 comments on commit 5e08f0c

Please sign in to comment.