Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

test_clean_pmt_history.py 17 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
  1. '''
  2. Tests for clean_pmt_history.py which contains functions used in 04_clean_pmt_history.py
  3. '''
  4. import numpy as np
  5. import pandas as pd
  6. import pandas.api.types as ptypes
  7. from lendingclub.csv_preparation import clean_pmt_history as cph
  8. def test_detect_strange_pmt_hist():
  9. test_cases = [
  10. pd.DataFrame({
  11. 'RECEIVED_D': {
  12. 15049: 'APR2014'
  13. },
  14. 'PERIOD_END_LSTAT': {
  15. 15049: 'Current'
  16. },
  17. 'MONTH': {
  18. 15049: 'MAR2014'
  19. },
  20. 'IssuedDate': {
  21. 15049: 'FEB2014'
  22. },
  23. 'State': {
  24. 15049: 'VA'
  25. },
  26. 'HomeOwnership': {
  27. 15049: 'MORTGAGE'
  28. },
  29. 'EarliestCREDITLine': {
  30. 15049: 'OCT1994'
  31. },
  32. 'EmploymentLength': {
  33. 15049: '10+ years'
  34. },
  35. 'grade': {
  36. 15049: 'E'
  37. },
  38. 'APPL_FICO_BAND': {
  39. 15049: '675-679'
  40. },
  41. 'Last_FICO_BAND': {
  42. 15049: '700-704'
  43. },
  44. 'VINTAGE': {
  45. 15049: '14Q1'
  46. },
  47. 'LOAN_ID': {
  48. 15049: 12287070
  49. },
  50. 'PBAL_BEG_PERIOD': {
  51. 15049: 27600.0
  52. },
  53. 'PRNCP_PAID': {
  54. 15049: 267.510009765625
  55. },
  56. 'INT_PAID': {
  57. 15049: 471.2699890136719
  58. },
  59. 'FEE_PAID': {
  60. 15049: 0.0
  61. },
  62. 'DUE_AMT': {
  63. 15049: 738.780029296875
  64. },
  65. 'RECEIVED_AMT': {
  66. 15049: 738.780029296875
  67. },
  68. 'PBAL_END_PERIOD': {
  69. 15049: 27332.490234375
  70. },
  71. 'MOB': {
  72. 15049: 1
  73. },
  74. 'CO': {
  75. 15049: 0
  76. },
  77. 'COAMT': {
  78. 15049: 0.0
  79. },
  80. 'InterestRate': {
  81. 15049: 0.05999999865889549
  82. },
  83. 'MONTHLYCONTRACTAMT': {
  84. 15049: 614.2999877929688
  85. },
  86. 'dti': {
  87. 15049: 22.079999923706055
  88. },
  89. 'MonthlyIncome': {
  90. 15049: 6250.0
  91. },
  92. 'OpenCREDITLines': {
  93. 15049: 17.0
  94. },
  95. 'TotalCREDITLines': {
  96. 15049: 47.0
  97. },
  98. 'RevolvingCREDITBalance': {
  99. 15049: 23483
  100. },
  101. 'RevolvingLineUtilization': {
  102. 15049: 0.6909999847412109
  103. },
  104. 'Inquiries6M': {
  105. 15049: 3.0
  106. },
  107. 'DQ2yrs': {
  108. 15049: 0.0
  109. },
  110. 'MonthsSinceDQ': {
  111. 15049: np.nan
  112. },
  113. 'PublicRec': {
  114. 15049: 0.0
  115. },
  116. 'MonthsSinceLastRec': {
  117. 15049: np.nan
  118. },
  119. 'currentpolicy': {
  120. 15049: 1
  121. },
  122. 'term': {
  123. 15049: 60
  124. },
  125. 'PCO_RECOVERY': {
  126. 15049: np.nan
  127. },
  128. 'PCO_COLLECTION_FEE': {
  129. 15049: np.nan
  130. }
  131. }),
  132. pd.DataFrame({
  133. 'RECEIVED_D': {
  134. 0: 'SEP2007'
  135. },
  136. 'PERIOD_END_LSTAT': {
  137. 0: 'Current'
  138. },
  139. 'MONTH': {
  140. 0: 'SEP2007'
  141. },
  142. 'IssuedDate': {
  143. 0: 'AUG2007'
  144. },
  145. 'State': {
  146. 0: 'FL'
  147. },
  148. 'HomeOwnership': {
  149. 0: 'RENT'
  150. },
  151. 'EarliestCREDITLine': {
  152. 0: 'MAY2005'
  153. },
  154. 'EmploymentLength': {
  155. 0: '1 year'
  156. },
  157. 'grade': {
  158. 0: 'G'
  159. },
  160. 'APPL_FICO_BAND': {
  161. 0: '640-644'
  162. },
  163. 'Last_FICO_BAND': {
  164. 0: '640-644'
  165. },
  166. 'VINTAGE': {
  167. 0: '07Q3'
  168. },
  169. 'LOAN_ID': {
  170. 0: 114469
  171. },
  172. 'PBAL_BEG_PERIOD': {
  173. 0: 13000.0
  174. },
  175. 'PRNCP_PAID': {
  176. 0: 278.3390808105469
  177. },
  178. 'INT_PAID': {
  179. 0: 186.60092163085938
  180. },
  181. 'FEE_PAID': {
  182. 0: 0.0
  183. },
  184. 'DUE_AMT': {
  185. 0: 464.95001220703125
  186. },
  187. 'RECEIVED_AMT': {
  188. 0: 464.94000244140625
  189. },
  190. 'PBAL_END_PERIOD': {
  191. 0: 12721.6611328125
  192. },
  193. 'MOB': {
  194. 0: 1
  195. },
  196. 'CO': {
  197. 0: 0
  198. },
  199. 'COAMT': {
  200. 0: 0.0
  201. },
  202. 'InterestRate': {
  203. 0: 0.17219999432563782
  204. },
  205. 'MONTHLYCONTRACTAMT': {
  206. 0: 464.95001220703125
  207. },
  208. 'dti': {
  209. 0: 15.979999542236328
  210. },
  211. 'MonthlyIncome': {
  212. 0: 2666.666748046875
  213. },
  214. 'OpenCREDITLines': {
  215. 0: 6.0
  216. },
  217. 'TotalCREDITLines': {
  218. 0: 7.0
  219. },
  220. 'RevolvingCREDITBalance': {
  221. 0: 6703
  222. },
  223. 'RevolvingLineUtilization': {
  224. 0: 0.7979999780654907
  225. },
  226. 'Inquiries6M': {
  227. 0: 1.0
  228. },
  229. 'DQ2yrs': {
  230. 0: 0.0
  231. },
  232. 'MonthsSinceDQ': {
  233. 0: 0.0
  234. },
  235. 'PublicRec': {
  236. 0: 0.0
  237. },
  238. 'MonthsSinceLastRec': {
  239. 0: 0.0
  240. },
  241. 'currentpolicy': {
  242. 0: 0
  243. },
  244. 'term': {
  245. 0: 36
  246. },
  247. 'PCO_RECOVERY': {
  248. 0: np.nan
  249. },
  250. 'PCO_COLLECTION_FEE': {
  251. 0: np.nan
  252. }
  253. })
  254. ]
  255. assert cph.detect_strange_pmt_hist(test_cases[0])
  256. assert not cph.detect_strange_pmt_hist(test_cases[1])
  257. def test_pmt_hist_fmt_date():
  258. test_cases = [
  259. pd.DataFrame({
  260. 'pmt_date': {
  261. 0: 'SEP2007'
  262. },
  263. 'status_period_end': {
  264. 0: 'Current'
  265. },
  266. 'date': {
  267. 0: 'SEP2007'
  268. },
  269. 'issue_d': {
  270. 0: 'AUG2007'
  271. },
  272. 'addr_state': {
  273. 0: 'FL'
  274. },
  275. 'home_ownership': {
  276. 0: 'RENT'
  277. },
  278. 'first_credit_line': {
  279. 0: 'MAY2005'
  280. },
  281. 'emp_len': {
  282. 0: '1 year'
  283. },
  284. 'grade': {
  285. 0: 'G'
  286. },
  287. 'fico_apply': {
  288. 0: '640-644'
  289. },
  290. 'fico_last': {
  291. 0: '640-644'
  292. },
  293. 'vintage': {
  294. 0: '07Q3'
  295. },
  296. 'loan_id': {
  297. 0: 114469
  298. },
  299. 'outs_princp_beg': {
  300. 0: 13000.0
  301. },
  302. 'princp_paid': {
  303. 0: 278.3389892578125
  304. },
  305. 'int_paid': {
  306. 0: 186.6009979248047
  307. },
  308. 'fee_paid': {
  309. 0: 0.0
  310. },
  311. 'amt_due': {
  312. 0: 464.95001220703125
  313. },
  314. 'amt_paid': {
  315. 0: 464.94000244140625
  316. },
  317. 'outs_princp_end': {
  318. 0: 12721.6611328125
  319. },
  320. 'm_on_books': {
  321. 0: 1
  322. },
  323. 'charged_off_this_month': {
  324. 0: 0
  325. },
  326. 'charged_off_amt': {
  327. 0: 0.0
  328. },
  329. 'int_rate': {
  330. 0: 0.1720000058412552
  331. },
  332. 'monthly_pmt': {
  333. 0: 464.95001220703125
  334. },
  335. 'dti': {
  336. 0: 15.979999542236328
  337. },
  338. 'm_income': {
  339. 0: 2666.6669921875
  340. },
  341. 'open_credit_lines': {
  342. 0: 6.0
  343. },
  344. 'total_credit_lines': {
  345. 0: 7.0
  346. },
  347. 'revol_credit_bal': {
  348. 0: 6703
  349. },
  350. 'revol_line_util': {
  351. 0: 0.7979999780654907
  352. },
  353. 'inq_6m': {
  354. 0: 1.0
  355. },
  356. 'dq_24m': {
  357. 0: 0.0
  358. },
  359. 'm_since_dq': {
  360. 0: 0.0
  361. },
  362. 'public_recs': {
  363. 0: 0.0
  364. },
  365. 'm_since_rec': {
  366. 0: 0.0
  367. },
  368. 'current_policy': {
  369. 0: 0
  370. },
  371. 'term': {
  372. 0: 36
  373. },
  374. 'recovs': {
  375. 0: np.nan
  376. },
  377. 'recov_fees': {
  378. 0: np.nan
  379. },
  380. 'calc_amt_paid': {
  381. 0: 464.94000244140625
  382. }
  383. })
  384. ]
  385. test_col = 'date'
  386. cph.pmt_hist_fmt_date(test_cases[0], test_col)
  387. assert ptypes.is_datetime64_any_dtype(test_cases[0][test_col])
  388. def test_merge_dupe_dates():
  389. test_cases = [(pd.DataFrame({
  390. 'pmt_date': {
  391. 7060: pd.NaT,
  392. 7061: pd.NaT
  393. },
  394. 'status_period_end': {
  395. 7060: 'defaulted',
  396. 7061: 'charged_off'
  397. },
  398. 'date': {
  399. 7060: pd.Timestamp('2014-07-01 00:00:00'),
  400. 7061: pd.Timestamp('2014-07-01 00:00:00')
  401. },
  402. 'issue_d': {
  403. 7060: pd.Timestamp('2013-03-01 00:00:00'),
  404. 7061: pd.Timestamp('2013-03-01 00:00:00')
  405. },
  406. 'addr_state': {
  407. 7060: 'FL',
  408. 7061: 'FL'
  409. },
  410. 'home_ownership': {
  411. 7060: 'mortgage',
  412. 7061: 'mortgage'
  413. },
  414. 'first_credit_line': {
  415. 7060: pd.Timestamp('1999-09-01 00:00:00'),
  416. 7061: pd.Timestamp('1999-09-01 00:00:00')
  417. },
  418. 'emp_len': {
  419. 7060: '< 1 year',
  420. 7061: '< 1 year'
  421. },
  422. 'grade': {
  423. 7060: 'B',
  424. 7061: 'B'
  425. },
  426. 'vintage': {
  427. 7060: '13Q1',
  428. 7061: '13Q1'
  429. },
  430. 'outs_princp_beg': {
  431. 7060: 5649.046875,
  432. 7061: 2000
  433. },
  434. 'princp_paid': {
  435. 7060: 5,
  436. 7061: 0.0
  437. },
  438. 'int_paid': {
  439. 7060: 5,
  440. 7061: 0.0
  441. },
  442. 'fee_paid': {
  443. 7060: 5,
  444. 7061: 0.0
  445. },
  446. 'amt_due': {
  447. 7060: 1031.9599609375,
  448. 7061: 1286.199951171875
  449. },
  450. 'amt_paid': {
  451. 7060: 15,
  452. 7061: 0.0
  453. },
  454. 'outs_princp_end': {
  455. 7060: 5649.046875,
  456. 7061: 5649.046875
  457. },
  458. 'm_on_books': {
  459. 7060: 15,
  460. 7061: 16
  461. },
  462. 'charged_off_this_month': {
  463. 7060: 0,
  464. 7061: 1
  465. },
  466. 'charged_off_amt': {
  467. 7060: 10,
  468. 7061: 20
  469. },
  470. 'int_rate': {
  471. 7060: 0.11100000143051147,
  472. 7061: 0.11100000143051147
  473. },
  474. 'monthly_pmt': {
  475. 7060: 254.24000549316406,
  476. 7061: 254.24000549316406
  477. },
  478. 'dti': {
  479. 7060: 32.36000061035156,
  480. 7061: 32.36000061035156
  481. },
  482. 'm_income': {
  483. 7060: 6066.666015625,
  484. 7061: 6066.666015625
  485. },
  486. 'open_credit_lines': {
  487. 7060: 13.0,
  488. 7061: 13.0
  489. },
  490. 'total_credit_lines': {
  491. 7060: 51.0,
  492. 7061: 51.0
  493. },
  494. 'revol_line_util': {
  495. 7060: 0.6439999938011169,
  496. 7061: 0.6439999938011169
  497. },
  498. 'inq_6m': {
  499. 7060: 3.0,
  500. 7061: 3.0
  501. },
  502. 'dq_24m': {
  503. 7060: 0.0,
  504. 7061: 0.0
  505. },
  506. 'm_since_dq': {
  507. 7060: 55.0,
  508. 7061: 55.0
  509. },
  510. 'public_recs': {
  511. 7060: 0.0,
  512. 7061: 0.0
  513. },
  514. 'm_since_rec': {
  515. 7060: np.nan,
  516. 7061: np.nan
  517. },
  518. 'current_policy': {
  519. 7060: 1,
  520. 7061: 1
  521. },
  522. 'term': {
  523. 7060: 36,
  524. 7061: 36
  525. },
  526. 'recovs': {
  527. 7060: 20,
  528. 7061: 30
  529. },
  530. 'recov_fees': {
  531. 7060: 15,
  532. 7061: 25
  533. },
  534. 'all_cash_to_inv': {
  535. 7060: 100,
  536. 7061: 200
  537. },
  538. 'fico_apply': {
  539. 7060: 712,
  540. 7061: 712
  541. },
  542. 'fico_last': {
  543. 7060: 587,
  544. 7061: 582
  545. },
  546. 'loan_id': {
  547. 7060: 3626698,
  548. 7061: 3626698
  549. },
  550. 'revol_credit_bal': {
  551. 7060: 31549.0,
  552. 7061: 31549.0
  553. }
  554. }), {
  555. 'pmt_date': 0,
  556. 'status_period_end': 1,
  557. 'date': 2,
  558. 'issue_d': 3,
  559. 'addr_state': 4,
  560. 'home_ownership': 5,
  561. 'first_credit_line': 6,
  562. 'emp_len': 7,
  563. 'grade': 8,
  564. 'vintage': 9,
  565. 'outs_princp_beg': 10,
  566. 'princp_paid': 11,
  567. 'int_paid': 12,
  568. 'fee_paid': 13,
  569. 'amt_due': 14,
  570. 'amt_paid': 15,
  571. 'outs_princp_end': 16,
  572. 'm_on_books': 17,
  573. 'charged_off_this_month': 18,
  574. 'charged_off_amt': 19,
  575. 'int_rate': 20,
  576. 'monthly_pmt': 21,
  577. 'dti': 22,
  578. 'm_income': 23,
  579. 'open_credit_lines': 24,
  580. 'total_credit_lines': 25,
  581. 'revol_line_util': 26,
  582. 'inq_6m': 27,
  583. 'dq_24m': 28,
  584. 'm_since_dq': 29,
  585. 'public_recs': 30,
  586. 'm_since_rec': 31,
  587. 'current_policy': 32,
  588. 'term': 33,
  589. 'recovs': 34,
  590. 'recov_fees': 35,
  591. 'all_cash_to_inv': 36,
  592. 'fico_apply': 37,
  593. 'fico_last': 38,
  594. 'loan_id': 39,
  595. 'revol_credit_bal': 40
  596. })]
  597. ex = cph.merge_dupe_dates(*test_cases[0])
  598. assert ex.shape[0] == 1
  599. assert ex['outs_princp_beg'].iloc[0] == 5649.046875
  600. assert ex['princp_paid'].iloc[0] == 5
  601. assert ex['int_paid'].iloc[0] == 5
  602. assert ex['fee_paid'].iloc[0] == 5
  603. assert ex['amt_paid'].iloc[0] == 15
  604. assert ex['charged_off_this_month'].iloc[0] == 1
  605. assert ex['charged_off_amt'].iloc[0] == 30
  606. assert ex['recovs'].iloc[0] == 50
  607. assert ex['recov_fees'].iloc[0] == 40
  608. assert ex['all_cash_to_inv'].iloc[0] == 300
  609. def test_insert_missing_dates():
  610. test_cases = [
  611. (pd.DataFrame(
  612. {'pmt_date': {7059: pd.NaT, 7061: pd.NaT},
  613. 'status_period_end': {7059: 'late_120', 7061: 'charged_off'},
  614. 'date': {7059: pd.Timestamp('2014-05-01 00:00:00'),
  615. 7061: pd.Timestamp('2014-07-01 00:00:00')},
  616. 'issue_d': {7059: pd.Timestamp('2013-03-01 00:00:00'),
  617. 7061: pd.Timestamp('2013-03-01 00:00:00')},
  618. 'addr_state': {7059: 'FL', 7061: 'FL'},
  619. 'home_ownership': {7059: 'mortgage', 7061: 'mortgage'},
  620. 'first_credit_line': {7059: pd.Timestamp('1999-09-01 00:00:00'),
  621. 7061: pd.Timestamp('1999-09-01 00:00:00')},
  622. 'emp_len': {7059: '< 1 year', 7061: '< 1 year'},
  623. 'grade': {7059: 'B', 7061: 'B'},
  624. 'vintage': {7059: '13Q1', 7061: '13Q1'},
  625. 'outs_princp_beg': {7059: 5649.046875, 7061: 5649.046875},
  626. 'princp_paid': {7059: 0.0, 7061: 0.0},
  627. 'int_paid': {7059: 0.0, 7061: 0.0},
  628. 'fee_paid': {7059: 0.0, 7061: 0.0},
  629. 'amt_due': {7059: 777.719970703125, 7061: 1286.199951171875},
  630. 'amt_paid': {7059: 0.0, 7061: 0.0},
  631. 'outs_princp_end': {7059: 5649.046875, 7061: 5649.046875},
  632. 'm_on_books': {7059: 14, 7061: 16},
  633. 'charged_off_this_month': {7059: 0.0, 7061: 1.0},
  634. 'charged_off_amt': {7059: 0.0, 7061: 5649.046875},
  635. 'int_rate': {7059: 0.11100000143051147, 7061: 0.11100000143051147},
  636. 'monthly_pmt': {7059: 254.24000549316406, 7061: 254.24000549316406},
  637. 'dti': {7059: 32.36000061035156, 7061: 32.36000061035156},
  638. 'm_income': {7059: 6066.666015625, 7061: 6066.666015625},
  639. 'open_credit_lines': {7059: 13.0, 7061: 13.0},
  640. 'total_credit_lines': {7059: 51.0, 7061: 51.0},
  641. 'revol_line_util': {7059: 0.6439999938011169, 7061: 0.6439999938011169},
  642. 'inq_6m': {7059: 3.0, 7061: 3.0},
  643. 'dq_24m': {7059: 0.0, 7061: 0.0},
  644. 'm_since_dq': {7059: 55.0, 7061: 55.0},
  645. 'public_recs': {7059: 0.0, 7061: 0.0},
  646. 'm_since_rec': {7059: np.nan, 7061: np.nan},
  647. 'current_policy': {7059: 1, 7061: 1},
  648. 'term': {7059: 36, 7061: 36},
  649. 'recovs': {7059: 0.0, 7061: 786.4600219726562},
  650. 'recov_fees': {7059: 0.0, 7061: 7.861999988555908},
  651. 'all_cash_to_inv': {7059: 0.0, 7061: 778.5980224609375},
  652. 'fico_apply': {7059: 712, 7061: 712},
  653. 'fico_last': {7059: 587, 7061: 582},
  654. 'loan_id': {7059: 3626698, 7061: 3626698},
  655. 'revol_credit_bal': {7059: 31549.0, 7061: 31549.0}}), 3626698)
  656. ]
  657. ex = cph.insert_missing_dates(*test_cases[0], verbose=True)
  658. assert ex.shape[0] == 3
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...