-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c519e47
commit 5e08f0c
Showing
4 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/) | ||
|