Skip to content

Commit d16cd87

Browse files
committed
feat(pandas): add problem 1581
1 parent 34f85c7 commit d16cd87

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Fiddling around with DataFusion, pandas, and PyArrow.
1818
|----------:|:------|:-----------|:-------:|:----------:|:------:|
1919
| 1378 | [Replace Employee ID With The Unique Identifier](https://leetcode.com/problems/replace-employee-id-with-the-unique-identifier) | Easy ||||
2020
| 1068 | [Product Sales Analysis I](https://leetcode.com/problems/product-sales-analysis-i) | Easy ||||
21-
| 1581 | [Customer Who Visited but Did Not Make Any Transactions](https://leetcode.com/problems/customer-who-visited-but-did-not-make-any-transactions) | Easy ||| |
21+
| 1581 | [Customer Who Visited but Did Not Make Any Transactions](https://leetcode.com/problems/customer-who-visited-but-did-not-make-any-transactions) | Easy ||| |
2222
| 197 | [Rising Temperature](https://leetcode.com/problems/rising-temperature) | Easy ||||
2323
| 1661 | [Average Time of Process per Machine](https://leetcode.com/problems/average-time-of-process-per-machine) | Easy ||||
2424
| 577 | [Employee Bonus](https://leetcode.com/problems/employee-bonus) | Easy ||||

problems/pandas.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,32 @@ def problem_1378(employees: pd.DataFrame, employee_uni: pd.DataFrame) -> pd.Data
199199
return employees.merge(employee_uni, how="left", on="id")[["unique_id", "name"]]
200200

201201

202+
def problem_1581(visits: pd.DataFrame, transactions: pd.DataFrame) -> pd.DataFrame:
203+
"""Find users who visited without transactions and count their visit frequency.
204+
205+
Return the result table sorted in any order.
206+
207+
Parameters
208+
----------
209+
visits : pd.DataFrame
210+
Table containing information about the customers who visited the mall.
211+
transactions : pd.DataFrame
212+
Table containing information about the transactions made during the visit_id.
213+
214+
Returns
215+
-------
216+
pd.DataFrame
217+
218+
"""
219+
joined = visits.merge(transactions, how="left")
220+
return (
221+
joined.loc[joined["transaction_id"].isnull(), ["visit_id", "customer_id"]]
222+
.groupby("customer_id", as_index=False)
223+
.count()
224+
.rename(columns={"visit_id": "count_no_trans"})
225+
)
226+
227+
202228
def problem_1683(tweets: pd.DataFrame) -> pd.DataFrame:
203229
"""Find the IDs of the invalid tweets.
204230

tests/test_pandas.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pandas as pd
44
import pytest
5+
from pandas.testing import assert_frame_equal
56

67
from problems.pandas import (
78
problem_176,
@@ -12,6 +13,7 @@
1213
problem_1148,
1314
problem_1321,
1415
problem_1378,
16+
problem_1581,
1517
problem_1683,
1618
problem_1757,
1719
)
@@ -425,6 +427,41 @@ def test_problem_1378(input_data_1, input_data_2, expected_data):
425427
assert result.equals(expected_table)
426428

427429

430+
@pytest.mark.parametrize(
431+
"input_data_1, input_data_2, expected_data",
432+
[
433+
pytest.param(
434+
{
435+
"visit_id": [1, 2, 5, 5, 5, 4, 6, 7, 8],
436+
"customer_id": [23, 9, 54, 54, 54, 30, 96, 54, 54],
437+
},
438+
{
439+
"visit_id": [1, 2, 5],
440+
"transaction_id": [12, 13, 9],
441+
"amount": [910, 970, 200],
442+
},
443+
{"customer_id": [30, 96, 54], "count_no_trans": [1, 1, 2]},
444+
id="happy_path",
445+
),
446+
pytest.param(
447+
{"visit_id": [10, 11], "customer_id": [100, 101]},
448+
{"visit_id": [1, 2], "transaction_id": [12, 13], "amount": [910, 970]},
449+
{"customer_id": [100, 101], "count_no_trans": [1, 1]},
450+
id="no_matching_visit_id",
451+
),
452+
],
453+
)
454+
def test_problem_1581(input_data_1, input_data_2, expected_data):
455+
table_1 = pd.DataFrame(input_data_1)
456+
table_2 = pd.DataFrame(input_data_2)
457+
expected_table = (
458+
pd.DataFrame(expected_data).sort_values(by="customer_id").reset_index(drop=True)
459+
)
460+
result = problem_1581(table_1, table_2).sort_values(by="customer_id")
461+
462+
assert_frame_equal(result, expected_table, check_dtype=False, check_index_type=True)
463+
464+
428465
@pytest.mark.parametrize(
429466
"input_data, expected_data",
430467
[

0 commit comments

Comments
 (0)