You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Activity 3, part 2: Add checks to the `format_date` function
162
162
163
-
The code below creates a `pd.DataFrame` with the first 15 publications in the JOSS sample `data.json` file. This is the first of 3 files you must process in your workflow.
163
+
The code below creates a {class}`pandas.DataFrame` with the first 15 publications in the JOSS sample `data.json` file. This is the first of 3 files you must process in your workflow.
164
164
165
-
Your first task is to process and format the `published_date` column in the data to make it a `pandas.datetime` object. Having a date in a `datetime` format will allow you to do time-related analysis on your data, such as counting publications by month and year! The expected CrossRef published date should be:
165
+
Your first task is to process and format the `published_date` column in the data to make it a {class}`pandas.Timestamp` object. Having a date in a `datetime` format like {class}`pandas.Timestamp` or {class}`datetime.datetime` will allow you to do time-related analysis on your data, such as counting publications by month and year! The expected CrossRef published date should be:
166
166
167
167
```json
168
168
"published": {
@@ -195,7 +195,7 @@ In small groups, do the following:
Let's work on formatting dates so there is a consistent format in our dataframe. Python has a [string formatting language](https://docs.python.org/3/library/string.html#formatspec) that defines useful characters for formatting.
### How to apply functions to DataFrame values: `pandas.apply()`
365
+
### How to apply functions to DataFrame values: `.apply()`
366
366
367
-
The `pandas.apply()` method allows you to apply any function to rows or columns in a `pandas.DataFrame`. For example, you can use it to perform operations on specific column or row values. When you use `.apply()`, you can specify whether you want to apply the function across columns `(axis=0)` (the default) or across rows `(axis=1)`.
367
+
The {meth}`.apply() <pandas.DataFrame.apply>` method allows you to apply any function to rows or columns in a {class}`pandas.DataFrame`. For example, you can use it to perform operations on specific column or row values. When you use `.apply()`, you can specify whether you want to apply the function across columns `(axis=0)` (the default) or across rows `(axis=1)`.
368
368
369
369
For example, if you want to apply a function to each row of a DataFrame, you would use `df.apply(your_function, axis=1)`. This function is especially useful for applying logic that can’t be easily achieved with built-in pandas functions, allowing for more flexibility in data processing.
370
370
371
-
You can use `.apply` in pandas to efficiently replace `for loops` to process row and column values in a `pandas.DataFrame`.
371
+
You can use `.apply` in pandas to efficiently replace `for loops` to process row and column values in a {class}`pandas.DataFrame`.
372
372
373
373
```python
374
374
# Apply the format_date function to every row in the published_date column
Copy file name to clipboardExpand all lines: clean-modular-code/python-pep-8.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -325,7 +325,7 @@ Most text editors allow you to set up guides to see how long your code is. You c
325
325
* PEP 8 recommends organizing imports in the following order:
326
326
327
327
1. **Standard Library Imports**: These built-in modules come with Python, such as `os` and `glob`. You can find the full list [here](https://docs.python.org/3/library/index.html).
328
-
2. **Third-Party Imports**: Libraries that you install via `pip`, like `numpy` and `pandas`.
328
+
2. **Third-Party Imports**: Libraries that you install via `pip`, like {mod}`numpy` and {mod}`pandas`.
329
329
3. **Local Imports**: Code or modules specific to your project.
Copy file name to clipboardExpand all lines: code-workflow-logic/about-python-functions.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -71,9 +71,9 @@ Source: Francois Michonneau.
71
71
72
72
A well-defined function only does one thing but does it well and often in various contexts. Often, the operations in a good function are useful for many tasks.
73
73
74
-
Take, for instance, the **numpy** function called `mean()`, which computes mean values from a **numpy** array.
74
+
Take, for instance, the **{mod}`numpy`** function called {func}`~numpy.mean`, which computes mean values from a **numpy** array.
75
75
76
-
This function only does one thing-- it computes a mean. However, you may use the `np.mean()` function many times in your code on multiple **numpy** arrays because it has been defined to take any **numpy** array as an input.
76
+
This function only does one thing-- it computes a mean. However, you may use the {func}`np.mean() <numpy.mean>` function many times in your code on multiple **numpy** arrays because it has been defined to take any **numpy** array as an input.
The `np.mean()` function is modular, and it can be easily combined with other functions to accomplish various tasks.
91
+
The {func}`numpy.mean()` function is modular, and it can be easily combined with other functions to accomplish various tasks.
92
92
93
93
When you write modular functions, you can reuse them for other workflows and projects. Some people even write their own **Python** packages for personal and professional use that contain custom functions for tasks that they have to complete regularly.
When the function is called, a user can provide any value for `data_1` or `data_2` as input for that parameter (e.g., single-value variable, list, **numpy** array, **pandas** data frame column).
53
+
When the function is called, a user can provide any value for `data_1` or `data_2` as input for that parameter (e.g., single-value variable, list, {class}`numpy.ndarray`, {class}`pandas.DataFrame` column).
54
54
55
55
56
56
## Write a Function with Multiple Parameters in Python
@@ -66,6 +66,7 @@ def multiply_values():
66
66
67
67
Next, provide two placeholder variable names for the input parameters, as shown below.
68
68
69
+
69
70
```python
70
71
defmultiply_values(x, y):
71
72
```
@@ -163,7 +164,6 @@ Use placeholder variable names that highlight the purpose of each parameter:
163
164
defmean_mm_to_in(data_mm, axis_value):
164
165
```
165
166
166
-
167
167
Next, add the code to first calculate the mean of the input array along a specified axis, and then to convert the mean values from millimeters to inches.
168
168
169
169
First, add the code line to calculate a mean along a specified axis.
@@ -387,8 +387,8 @@ monthly_mean_in
387
387
You can also write multi-parameter functions to combine other tasks into one function, such as downloading and reading data files into a **pandas** dataframe.
388
388
389
389
Think about the code that you need to include in the function:
390
-
1. download data file from URL: `et.data.get_data(url=file_url)`
391
-
2. read data file into **pandas** dataframe: `pd.read_csv(path)`
390
+
1. download data file from URL: {func}`et.data.get_data(url=file_url) <earthpy.data.get_data>`
391
+
2. read data file into **pandas** dataframe: {func}`pd.read_csv(path) <pandas.read_csv>`
392
392
393
393
From this code, you can see that you will need two input parameters for the combined function:
Copy file name to clipboardExpand all lines: code-workflow-logic/write-python-functions.md
+5-8Lines changed: 5 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -88,22 +88,19 @@ In **Python**, the required parameters are provided within parenthesis `()`, as
88
88
89
89
```python
90
90
deffunction_name(parameter):
91
-
```
91
+
```
92
92
93
93
You can define an input parameter for a function using a placeholder variable, such as `data`, which represents the value or object that will be acted upon in the function.
94
94
95
-
96
95
```python
97
96
deffunction_name(data):
98
-
```
97
+
```
99
98
100
99
You can define a function using multiple parameters.
101
100
102
-
103
101
```python
104
102
defadd_numbers(num_1, num_2):
105
-
```
106
-
103
+
```
107
104
108
105
### Return Statement
109
106
@@ -214,7 +211,7 @@ Can the function `mm_to_in()` to take a list as an input? Look again at the code
Just like you can call `help()` on a function provided by a **Python** package such as **pandas** (e.g. `help(pd.DataFrame)`, you can also call `help()` on custom functions.
214
+
Just like you can call `help()` on a function provided by a **Python** package such as **{mod}`pandas`** (e.g. {class}`help(pd.DataFrame) <pandas.DataFrame>`), you can also call `help()` on your own custom functions.
Notice that when you call `help()` on custom functions, you will see the docstring that was provided in the function definition.
233
230
234
-
The `help()` results for `np.mean` are simply longer because the docstring contains more information, such as sections for Notes and Examples.
231
+
The `help()` results for {func}`np.mean <numpy.mean>` are simply longer because the docstring contains more information, such as sections for Notes and Examples.
235
232
236
233
Combining related function calls into a single line of code allows you to write code that is much more efficient and less repetitive, assisting you in writing DRY code in **Python**.
0 commit comments