Skip to content

Commit cb7719a

Browse files
anissa111kafitzgeraldandy-theia
authored
Python datetime and days_in_month (#44)
* minimal first pass * minor updates * add days_in_month ncl page and index entry * add raw ncl, receipt file, and update main ncl entry * add note about input types * fix typo, add days in month section to datetime.ipynb * change maxdepth receipts page * remove filler content under receipts * Update applications/date_time/datetime.ipynb Co-authored-by: Katelyn FitzGerald <[email protected]> * Update applications/date_time/datetime.ipynb Co-authored-by: Katelyn FitzGerald <[email protected]> * Update applications/date_time/datetime.ipynb Co-authored-by: Katelyn FitzGerald <[email protected]> * Update applications/date_time/datetime.ipynb Co-authored-by: Katelyn FitzGerald <[email protected]> * Update applications/date_time/datetime.ipynb Co-authored-by: Katelyn FitzGerald <[email protected]> * dateS and timeS * remove real python link * dateS and timeS * remove card link to subsection page * remove rest of the card header links * convert grab and go to python block w/ output * another dateS adn timeS * top level toc ncl -> ncl to python * add links to overview * reword for cftime input caveat section * reword for cftime input caveat section * Update ncl/ncl_entries/days_in_month.ipynb Co-authored-by: Andy McKeen <[email protected]> --------- Co-authored-by: Katelyn FitzGerald <[email protected]> Co-authored-by: Andy McKeen <[email protected]>
1 parent a350e09 commit cb7719a

14 files changed

+733
-159
lines changed

applications/applications.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88
.. grid:: 1
99
:gutter: 1
1010

11-
.. card:: :ref:`applications_datetime`
11+
.. card:: Dates and Times
1212

1313
- `Working with Date and Time <applications/date_time/datetime.ipynb>`_
1414

15-
.. card:: :ref:`applications_plot_types`
15+
.. card:: Plot Types
1616

1717

1818
.. grid-item::
1919

2020
.. grid:: 1
2121
:gutter: 1
2222

23-
.. card:: :ref:`applications_data_analysis`
23+
.. card:: Data Analysis
2424

2525
.. grid-item::
2626

applications/date_time/NCL_datetime.ipynb

-63
This file was deleted.

applications/date_time/date_time.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
.. _applications_datetime:
44

5-
Date and Time
6-
=============
5+
Dates and Times
6+
===============
77

88
.. toctree::
99
:maxdepth: 1

applications/date_time/datetime.ipynb

+181-8
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,197 @@
11
{
22
"cells": [
33
{
4+
"cell_type": "markdown",
5+
"id": "b53cacffa29d30a6",
6+
"metadata": {},
7+
"source": "# Working with Dates and Times"
8+
},
9+
{
10+
"cell_type": "markdown",
11+
"id": "783a656d272204c2",
12+
"metadata": {},
13+
"source": [
14+
"## Overview\n",
15+
"Working with dates and times in Python is a common task in geoscience workflows. This notebook will cover:\n",
16+
"- Working with the [`datetime`](docs.python.org/3/library/datetime.html) module from the Python Standard Library\n",
17+
"- [`cftime`](unidata.github.io/cftime/index.html), [CF Conventions](cfconventions.org), and how they are related to working with geoscience data\n",
18+
"- A resource guide to point you to more detailed information depending on your use case"
19+
]
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"id": "13edacb9510e559e",
24+
"metadata": {},
25+
"source": [
26+
"## The `datetime` module\n",
27+
"From the [module's documentation](https://docs.python.org/3/library/datetime.html):\n",
28+
"\n",
29+
"> The `datetime` module supplies classes for manipulating dates and times.\n",
30+
">\n",
31+
"> While date and time arithmetic is supported, the focus of the implementation is on efficient attribute extraction for output formatting and manipulation."
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"id": "2092ca5d86a3a24b",
37+
"metadata": {
38+
"ExecuteTime": {
39+
"end_time": "2024-06-03T04:44:37.784336Z",
40+
"start_time": "2024-06-03T04:44:37.781266Z"
41+
}
42+
},
43+
"source": [
44+
"import datetime\n",
45+
"\n",
46+
"today = datetime.date.today()\n",
47+
"print(f\"datetime.date.today() -> \\t {today}\")\n",
48+
"now = datetime.datetime.now()\n",
49+
"print(f\"datetime.datetime.now() -> \\t {now}\")"
50+
],
51+
"outputs": [
52+
{
53+
"name": "stdout",
54+
"output_type": "stream",
55+
"text": [
56+
"datetime.date.today() -> \t 2024-06-02\n",
57+
"datetime.datetime.now() -> \t 2024-06-02 22:44:37.783255\n"
58+
]
59+
}
60+
],
61+
"execution_count": 1
62+
},
63+
{
64+
"cell_type": "markdown",
65+
"id": "6368d2ab290b3d1",
66+
"metadata": {},
67+
"source": [
68+
"### `strftime()`\n",
69+
"\n",
70+
"You can use [`strftime()`](https://docs.python.org/3/library/datetime.html#datetime.datetime.strftime) to parse and format these date objects.To see all formatting options, see the [full list of format codes](https://docs.python.org/3/library/datetime.html#format-codes)."
71+
]
72+
},
73+
{
74+
"cell_type": "code",
75+
"id": "1d18147498a434eb",
76+
"metadata": {
77+
"ExecuteTime": {
78+
"end_time": "2024-06-03T04:44:37.787843Z",
79+
"start_time": "2024-06-03T04:44:37.785520Z"
80+
}
81+
},
82+
"source": [
83+
"# print just the year\n",
84+
"print(\"now.strftime('%Y')\")\n",
85+
"print(now.strftime(\"%Y\"))\n",
86+
"print()\n",
87+
"\n",
88+
"# print weekday, month, day, year, 12h time, and AM/PM\n",
89+
"print(\"now.strftime('%A, %B %d %Y %I:%M:%S %p')\")\n",
90+
"print(now.strftime(\"%A, %B %d %Y %I:%M:%S %p\"))\n",
91+
"print()\n",
92+
"\n",
93+
"# use a shorter version to produce \"locale appropriate\" date and time\n",
94+
"print(\"now.strftime('%c')\")\n",
95+
"print(now.strftime(\"%c\"))"
96+
],
97+
"outputs": [
98+
{
99+
"name": "stdout",
100+
"output_type": "stream",
101+
"text": [
102+
"now.strftime('%Y')\n",
103+
"2024\n",
104+
"\n",
105+
"now.strftime('%A, %B %d %Y %I:%M:%S %p')\n",
106+
"Sunday, June 02 2024 10:44:37 PM\n",
107+
"\n",
108+
"now.strftime('%c')\n",
109+
"Sun Jun 2 22:44:37 2024\n"
110+
]
111+
}
112+
],
113+
"execution_count": 2
114+
},
115+
{
116+
"cell_type": "markdown",
117+
"id": "d176a3090c4730eb",
4118
"metadata": {},
119+
"source": "These functions use a [propeleptic Gregorian](https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar) calendar, though, which is not always sufficient for working with geoscience data."
120+
},
121+
{
5122
"cell_type": "markdown",
6-
"source": "# Working with Date and Time",
7-
"id": "b53cacffa29d30a6"
123+
"id": "c8a19ab9f50212c5",
124+
"metadata": {},
125+
"source": [
126+
"## cftime\n",
127+
"\n",
128+
"[`cftime`](https://unidata.github.io/cftime/) is another library for working with dates and times in Python that conforms to the [Climate and Forecasting (CF) metadata conventions](https://cfconventions.org/cf-conventions/cf-conventions#time-coordinate).\n",
129+
"\n",
130+
"Importantly, `cftime` supports all the calendars defined in the CF conventions, including `gregorian` or `standard`, which is the mixed Gregorian/Julian calendar defined by [Udunits](https://www.unidata.ucar.edu/software/udunits/), `proleptic_gregorian`, `noleap` or `365_day`, `all_leap` or `366_day`, `360_day`, and `julian`.\n",
131+
"\n",
132+
"`cftime` is often useful for working with geoscience data and can be used with [`xarray`](https://docs.xarray.dev/en/stable/generated/xarray.CFTimeIndex.html)."
133+
]
8134
},
9135
{
10136
"metadata": {},
11137
"cell_type": "markdown",
12-
"source": "## The datetime module",
13-
"id": "13edacb9510e559e"
138+
"source": "## Specific Use Cases",
139+
"id": "a710537046d5a159"
14140
},
15141
{
16142
"metadata": {},
143+
"cell_type": "markdown",
144+
"source": [
145+
"### Calculating the number of days in a month\n",
146+
"To calculate the number of days in a month, use [`cftime.datetime().daysinmonth`](https://unidata.github.io/cftime/api.html#cftime.datetime)."
147+
],
148+
"id": "de2a66ba6d34497d"
149+
},
150+
{
151+
"metadata": {
152+
"ExecuteTime": {
153+
"end_time": "2024-06-03T04:44:37.861353Z",
154+
"start_time": "2024-06-03T04:44:37.788458Z"
155+
}
156+
},
17157
"cell_type": "code",
18-
"outputs": [],
19-
"execution_count": null,
20-
"source": "",
21-
"id": "bc54610955d8ef71"
158+
"source": [
159+
"import cftime\n",
160+
"\n",
161+
"day = 1\n",
162+
"month = 6\n",
163+
"year = 2024\n",
164+
"\n",
165+
"date = cftime.datetime(year, month, day, calendar=\"standard\").daysinmonth\n",
166+
"date"
167+
],
168+
"id": "f0390a82422e4913",
169+
"outputs": [
170+
{
171+
"data": {
172+
"text/plain": [
173+
"30"
174+
]
175+
},
176+
"execution_count": 3,
177+
"metadata": {},
178+
"output_type": "execute_result"
179+
}
180+
],
181+
"execution_count": 3
182+
},
183+
{
184+
"cell_type": "markdown",
185+
"id": "1d434857c1cd81d1",
186+
"metadata": {},
187+
"source": [
188+
"## Curated Resources\n",
189+
"\n",
190+
"To learn more about working with dates and times in Python, we suggest:\n",
191+
"- the Project Pythia Foundations chapter titled [Times and Dates in Python](https://foundations.projectpythia.org/core/datetime/datetime.html)\n",
192+
"- this Xarray [documentation page on timeseries data](https://docs.xarray.dev/en/latest/user-guide/time-series.html)\n",
193+
"- the [\"How to handle time series data with ease\"](https://pandas.pydata.org/docs/getting_started/intro_tutorials/09_timeseries.html) tutorial if you're working with `pandas`"
194+
]
22195
}
23196
],
24197
"metadata": {

index.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ Python Examples
5252
.. toctree::
5353
:maxdepth: 2
5454
:hidden:
55-
:caption: NCL
55+
:caption: NCL to Python
5656

5757
NCL Index <ncl/ncl_index/ncl_index>
58-
NCL Applications <applications/ncl_applications.rst>
58+
NCL Applications <ncl/ncl_entries/ncl_entries.rst>
5959

6060
.. toctree::
6161
:maxdepth: 2

0 commit comments

Comments
 (0)