File size: 31,124 Bytes
75890c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # creating a function which takes in a game id and returns the dataframe of the game's play by play data\n",
    "\n",
    "\n",
    "# def get_game_pbp(game_id):\n",
    "#     # Construct the URL for the play-by-play endpoint\n",
    "#     url = f\"https://api.sportradar.us/nba/trial/v8/en/games/{game_id}/pbp.json?api_key={api_key}\"\n",
    "\n",
    "#     # Make a GET request to the endpoint\n",
    "#     game_data = requests.get(url)\n",
    "\n",
    "#     # Check if the request was successful\n",
    "#     if game_data.status_code == 200:\n",
    "#         # Convert the response to JSON\n",
    "#         data = game_data.json()\n",
    "\n",
    "#         # Extract the relevant information\n",
    "#         scheduled = data[\"scheduled\"]  # This is when the game was scheduled\n",
    "#         home_team_name = data[\"home\"][\"market\"] + \" \" + data[\"home\"][\"name\"]\n",
    "#         away_team_name = data[\"away\"][\"market\"] + \" \" + data[\"away\"][\"name\"]\n",
    "#         descriptions = []\n",
    "#         clocks = []\n",
    "#         event_types = []\n",
    "#         home_points = []\n",
    "#         away_points = []\n",
    "#         quarters = []\n",
    "\n",
    "#         for period in data[\"periods\"]:\n",
    "#             for event in period[\"events\"]:\n",
    "#                 descriptions.append(\n",
    "#                     event.get(\"description\", None)\n",
    "#                 )  # This is what happened\n",
    "#                 clocks.append(event.get(\"clock\", None))  # This is when it happened\n",
    "#                 event_types.append(\n",
    "#                     event.get(\"event_type\", None)\n",
    "#                 )  # This is the type of event\n",
    "#                 home_points.append(\n",
    "#                     event.get(\"home_points\", None)\n",
    "#                 )  # This is the home team's points\n",
    "#                 away_points.append(\n",
    "#                     event.get(\"away_points\", None)\n",
    "#                 )  # This is the visitor team's points\n",
    "#                 quarters.append(period.get(\"number\", None))  # This is the quarter\n",
    "\n",
    "#         # Create a DataFrame from the extracted information\n",
    "#         df = pd.DataFrame(\n",
    "#             {\n",
    "#                 \"date\": scheduled,  # This is when the game was scheduled\n",
    "#                 \"home_team_name\": home_team_name,\n",
    "#                 \"away_team_name\": away_team_name,\n",
    "#                 \"description\": descriptions,\n",
    "#                 \"clock\": clocks,\n",
    "#                 \"event_type\": event_types,\n",
    "#                 \"home_points\": home_points,\n",
    "#                 \"away_points\": away_points,\n",
    "#                 \"quarter\": quarters,\n",
    "#             }\n",
    "#         )\n",
    "\n",
    "#         # Return the DataFrame\n",
    "#         return df\n",
    "\n",
    "#     else:\n",
    "#         # If the request was not successful, return None\n",
    "#         return None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "json_files = []\n",
    "\n",
    "# Walk through current directory\n",
    "for dirpath, dirnames, filenames in os.walk('.'):\n",
    "    # Find JSON files in each directory\n",
    "    for filename in filenames:\n",
    "        if filename.endswith('.json'):\n",
    "            # Append the path to the file\n",
    "            json_files.append(os.path.join(dirpath, filename))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import json\n",
    "import pandas as pd\n",
    "\n",
    "def get_game_pbp_from_file(file_path):\n",
    "    # Load JSON data from the file\n",
    "    with open(file_path, 'r') as file:\n",
    "        data = json.load(file)\n",
    "\n",
    "    # Extract the relevant information\n",
    "    id = data[\"id\"] # This is the game's ID\n",
    "    scheduled = data[\"scheduled\"]  # This is when the game was scheduled\n",
    "    home_team_name = data[\"home\"][\"market\"] + \" \" + data[\"home\"][\"name\"]\n",
    "    away_team_name = data[\"away\"][\"market\"] + \" \" + data[\"away\"][\"name\"]\n",
    "    descriptions = []\n",
    "    clocks = []\n",
    "    event_types = []\n",
    "    home_points = []\n",
    "    away_points = []\n",
    "    quarters = []\n",
    "\n",
    "    if \"periods\" not in data:\n",
    "        print (f\"File {file_path} does not contain play-by-play data\")\n",
    "        return None\n",
    "    \n",
    "    else:\n",
    "\n",
    "        for period in data[\"periods\"]:\n",
    "            for event in period[\"events\"]:\n",
    "                descriptions.append(event.get(\"description\", None))  # This is what happened\n",
    "                clocks.append(event.get(\"clock\", None))  # This is when it happened\n",
    "                event_types.append(event.get(\"event_type\", None))  # This is the type of event\n",
    "                home_points.append(event.get(\"home_points\", None))  # This is the home team's points\n",
    "                away_points.append(event.get(\"away_points\", None))  # This is the visitor team's points\n",
    "                quarters.append(period.get(\"number\", None))  # This is the quarter\n",
    "\n",
    "    # Create a DataFrame from the extracted information\n",
    "    df = pd.DataFrame({\n",
    "        \"id\": id,  # This is the game's ID\n",
    "        \"date\": scheduled,  # This is when the game was scheduled\n",
    "        \"home_team_name\": home_team_name,\n",
    "        \"away_team_name\": away_team_name,\n",
    "        \"description\": descriptions,\n",
    "        \"clock\": clocks,\n",
    "        \"event_type\": event_types,\n",
    "        \"home_points\": home_points,\n",
    "        \"away_points\": away_points,\n",
    "        \"quarter\": quarters,\n",
    "    })\n",
    "\n",
    "    # Return the DataFrame\n",
    "    return df\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Just testing the function with one file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>date</th>\n",
       "      <th>home_team_name</th>\n",
       "      <th>away_team_name</th>\n",
       "      <th>description</th>\n",
       "      <th>clock</th>\n",
       "      <th>event_type</th>\n",
       "      <th>home_points</th>\n",
       "      <th>away_points</th>\n",
       "      <th>quarter</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>6decf9f6-cf39-4bf4-94b1-0acc349bfb69</td>\n",
       "      <td>2023-01-23T02:00:00+00:00</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>Los Angeles Lakers</td>\n",
       "      <td>Lakers lineup change (Thomas Bryant, LeBron Ja...</td>\n",
       "      <td>12:00</td>\n",
       "      <td>lineupchange</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>6decf9f6-cf39-4bf4-94b1-0acc349bfb69</td>\n",
       "      <td>2023-01-23T02:00:00+00:00</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>Los Angeles Lakers</td>\n",
       "      <td>Trail Blazers lineup change (Damian Lillard, J...</td>\n",
       "      <td>12:00</td>\n",
       "      <td>lineupchange</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6decf9f6-cf39-4bf4-94b1-0acc349bfb69</td>\n",
       "      <td>2023-01-23T02:00:00+00:00</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>Los Angeles Lakers</td>\n",
       "      <td>Thomas Bryant vs. Jusuf Nurkic (Dennis Schrode...</td>\n",
       "      <td>12:00</td>\n",
       "      <td>opentip</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>6decf9f6-cf39-4bf4-94b1-0acc349bfb69</td>\n",
       "      <td>2023-01-23T02:00:00+00:00</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>Los Angeles Lakers</td>\n",
       "      <td>LeBron James makes two point cutting dunk (Den...</td>\n",
       "      <td>11:47</td>\n",
       "      <td>twopointmade</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>6decf9f6-cf39-4bf4-94b1-0acc349bfb69</td>\n",
       "      <td>2023-01-23T02:00:00+00:00</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>Los Angeles Lakers</td>\n",
       "      <td>Damian Lillard makes two point jump shot (Josh...</td>\n",
       "      <td>11:31</td>\n",
       "      <td>twopointmade</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>458</th>\n",
       "      <td>6decf9f6-cf39-4bf4-94b1-0acc349bfb69</td>\n",
       "      <td>2023-01-23T02:00:00+00:00</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>Los Angeles Lakers</td>\n",
       "      <td>Dennis Schroder makes regular free throw 1 of 2</td>\n",
       "      <td>00:30</td>\n",
       "      <td>freethrowmade</td>\n",
       "      <td>112</td>\n",
       "      <td>120</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>459</th>\n",
       "      <td>6decf9f6-cf39-4bf4-94b1-0acc349bfb69</td>\n",
       "      <td>2023-01-23T02:00:00+00:00</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>Los Angeles Lakers</td>\n",
       "      <td>Dennis Schroder makes regular free throw 2 of 2</td>\n",
       "      <td>00:30</td>\n",
       "      <td>freethrowmade</td>\n",
       "      <td>112</td>\n",
       "      <td>121</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>460</th>\n",
       "      <td>6decf9f6-cf39-4bf4-94b1-0acc349bfb69</td>\n",
       "      <td>2023-01-23T02:00:00+00:00</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>Los Angeles Lakers</td>\n",
       "      <td>Damian Lillard misses three point pullup jump ...</td>\n",
       "      <td>00:24</td>\n",
       "      <td>threepointmiss</td>\n",
       "      <td>112</td>\n",
       "      <td>121</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>461</th>\n",
       "      <td>6decf9f6-cf39-4bf4-94b1-0acc349bfb69</td>\n",
       "      <td>2023-01-23T02:00:00+00:00</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>Los Angeles Lakers</td>\n",
       "      <td>LeBron James defensive rebound</td>\n",
       "      <td>00:21</td>\n",
       "      <td>rebound</td>\n",
       "      <td>112</td>\n",
       "      <td>121</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>462</th>\n",
       "      <td>6decf9f6-cf39-4bf4-94b1-0acc349bfb69</td>\n",
       "      <td>2023-01-23T02:00:00+00:00</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>Los Angeles Lakers</td>\n",
       "      <td>End of 4th Quarter.</td>\n",
       "      <td>00:00</td>\n",
       "      <td>endperiod</td>\n",
       "      <td>112</td>\n",
       "      <td>121</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>463 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                       id                       date  \\\n",
       "0    6decf9f6-cf39-4bf4-94b1-0acc349bfb69  2023-01-23T02:00:00+00:00   \n",
       "1    6decf9f6-cf39-4bf4-94b1-0acc349bfb69  2023-01-23T02:00:00+00:00   \n",
       "2    6decf9f6-cf39-4bf4-94b1-0acc349bfb69  2023-01-23T02:00:00+00:00   \n",
       "3    6decf9f6-cf39-4bf4-94b1-0acc349bfb69  2023-01-23T02:00:00+00:00   \n",
       "4    6decf9f6-cf39-4bf4-94b1-0acc349bfb69  2023-01-23T02:00:00+00:00   \n",
       "..                                    ...                        ...   \n",
       "458  6decf9f6-cf39-4bf4-94b1-0acc349bfb69  2023-01-23T02:00:00+00:00   \n",
       "459  6decf9f6-cf39-4bf4-94b1-0acc349bfb69  2023-01-23T02:00:00+00:00   \n",
       "460  6decf9f6-cf39-4bf4-94b1-0acc349bfb69  2023-01-23T02:00:00+00:00   \n",
       "461  6decf9f6-cf39-4bf4-94b1-0acc349bfb69  2023-01-23T02:00:00+00:00   \n",
       "462  6decf9f6-cf39-4bf4-94b1-0acc349bfb69  2023-01-23T02:00:00+00:00   \n",
       "\n",
       "             home_team_name      away_team_name  \\\n",
       "0    Portland Trail Blazers  Los Angeles Lakers   \n",
       "1    Portland Trail Blazers  Los Angeles Lakers   \n",
       "2    Portland Trail Blazers  Los Angeles Lakers   \n",
       "3    Portland Trail Blazers  Los Angeles Lakers   \n",
       "4    Portland Trail Blazers  Los Angeles Lakers   \n",
       "..                      ...                 ...   \n",
       "458  Portland Trail Blazers  Los Angeles Lakers   \n",
       "459  Portland Trail Blazers  Los Angeles Lakers   \n",
       "460  Portland Trail Blazers  Los Angeles Lakers   \n",
       "461  Portland Trail Blazers  Los Angeles Lakers   \n",
       "462  Portland Trail Blazers  Los Angeles Lakers   \n",
       "\n",
       "                                           description  clock      event_type  \\\n",
       "0    Lakers lineup change (Thomas Bryant, LeBron Ja...  12:00    lineupchange   \n",
       "1    Trail Blazers lineup change (Damian Lillard, J...  12:00    lineupchange   \n",
       "2    Thomas Bryant vs. Jusuf Nurkic (Dennis Schrode...  12:00         opentip   \n",
       "3    LeBron James makes two point cutting dunk (Den...  11:47    twopointmade   \n",
       "4    Damian Lillard makes two point jump shot (Josh...  11:31    twopointmade   \n",
       "..                                                 ...    ...             ...   \n",
       "458    Dennis Schroder makes regular free throw 1 of 2  00:30   freethrowmade   \n",
       "459    Dennis Schroder makes regular free throw 2 of 2  00:30   freethrowmade   \n",
       "460  Damian Lillard misses three point pullup jump ...  00:24  threepointmiss   \n",
       "461                     LeBron James defensive rebound  00:21         rebound   \n",
       "462                                End of 4th Quarter.  00:00       endperiod   \n",
       "\n",
       "     home_points  away_points  quarter  \n",
       "0              0            0        1  \n",
       "1              0            0        1  \n",
       "2              0            0        1  \n",
       "3              0            2        1  \n",
       "4              2            2        1  \n",
       "..           ...          ...      ...  \n",
       "458          112          120        4  \n",
       "459          112          121        4  \n",
       "460          112          121        4  \n",
       "461          112          121        4  \n",
       "462          112          121        4  \n",
       "\n",
       "[463 rows x 10 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "file = json_files[0]\n",
    "\n",
    "# Get the play by play data from the file\n",
    "play_by_play_df = get_game_pbp_from_file(file)\n",
    "\n",
    "play_by_play_df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Doing it on all files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File ./json_files/play_by_play_sportsradar_69fd5274-20fe-494a-89c2-14988b791d31.json does not contain play-by-play data\n"
     ]
    }
   ],
   "source": [
    "all_dfs = []\n",
    "\n",
    "# Loop through each JSON file\n",
    "for file_name in json_files:\n",
    "    # Get the DataFrame for the current file\n",
    "    play_by_play_df = get_game_pbp_from_file(file_name)\n",
    "    # Append the DataFrame to the list\n",
    "    all_dfs.append(play_by_play_df)\n",
    "\n",
    "# Concatenate all DataFrames into a single DataFrame\n",
    "final_df = pd.concat(all_dfs, ignore_index=True)\n",
    "\n",
    "# # Print the final DataFrame\n",
    "# print(final_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "193"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "final_df\n",
    "\n",
    "#check for unique dates\n",
    "\n",
    "len(final_df[\"id\"].unique())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>date</th>\n",
       "      <th>home_team_name</th>\n",
       "      <th>away_team_name</th>\n",
       "      <th>description</th>\n",
       "      <th>clock</th>\n",
       "      <th>event_type</th>\n",
       "      <th>home_points</th>\n",
       "      <th>away_points</th>\n",
       "      <th>quarter</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>6decf9f6-cf39-4bf4-94b1-0acc349bfb69</td>\n",
       "      <td>2023-01-23T02:00:00+00:00</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>Los Angeles Lakers</td>\n",
       "      <td>Lakers lineup change (Thomas Bryant, LeBron Ja...</td>\n",
       "      <td>12:00</td>\n",
       "      <td>lineupchange</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>6decf9f6-cf39-4bf4-94b1-0acc349bfb69</td>\n",
       "      <td>2023-01-23T02:00:00+00:00</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>Los Angeles Lakers</td>\n",
       "      <td>Trail Blazers lineup change (Damian Lillard, J...</td>\n",
       "      <td>12:00</td>\n",
       "      <td>lineupchange</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6decf9f6-cf39-4bf4-94b1-0acc349bfb69</td>\n",
       "      <td>2023-01-23T02:00:00+00:00</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>Los Angeles Lakers</td>\n",
       "      <td>Thomas Bryant vs. Jusuf Nurkic (Dennis Schrode...</td>\n",
       "      <td>12:00</td>\n",
       "      <td>opentip</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>6decf9f6-cf39-4bf4-94b1-0acc349bfb69</td>\n",
       "      <td>2023-01-23T02:00:00+00:00</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>Los Angeles Lakers</td>\n",
       "      <td>LeBron James makes two point cutting dunk (Den...</td>\n",
       "      <td>11:47</td>\n",
       "      <td>twopointmade</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>6decf9f6-cf39-4bf4-94b1-0acc349bfb69</td>\n",
       "      <td>2023-01-23T02:00:00+00:00</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>Los Angeles Lakers</td>\n",
       "      <td>Damian Lillard makes two point jump shot (Josh...</td>\n",
       "      <td>11:31</td>\n",
       "      <td>twopointmade</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>88964</th>\n",
       "      <td>81509f8f-9014-460b-b383-70b65a0efdaf</td>\n",
       "      <td>2023-03-20T01:00:00+00:00</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>LA Clippers</td>\n",
       "      <td>Paul George defensive rebound</td>\n",
       "      <td>00:47</td>\n",
       "      <td>rebound</td>\n",
       "      <td>102</td>\n",
       "      <td>117</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>88965</th>\n",
       "      <td>81509f8f-9014-460b-b383-70b65a0efdaf</td>\n",
       "      <td>2023-03-20T01:00:00+00:00</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>LA Clippers</td>\n",
       "      <td>Clippers turnover (shot clock violation)</td>\n",
       "      <td>00:24</td>\n",
       "      <td>turnover</td>\n",
       "      <td>102</td>\n",
       "      <td>117</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>88966</th>\n",
       "      <td>81509f8f-9014-460b-b383-70b65a0efdaf</td>\n",
       "      <td>2023-03-20T01:00:00+00:00</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>LA Clippers</td>\n",
       "      <td>Damian Lillard misses three point pullup jump ...</td>\n",
       "      <td>00:19</td>\n",
       "      <td>threepointmiss</td>\n",
       "      <td>102</td>\n",
       "      <td>117</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>88967</th>\n",
       "      <td>81509f8f-9014-460b-b383-70b65a0efdaf</td>\n",
       "      <td>2023-03-20T01:00:00+00:00</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>LA Clippers</td>\n",
       "      <td>Ivica Zubac defensive rebound</td>\n",
       "      <td>00:16</td>\n",
       "      <td>rebound</td>\n",
       "      <td>102</td>\n",
       "      <td>117</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>88968</th>\n",
       "      <td>81509f8f-9014-460b-b383-70b65a0efdaf</td>\n",
       "      <td>2023-03-20T01:00:00+00:00</td>\n",
       "      <td>Portland Trail Blazers</td>\n",
       "      <td>LA Clippers</td>\n",
       "      <td>End of 4th Quarter.</td>\n",
       "      <td>00:00</td>\n",
       "      <td>endperiod</td>\n",
       "      <td>102</td>\n",
       "      <td>117</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>88969 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                         id                       date  \\\n",
       "0      6decf9f6-cf39-4bf4-94b1-0acc349bfb69  2023-01-23T02:00:00+00:00   \n",
       "1      6decf9f6-cf39-4bf4-94b1-0acc349bfb69  2023-01-23T02:00:00+00:00   \n",
       "2      6decf9f6-cf39-4bf4-94b1-0acc349bfb69  2023-01-23T02:00:00+00:00   \n",
       "3      6decf9f6-cf39-4bf4-94b1-0acc349bfb69  2023-01-23T02:00:00+00:00   \n",
       "4      6decf9f6-cf39-4bf4-94b1-0acc349bfb69  2023-01-23T02:00:00+00:00   \n",
       "...                                     ...                        ...   \n",
       "88964  81509f8f-9014-460b-b383-70b65a0efdaf  2023-03-20T01:00:00+00:00   \n",
       "88965  81509f8f-9014-460b-b383-70b65a0efdaf  2023-03-20T01:00:00+00:00   \n",
       "88966  81509f8f-9014-460b-b383-70b65a0efdaf  2023-03-20T01:00:00+00:00   \n",
       "88967  81509f8f-9014-460b-b383-70b65a0efdaf  2023-03-20T01:00:00+00:00   \n",
       "88968  81509f8f-9014-460b-b383-70b65a0efdaf  2023-03-20T01:00:00+00:00   \n",
       "\n",
       "               home_team_name      away_team_name  \\\n",
       "0      Portland Trail Blazers  Los Angeles Lakers   \n",
       "1      Portland Trail Blazers  Los Angeles Lakers   \n",
       "2      Portland Trail Blazers  Los Angeles Lakers   \n",
       "3      Portland Trail Blazers  Los Angeles Lakers   \n",
       "4      Portland Trail Blazers  Los Angeles Lakers   \n",
       "...                       ...                 ...   \n",
       "88964  Portland Trail Blazers         LA Clippers   \n",
       "88965  Portland Trail Blazers         LA Clippers   \n",
       "88966  Portland Trail Blazers         LA Clippers   \n",
       "88967  Portland Trail Blazers         LA Clippers   \n",
       "88968  Portland Trail Blazers         LA Clippers   \n",
       "\n",
       "                                             description  clock  \\\n",
       "0      Lakers lineup change (Thomas Bryant, LeBron Ja...  12:00   \n",
       "1      Trail Blazers lineup change (Damian Lillard, J...  12:00   \n",
       "2      Thomas Bryant vs. Jusuf Nurkic (Dennis Schrode...  12:00   \n",
       "3      LeBron James makes two point cutting dunk (Den...  11:47   \n",
       "4      Damian Lillard makes two point jump shot (Josh...  11:31   \n",
       "...                                                  ...    ...   \n",
       "88964                      Paul George defensive rebound  00:47   \n",
       "88965           Clippers turnover (shot clock violation)  00:24   \n",
       "88966  Damian Lillard misses three point pullup jump ...  00:19   \n",
       "88967                      Ivica Zubac defensive rebound  00:16   \n",
       "88968                                End of 4th Quarter.  00:00   \n",
       "\n",
       "           event_type  home_points  away_points  quarter  \n",
       "0        lineupchange            0            0        1  \n",
       "1        lineupchange            0            0        1  \n",
       "2             opentip            0            0        1  \n",
       "3        twopointmade            0            2        1  \n",
       "4        twopointmade            2            2        1  \n",
       "...               ...          ...          ...      ...  \n",
       "88964         rebound          102          117        4  \n",
       "88965        turnover          102          117        4  \n",
       "88966  threepointmiss          102          117        4  \n",
       "88967         rebound          102          117        4  \n",
       "88968       endperiod          102          117        4  \n",
       "\n",
       "[88969 rows x 10 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "final_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "#save it to parquet\n",
    "\n",
    "final_df.to_parquet(\"combined_dataframe.parquet\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Separate parquet files for each game\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File ./json_files/play_by_play_sportsradar_69fd5274-20fe-494a-89c2-14988b791d31.json does not contain play-by-play data\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "# Loop through each JSON file\n",
    "for file_name in json_files:\n",
    "    # Get the DataFrame for the current file\n",
    "    play_by_play_df = get_game_pbp_from_file(file_name)\n",
    "    \n",
    "    # Extract home and away team names from the DataFrame\n",
    "    if play_by_play_df is None:\n",
    "        continue\n",
    "\n",
    "\n",
    "    home_team = play_by_play_df['home_team_name'].iloc[0]\n",
    "    away_team = play_by_play_df['away_team_name'].iloc[0]\n",
    "    \n",
    "    # Create the file name\n",
    "    parquet_file_name = f\"{home_team}VS{away_team}.parquet\"\n",
    "    \n",
    "    # Save the DataFrame as a Parquet file\n",
    "    play_by_play_df.to_parquet(os.path.join('Data/', parquet_file_name))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}