Skip to content

Commit 8eac069

Browse files
committed
feat(pandas) add problem 1193
1 parent 2d6d4b6 commit 8eac069

File tree

6 files changed

+114
-93
lines changed

6 files changed

+114
-93
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Fiddling around with DataFusion, pandas, and PyArrow.
3535
| 1075 | [Project Employees I](https://leetcode.com/problems/project-employees-i) | Easy ||||
3636
| 1633 | [Percentage of Users Attended a Contest](https://leetcode.com/problems/percentage-of-users-attended-a-contest) | Easy ||||
3737
| 1211 | [Queries Quality and Percentage](https://leetcode.com/problems/queries-quality-and-percentage) | Easy ||||
38-
| 1193 | [Monthly Transactions I](https://leetcode.com/problems/monthly-transactions-i) | Medium || ||
38+
| 1193 | [Monthly Transactions I](https://leetcode.com/problems/monthly-transactions-i) | Medium || ||
3939
| 1174 | [Immediate Food Delivery II](https://leetcode.com/problems/immediate-food-delivery-ii) | Medium ||||
4040
| 550 | [Game Play Analysis IV](https://leetcode.com/problems/game-play-analysis-iv) | Medium ||||
4141

problems/pandas.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,43 @@ def problem_1075(project: pd.DataFrame, employee: pd.DataFrame) -> pd.DataFrame:
230230
return joined
231231

232232

233+
def problem_1193(transactions: pd.DataFrame) -> pd.DataFrame:
234+
"""Find monthly, country-wise transaction counts, totals, approved counts and sums.
235+
236+
Find for each month and country, the number of transactions and their total amount,
237+
the number of approved transactions and their total amount.
238+
239+
Return the result table in any order.
240+
241+
Parameters
242+
----------
243+
transactions : pd.DataFrame
244+
The table has information about incoming transactions.
245+
246+
Returns
247+
-------
248+
pd.DataFrame
249+
250+
"""
251+
transactions["month"] = transactions["trans_date"].dt.strftime("%Y-%m")
252+
transactions["is_approved"] = transactions["state"] == "approved"
253+
transactions["approved_amount"] = transactions["is_approved"].case_when(
254+
[
255+
(transactions["is_approved"] == True, transactions["amount"]), # noqa E712
256+
(transactions["is_approved"] == False, 0), # noqa E712
257+
]
258+
)
259+
260+
return transactions.groupby(
261+
["month", "country"], as_index=False, dropna=False
262+
).aggregate(
263+
trans_count=pd.NamedAgg("id", "count"),
264+
approved_count=pd.NamedAgg("is_approved", "sum"),
265+
trans_total_amount=pd.NamedAgg("amount", "sum"),
266+
approved_total_amount=pd.NamedAgg("approved_amount", "sum"),
267+
)
268+
269+
233270
def problem_1148(views: pd.DataFrame) -> pd.DataFrame:
234271
"""Find all the authors that viewed at least one of their own articles.
235272

ruff.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
5151

5252
[lint.per-file-ignores]
5353
"problems/datasets.py" = ["D103"]
54-
"tests/*" = ["D"]
54+
"tests/*" = ["D", "F403", "F405"]
5555

5656
[format]
5757
# Like Black, use double quotes for strings.

tests/test_datafusion.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,7 @@
33
import pyarrow as pa
44
import pytest
55

6-
from problems.datafusion import (
7-
problem_176,
8-
problem_180,
9-
problem_584,
10-
problem_595,
11-
problem_620,
12-
problem_1068,
13-
problem_1148,
14-
problem_1321,
15-
problem_1378,
16-
problem_1484,
17-
problem_1517,
18-
problem_1683,
19-
problem_1757,
20-
)
6+
from problems.datafusion import *
217

228

239
@pytest.mark.parametrize(

tests/test_pandas.py

Lines changed: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,7 @@
44
import pytest
55
from pandas.testing import assert_frame_equal
66

7-
from problems.pandas import (
8-
problem_176,
9-
problem_180,
10-
problem_197,
11-
problem_570,
12-
problem_577,
13-
problem_584,
14-
problem_595,
15-
problem_620,
16-
problem_1068,
17-
problem_1075,
18-
problem_1148,
19-
problem_1211,
20-
problem_1251,
21-
problem_1280,
22-
problem_1321,
23-
problem_1327,
24-
problem_1378,
25-
problem_1517,
26-
problem_1581,
27-
problem_1633,
28-
problem_1661,
29-
problem_1683,
30-
problem_1757,
31-
problem_1934,
32-
)
7+
from problems.pandas import *
338

349

3510
@pytest.mark.parametrize(
@@ -517,6 +492,78 @@ def test_problem_1148(input_data, expected_data):
517492
)
518493

519494

495+
@pytest.mark.parametrize(
496+
"input_data, expected_data",
497+
[
498+
pytest.param(
499+
{
500+
"trans_date": [
501+
datetime(2023, 1, 1),
502+
datetime(2023, 1, 15),
503+
datetime(2023, 2, 1),
504+
],
505+
"state": ["approved", "pending", "approved"],
506+
"amount": [100, 200, 300],
507+
"country": ["US", "US", "CA"],
508+
"id": [1, 2, 3],
509+
},
510+
{
511+
"month": ["2023-01", "2023-02"],
512+
"country": ["US", "CA"],
513+
"trans_count": [2, 1],
514+
"approved_count": [1, 1],
515+
"trans_total_amount": [300, 300],
516+
"approved_total_amount": [100, 300],
517+
},
518+
id="happy_path_1",
519+
),
520+
pytest.param(
521+
{
522+
"trans_date": [datetime(2023, 3, 1)],
523+
"state": ["approved"],
524+
"amount": [500],
525+
"country": [None],
526+
"id": [4],
527+
},
528+
{
529+
"month": ["2023-03"],
530+
"country": [float("nan")],
531+
"trans_count": [1],
532+
"approved_count": [1],
533+
"trans_total_amount": [500],
534+
"approved_total_amount": [500],
535+
},
536+
id="happy_path_null_country",
537+
),
538+
pytest.param(
539+
{
540+
"trans_date": [datetime(2023, 4, 1), datetime(2023, 4, 2)],
541+
"state": ["pending", "rejected"],
542+
"amount": [150, 250],
543+
"country": ["FR", "FR"],
544+
"id": [5, 6],
545+
},
546+
{
547+
"month": ["2023-04"],
548+
"country": ["FR"],
549+
"trans_count": [2],
550+
"approved_count": [0],
551+
"trans_total_amount": [400],
552+
"approved_total_amount": [0],
553+
},
554+
id="edge_case_all_unapproved",
555+
),
556+
],
557+
)
558+
def test_problem_1193(input_data, expected_data):
559+
table = pd.DataFrame(input_data)
560+
expected_table = pd.DataFrame(expected_data)
561+
result = problem_1193(table).reset_index(drop=True)
562+
assert_frame_equal(
563+
result, expected_table, check_dtype=False, check_index_type=False
564+
)
565+
566+
520567
@pytest.mark.parametrize(
521568
"input_data, expected_data",
522569
[

tests/test_pyarrow.py

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,7 @@
33
import pyarrow as pa
44
import pytest
55

6-
from problems.pyarrow import (
7-
problem_176,
8-
problem_180,
9-
problem_185,
10-
problem_196,
11-
problem_197,
12-
problem_550,
13-
problem_570,
14-
problem_577,
15-
problem_584,
16-
problem_585,
17-
problem_595,
18-
problem_596,
19-
problem_602,
20-
problem_610,
21-
problem_619,
22-
problem_620,
23-
problem_626,
24-
problem_1045,
25-
problem_1068,
26-
problem_1070,
27-
problem_1075,
28-
problem_1141,
29-
problem_1148,
30-
problem_1164,
31-
problem_1174,
32-
problem_1193,
33-
problem_1204,
34-
problem_1211,
35-
problem_1251,
36-
problem_1280,
37-
problem_1327,
38-
problem_1341,
39-
problem_1378,
40-
problem_1517,
41-
problem_1527,
42-
problem_1581,
43-
problem_1633,
44-
problem_1661,
45-
problem_1667,
46-
problem_1683,
47-
problem_1729,
48-
problem_1731,
49-
problem_1757,
50-
problem_1789,
51-
problem_1907,
52-
problem_1934,
53-
problem_1978,
54-
problem_2356,
55-
)
6+
from problems.pyarrow import *
567

578

589
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)