Tools Borrowed Only in Spring
Problem
DataFrame: `tool` (`tool.csv`)
```text
+-----------+--------+
| Column | Type |
+-----------+--------+
| tool_id | int |
| tool_name | object |
| deposit | int |
+-----------+--------+
tool_id uniquely identifies each tool.
```
DataFrame: `loan` (`loan.csv`)
```text
+-------------+--------+
| Column | Type |
+-------------+--------+
| clerk_id | int |
| tool_id | int |
| borrower_id | int |
| lent_on | object |
| nights | int |
| fee | int |
+-------------+--------+
Each row records one loan of a tool. `lent_on` is an ISO date string (YYYY-MM-DD).
```
Write a function that returns the `tool_id` and `tool_name` of every tool whose loans **all** fall within spring 2022 — that is, every `lent_on` date is on or after `2022-03-01` and on or before `2022-05-31`. A tool with even one loan outside that window must be excluded. Only consider tools that were loaned at least once.
Return the rows in any order.
**Example**
Input — `tool`:
```text
tool_id tool_name deposit
1 Drill 1000
2 Ladder 800
3 Mower 1400
```
Input — `loan`:
```text
clerk_id tool_id borrower_id lent_on nights fee
1 1 1 2022-03-21 2 2000
1 2 2 2022-04-17 1 800
2 2 3 2022-08-02 1 800
3 3 4 2022-07-13 2 2800
```
Output:
```text
tool_id tool_name
1 Drill
```
The Drill's only loan (2022-03-21) is in spring. The Ladder has an August loan, and the Mower's only loan is in July, so both are excluded.
Input data
Example rows — the live problem includes the full dataset.
| tool_id | tool_name | deposit |
|---|---|---|
| 1 | Drill | 1000 |
| 2 | Ladder | 800 |
| 3 | Mower | 1400 |
| clerk_id | tool_id | borrower_id | lent_on | nights | fee |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 2022-03-21 | 2 | 2000 |
| 1 | 2 | 2 | 2022-04-17 | 1 | 800 |
| 2 | 2 | 3 | 2022-08-02 | 1 | 800 |
| 3 | 3 | 4 | 2022-07-13 | 2 | 2800 |
Expected output
Your answer should return 1 row with the columns tool_id, tool_name.
Starter code (Pandas (Python))
import pandas as pd
def spring_only_tools(tool: pd.DataFrame, loan: pd.DataFrame) -> pd.DataFrame:
# Your code here
return toolSolve this Pandas question free
Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock