nbastatpy 0.1.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of nbastatpy might be problematic. Click here for more details.

nbastatpy/season.py ADDED
@@ -0,0 +1,578 @@
1
+ from time import sleep
2
+
3
+ import nba_api.stats.endpoints as nba
4
+ import pandas as pd
5
+ import requests
6
+ from bs4 import BeautifulSoup
7
+ from rich.progress import track
8
+
9
+ from nbastatpy.utils import Formatter, PlayTypes
10
+
11
+
12
+ class Season:
13
+ def __init__(
14
+ self, season_year: str = None, playoffs=False, permode: str = "PERGAME"
15
+ ):
16
+ """
17
+ Initialize a Season object.
18
+
19
+ Args:
20
+ season_year (str, optional): The year of the season. Defaults to None.
21
+ playoffs (bool, optional): Indicates if the season is for playoffs. Defaults to False.
22
+ permode (str, optional): The per mode for the season. Defaults to "PERGAME".
23
+ """
24
+ self.permode = PlayTypes.PERMODE[
25
+ permode.replace("_", "").replace("-", "").upper()
26
+ ]
27
+ if season_year:
28
+ self.season_year = season_year
29
+ else:
30
+ self.season_year = Formatter.get_current_season_year()
31
+
32
+ self.season = Formatter.format_season(self.season_year)
33
+ self.season_type = "Regular Season"
34
+ if playoffs:
35
+ self.season_type = "Playoffs"
36
+
37
+ def get_salaries(self) -> pd.DataFrame:
38
+ """
39
+ Retrieves the salaries of NBA players for a specific season.
40
+
41
+ Returns:
42
+ pd.DataFrame: A DataFrame containing the salaries of NBA players for the specified season.
43
+ """
44
+ year = self.season.split("-")[0]
45
+ season_string = year + "-" + str(int(year) + 1)
46
+
47
+ url = f"https://hoopshype.com/salaries/players/{season_string}/"
48
+ result = requests.get(url)
49
+ soup = BeautifulSoup(result.content, features="html.parser")
50
+ tables = soup.find_all("table")[0]
51
+
52
+ # # Get the table rows
53
+ rows = [[cell.text.strip() for cell in row.find_all('td')] for row in tables.find_all('tr')]
54
+
55
+ self.salary_df = pd.DataFrame(rows[1:], columns=rows[0])
56
+ if '' in self.salary_df.columns:
57
+ self.salary_df = self.salary_df.drop(columns=[""])
58
+
59
+ self.salary_df['Season'] = self.salary_df.columns[1].replace("/", "_")
60
+ self.salary_df.columns = ["Player", "Salary", "Adj_Salary", "Season"]
61
+
62
+ return self.salary_df
63
+
64
+ def get_lineups(self):
65
+ """
66
+ Retrieves the lineups data for the specified season, season type, and per mode.
67
+
68
+ Returns:
69
+ pandas.DataFrame: The lineups data for the specified season, season type, and per mode.
70
+ """
71
+ self.lineups = nba.LeagueDashLineups(
72
+ season=self.season,
73
+ season_type_all_star=self.season_type,
74
+ per_mode_detailed=self.permode,
75
+ ).get_data_frames()[0]
76
+ return self.lineups
77
+
78
+ def get_lineup_details(self):
79
+ """
80
+ Retrieves the lineup details for the specified season.
81
+
82
+ Returns:
83
+ pandas.DataFrame: The lineup details for the specified season.
84
+ """
85
+ self.lineup_details = nba.LeagueLineupViz(
86
+ season=self.season,
87
+ season_type_all_star=self.season_type,
88
+ minutes_min=1,
89
+ per_mode_detailed=self.permode,
90
+ ).get_data_frames()[0]
91
+ return self.lineup_details
92
+
93
+ def get_opponent_shooting(self):
94
+ """
95
+ Retrieves the opponent shooting statistics for the specified season.
96
+
97
+ Returns:
98
+ pandas.DataFrame: The opponent shooting statistics for the season.
99
+ """
100
+ self.opponent_shooting = nba.LeagueDashOppPtShot(
101
+ season=self.season,
102
+ season_type_all_star=self.season_type,
103
+ per_mode_simple=self.permode,
104
+ ).get_data_frames()[0]
105
+ return self.opponent_shooting
106
+
107
+ def get_player_clutch(self):
108
+ """
109
+ Retrieves the player clutch data for the specified season.
110
+
111
+ Returns:
112
+ pandas.DataFrame: The player clutch data for the specified season.
113
+ """
114
+ self.player_clutch = nba.LeagueDashPlayerClutch(
115
+ season=self.season,
116
+ season_type_all_star=self.season_type,
117
+ per_mode_detailed=self.permode,
118
+ ).get_data_frames()[0]
119
+ return self.player_clutch
120
+
121
+ def get_player_shots(self):
122
+ """
123
+ Retrieves the player shots data for the specified season, season type, and per mode.
124
+
125
+ Returns:
126
+ pandas.DataFrame: The player shots data.
127
+ """
128
+ self.player_shots = nba.LeagueDashPlayerPtShot(
129
+ season=self.season,
130
+ season_type_all_star=self.season_type,
131
+ per_mode_simple=self.permode,
132
+ ).get_data_frames()[0]
133
+ return self.player_shots
134
+
135
+ def get_player_shot_locations(self):
136
+ """
137
+ Retrieves the shot locations data for the players in the specified season.
138
+
139
+ Returns:
140
+ pandas.DataFrame: A DataFrame containing the shot locations data for the players.
141
+ """
142
+ self.player_shot_locations = nba.LeagueDashPlayerShotLocations(
143
+ season=self.season,
144
+ season_type_all_star=self.season_type,
145
+ per_mode_detailed=self.permode,
146
+ ).get_data_frames()[0]
147
+ return self.player_shot_locations
148
+
149
+ def get_player_stats(self):
150
+ """
151
+ Retrieves the player statistics for the specified season.
152
+
153
+ Returns:
154
+ pandas.DataFrame: A DataFrame containing the player statistics.
155
+ """
156
+ self.player_stats = nba.LeagueDashPlayerStats(
157
+ season=self.season,
158
+ season_type_all_star=self.season_type,
159
+ per_mode_detailed=self.permode,
160
+ ).get_data_frames()[0]
161
+ return self.player_stats
162
+
163
+ def get_team_clutch(self):
164
+ """
165
+ Retrieves the clutch statistics for teams in the specified season.
166
+
167
+ Returns:
168
+ pandas.DataFrame: A DataFrame containing the clutch statistics for teams.
169
+ """
170
+ self.team_clutch = nba.LeagueDashTeamClutch(
171
+ season=self.season,
172
+ season_type_all_star=self.season_type,
173
+ per_mode_detailed=self.permode,
174
+ ).get_data_frames()[0]
175
+ return self.team_clutch
176
+
177
+ def get_team_shots_bypoint(self):
178
+ """
179
+ Retrieves the team shots by point data for the specified season.
180
+
181
+ Returns:
182
+ pandas.DataFrame: The team shots by point data.
183
+ """
184
+ self.team_shots_bypoint = nba.LeagueDashTeamPtShot(
185
+ season=self.season,
186
+ season_type_all_star=self.season_type,
187
+ per_mode_simple=self.permode,
188
+ ).get_data_frames()[0]
189
+ return self.team_shots_bypoint
190
+
191
+ def get_team_shot_locations(self):
192
+ """
193
+ Retrieves the shot locations data for teams in a specific season.
194
+
195
+ Returns:
196
+ pandas.DataFrame: The shot locations data for teams.
197
+ """
198
+ self.team_shot_locations = nba.LeagueDashTeamShotLocations(
199
+ season=self.season,
200
+ season_type_all_star=self.season_type,
201
+ per_mode_detailed=self.permode,
202
+ ).get_data_frames()[0]
203
+ return self.team_shot_locations
204
+
205
+ def get_team_stats(self):
206
+ """
207
+ Retrieves the team statistics for the specified season.
208
+
209
+ Returns:
210
+ pandas.DataFrame: A DataFrame containing the team statistics.
211
+ """
212
+ self.team_stats = nba.LeagueDashTeamStats(
213
+ season=self.season,
214
+ season_type_all_star=self.season_type,
215
+ per_mode_detailed=self.permode,
216
+ ).get_data_frames()[0]
217
+ return self.team_stats
218
+
219
+ def get_player_games(self) -> pd.DataFrame:
220
+ """
221
+ Retrieves the player games data for the specified season, season type, and per mode.
222
+
223
+ Returns:
224
+ pd.DataFrame: A DataFrame containing the player games data.
225
+ """
226
+ self.player_games = nba.PlayerGameLogs(
227
+ season_nullable=self.season,
228
+ season_type_nullable=self.season_type,
229
+ per_mode_simple_nullable=self.permode,
230
+ ).get_data_frames()[0]
231
+ return self.player_games
232
+
233
+ def get_team_games(self):
234
+ """
235
+ Retrieves the game log for a specific team in a given season.
236
+
237
+ Returns:
238
+ pandas.DataFrame: The game log data for the team.
239
+ """
240
+ self.team_games = nba.LeagueGameLog(
241
+ season=self.season,
242
+ season_type_all_star=self.season_type,
243
+ player_or_team_abbreviation="T",
244
+ ).get_data_frames()[0]
245
+ return self.team_games
246
+
247
+ def get_player_hustle(self):
248
+ """
249
+ Retrieves the hustle stats for players in the specified season and season type.
250
+
251
+ Returns:
252
+ pandas.DataFrame: A DataFrame containing the player hustle stats.
253
+ """
254
+ self.player_hustle = nba.LeagueHustleStatsPlayer(
255
+ season=self.season,
256
+ season_type_all_star=self.season_type,
257
+ ).get_data_frames()[0]
258
+ return self.player_hustle
259
+
260
+ def get_team_hustle(self):
261
+ """
262
+ Retrieves the team hustle stats for the specified season and season type.
263
+
264
+ Returns:
265
+ pandas.DataFrame: The team hustle stats for the specified season and season type.
266
+ """
267
+ self.team_hustle = nba.LeagueHustleStatsTeam(
268
+ season=self.season,
269
+ season_type_all_star=self.season_type,
270
+ ).get_data_frames()[0]
271
+ return self.team_hustle
272
+
273
+ def get_player_matchups(self):
274
+ """
275
+ Retrieves the player matchups for the current season.
276
+
277
+ Returns:
278
+ pandas.DataFrame: The player matchups data for the current season.
279
+ """
280
+ self.player_matchups = nba.LeagueSeasonMatchups(
281
+ season=self.season,
282
+ season_type_playoffs=self.season_type,
283
+ per_mode_simple=self.permode,
284
+ ).get_data_frames()[0]
285
+ return self.player_matchups
286
+
287
+ def get_player_estimated_metrics(self):
288
+ """
289
+ Retrieves the estimated metrics for players in the specified season and season type.
290
+
291
+ Returns:
292
+ pandas.DataFrame: A DataFrame containing the estimated metrics for players.
293
+ """
294
+ self.player_estimated_metrics = nba.PlayerEstimatedMetrics(
295
+ season=self.season,
296
+ season_type=self.season_type,
297
+ ).get_data_frames()[0]
298
+ return self.player_estimated_metrics
299
+
300
+ def get_synergy_player(
301
+ self, play_type: str = "Transition", offensive: bool = True
302
+ ) -> pd.DataFrame:
303
+ """
304
+ Retrieves synergy data for a specific play type and offensive/defensive category.
305
+
306
+ Args:
307
+ play_type (str, optional): The play type to retrieve synergy data for. Defaults to "Transition".
308
+ offensive (bool, optional): Specifies whether to retrieve offensive or defensive synergy data. Defaults to True.
309
+
310
+ Returns:
311
+ pd.DataFrame: The synergy data as a pandas DataFrame.
312
+ """
313
+ self.play_type = Formatter.check_playtype(
314
+ play_type, playtypes=PlayTypes.PLAYTYPES
315
+ )
316
+ if offensive:
317
+ self.off_def = "offensive"
318
+ else:
319
+ self.off_def = "defensive"
320
+
321
+ if isinstance(self.play_type, str):
322
+ self.synergy = nba.SynergyPlayTypes(
323
+ season=self.season,
324
+ per_mode_simple=self.permode,
325
+ play_type_nullable=self.play_type,
326
+ type_grouping_nullable=self.off_def,
327
+ player_or_team_abbreviation="P",
328
+ season_type_all_star=self.season_type,
329
+ ).get_data_frames()[0]
330
+
331
+ else:
332
+ df_list = []
333
+ for play in track(self.play_type):
334
+
335
+ temp_df = nba.SynergyPlayTypes(
336
+ season=self.season,
337
+ per_mode_simple=self.permode,
338
+ play_type_nullable=play,
339
+ type_grouping_nullable=self.off_def,
340
+ player_or_team_abbreviation="P",
341
+ season_type_all_star=self.season_type,
342
+ ).get_data_frames()[0]
343
+ df_list.append(temp_df)
344
+ sleep(1)
345
+
346
+ self.synergy = pd.concat(df_list)
347
+
348
+ return self.synergy
349
+
350
+ def get_synergy_team(
351
+ self, play_type: str = "Transition", offensive: bool = True
352
+ ) -> pd.DataFrame:
353
+ """
354
+ Retrieves synergy data for a specific play type and team.
355
+
356
+ Args:
357
+ play_type (str, optional): The play type to retrieve synergy data for. Defaults to "Transition".
358
+ offensive (bool, optional): Determines whether to retrieve offensive or defensive synergy data. Defaults to True.
359
+
360
+ Returns:
361
+ pd.DataFrame: A DataFrame containing the synergy data.
362
+
363
+ Raises:
364
+ ValueError: If the play type is not valid.
365
+
366
+ """
367
+ self.play_type = Formatter.check_playtype(
368
+ play_type, playtypes=PlayTypes.PLAYTYPES
369
+ )
370
+ if offensive:
371
+ self.off_def = "offensive"
372
+ else:
373
+ self.off_def = "defensive"
374
+
375
+ if isinstance(self.play_type, str):
376
+ self.synergy = nba.SynergyPlayTypes(
377
+ season=self.season,
378
+ per_mode_simple=self.permode,
379
+ play_type_nullable=self.play_type,
380
+ type_grouping_nullable=self.off_def,
381
+ player_or_team_abbreviation="T",
382
+ season_type_all_star=self.season_type,
383
+ ).get_data_frames()[0]
384
+
385
+ else:
386
+ df_list = []
387
+ for play in track(self.play_type):
388
+ temp_df = nba.SynergyPlayTypes(
389
+ season=self.season,
390
+ per_mode_simple=self.permode,
391
+ play_type_nullable=play,
392
+ type_grouping_nullable=self.off_def,
393
+ player_or_team_abbreviation="T",
394
+ season_type_all_star=self.season_type,
395
+ ).get_data_frames()[0]
396
+ df_list.append(temp_df)
397
+ sleep(1)
398
+
399
+ self.synergy = pd.concat(df_list)
400
+
401
+ return self.synergy
402
+
403
+ def get_tracking_player(
404
+ self,
405
+ track_type: str = "Efficiency",
406
+ ) -> pd.DataFrame:
407
+ """
408
+ Retrieves tracking data for players based on the specified track type.
409
+
410
+ Parameters:
411
+ track_type (str): The type of tracking data to retrieve. Defaults to "Efficiency".
412
+
413
+ Returns:
414
+ pd.DataFrame: A DataFrame containing the tracking data for players.
415
+ """
416
+ self.play_type = Formatter.check_playtype(
417
+ track_type, playtypes=PlayTypes.TRACKING_TYPES
418
+ )
419
+
420
+ if isinstance(self.play_type, str):
421
+ self.tracking = nba.LeagueDashPtStats(
422
+ season=self.season,
423
+ per_mode_simple=self.permode,
424
+ pt_measure_type=self.play_type,
425
+ player_or_team="Player",
426
+ season_type_all_star=self.season_type,
427
+ ).get_data_frames()[0]
428
+
429
+ else:
430
+ df_list = []
431
+ for play in track(self.play_type):
432
+
433
+ temp_df = nba.LeagueDashPtStats(
434
+ season=self.season,
435
+ per_mode_simple=self.permode,
436
+ pt_measure_type=play,
437
+ player_or_team="Player",
438
+ season_type_all_star=self.season_type,
439
+ ).get_data_frames()[0]
440
+ df_list.append(temp_df)
441
+ sleep(1)
442
+
443
+ self.tracking = pd.concat(df_list)
444
+
445
+ return self.tracking
446
+
447
+ def get_tracking_team(
448
+ self,
449
+ track_type: str = "Efficiency",
450
+ ) -> pd.DataFrame:
451
+ """
452
+ Retrieves tracking data for a specific play type and returns it as a pandas DataFrame.
453
+
454
+ Parameters:
455
+ track_type (str): The play type to track. Defaults to "Efficiency".
456
+
457
+ Returns:
458
+ pd.DataFrame: The tracking data as a pandas DataFrame.
459
+ """
460
+ self.play_type = Formatter.check_playtype(track_type, PlayTypes.TRACKING_TYPES)
461
+
462
+ if isinstance(self.play_type, str):
463
+ self.tracking = nba.LeagueDashPtStats(
464
+ season=self.season,
465
+ per_mode_simple=self.permode,
466
+ pt_measure_type=self.play_type,
467
+ player_or_team="Team",
468
+ season_type_all_star=self.season_type,
469
+ ).get_data_frames()[0]
470
+
471
+ else:
472
+ df_list = []
473
+ for play in track(self.play_type):
474
+
475
+ temp_df = nba.LeagueDashPtStats(
476
+ season=self.season,
477
+ per_mode_simple=self.permode,
478
+ pt_measure_type=play,
479
+ player_or_team="Team",
480
+ season_type_all_star=self.season_type,
481
+ ).get_data_frames()[0]
482
+ df_list.append(temp_df)
483
+ sleep(1)
484
+
485
+ self.tracking = pd.concat(df_list)
486
+
487
+ return self.tracking
488
+
489
+ def get_defense_player(
490
+ self,
491
+ defense_type: str = "Overall",
492
+ ) -> pd.DataFrame:
493
+ """
494
+ Retrieves the defensive player data based on the specified defense type.
495
+
496
+ Args:
497
+ defense_type (str, optional): The type of defense to retrieve. Defaults to "Overall".
498
+
499
+ Returns:
500
+ pd.DataFrame: The defensive player data.
501
+
502
+ """
503
+ self.play_type = Formatter.check_playtype(
504
+ defense_type, playtypes=PlayTypes.DEFENSE_TYPES
505
+ )
506
+
507
+ if isinstance(self.play_type, str):
508
+ self.defense = nba.LeagueDashPtDefend(
509
+ season=self.season,
510
+ per_mode_simple=self.permode,
511
+ defense_category=self.play_type,
512
+ season_type_all_star=self.season_type,
513
+ ).get_data_frames()[0]
514
+
515
+ else:
516
+ df_list = []
517
+ for play in track(self.play_type):
518
+
519
+ temp_df = nba.LeagueDashPtDefend(
520
+ season=self.season,
521
+ per_mode_simple=self.permode,
522
+ defense_category=play,
523
+ season_type_all_star=self.season_type,
524
+ ).get_data_frames()[0]
525
+ df_list.append(temp_df)
526
+ sleep(1)
527
+
528
+ self.defense = pd.concat(df_list)
529
+
530
+ return self.defense
531
+
532
+ def get_defense_team(
533
+ self,
534
+ defense_type: str = "Overall",
535
+ ) -> pd.DataFrame:
536
+ """
537
+ Retrieves the defensive statistics for teams based on the specified defense type.
538
+
539
+ Args:
540
+ defense_type (str, optional): The type of defense to retrieve statistics for. Defaults to "Overall".
541
+
542
+ Returns:
543
+ pd.DataFrame: A DataFrame containing the defensive statistics for teams.
544
+ """
545
+ self.play_type = Formatter.check_playtype(
546
+ defense_type, playtypes=PlayTypes.DEFENSE_TYPES
547
+ )
548
+
549
+ if isinstance(self.play_type, str):
550
+ self.defense = nba.LeagueDashPtTeamDefend(
551
+ season=self.season,
552
+ per_mode_simple=self.permode,
553
+ defense_category=self.play_type,
554
+ season_type_all_star=self.season_type,
555
+ ).get_data_frames()[0]
556
+
557
+ else:
558
+ df_list = []
559
+ for play in track(self.play_type):
560
+
561
+ temp_df = nba.LeagueDashPtTeamDefend(
562
+ season=self.season,
563
+ per_mode_simple=self.permode,
564
+ defense_category=play,
565
+ season_type_all_star=self.season_type,
566
+ ).get_data_frames()[0]
567
+ df_list.append(temp_df)
568
+ sleep(1)
569
+
570
+ self.defense = pd.concat(df_list)
571
+
572
+ return self.defense
573
+
574
+
575
+ if __name__ == "__main__":
576
+ seas = Season(season_year="2004")
577
+ print(seas.permode)
578
+ print(seas.get_salaries())