nbastatpy 0.1.6__py3-none-any.whl → 0.2.0__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 CHANGED
@@ -6,26 +6,29 @@ import requests
6
6
  from bs4 import BeautifulSoup
7
7
  from rich.progress import track
8
8
 
9
+ from nbastatpy.standardize import standardize_dataframe
9
10
  from nbastatpy.utils import Formatter, PlayTypes
10
11
 
11
12
 
12
13
  class Season:
13
- def __init__(
14
- self, season_year: str = None, playoffs=False, permode: str = "PERGAME"
15
- ):
14
+ def __init__(self, season_year=None, playoffs=False, permode: str = "PERGAME"):
16
15
  """
17
16
  Initialize a Season object.
18
17
 
19
18
  Args:
20
- season_year (str, optional): The year of the season. Defaults to None.
19
+ season_year (int | str, optional): The year of the season. Can be provided in various formats:
20
+ - 4-digit year: 2022, "2022" -> 2022-23 season
21
+ - 2-digit year: 22, "22" -> 2022-23 season
22
+ - Full season string: "2022-23" -> 2022-23 season
23
+ Defaults to current season if None.
21
24
  playoffs (bool, optional): Indicates if the season is for playoffs. Defaults to False.
22
25
  permode (str, optional): The per mode for the season. Defaults to "PERGAME".
23
26
  """
24
27
  self.permode = PlayTypes.PERMODE[
25
28
  permode.replace("_", "").replace("-", "").upper()
26
29
  ]
27
- if season_year:
28
- self.season_year = season_year
30
+ if season_year is not None:
31
+ self.season_year = Formatter.normalize_season_year(season_year)
29
32
  else:
30
33
  self.season_year = Formatter.get_current_season_year()
31
34
 
@@ -48,253 +51,477 @@ class Season:
48
51
  result = requests.get(url)
49
52
  soup = BeautifulSoup(result.content, features="html.parser")
50
53
  tables = soup.find_all("table")[0]
51
-
54
+
52
55
  # # Get the table rows
53
- rows = [[cell.text.strip() for cell in row.find_all('td')] for row in tables.find_all('tr')]
56
+ rows = [
57
+ [cell.text.strip() for cell in row.find_all("td")]
58
+ for row in tables.find_all("tr")
59
+ ]
54
60
 
55
61
  self.salary_df = pd.DataFrame(rows[1:], columns=rows[0])
56
- if '' in self.salary_df.columns:
62
+ if "" in self.salary_df.columns:
57
63
  self.salary_df = self.salary_df.drop(columns=[""])
58
-
59
- self.salary_df['Season'] = self.salary_df.columns[1].replace("/", "_")
64
+
65
+ self.salary_df["Season"] = self.salary_df.columns[1].replace("/", "_")
60
66
  self.salary_df.columns = ["Player", "Salary", "Adj_Salary", "Season"]
61
67
 
62
68
  return self.salary_df
63
69
 
64
- def get_lineups(self):
70
+ def get_lineups(self, standardize: bool = False):
65
71
  """
66
72
  Retrieves the lineups data for the specified season, season type, and per mode.
67
73
 
74
+ Args:
75
+ standardize: Whether to apply data standardization
76
+
68
77
  Returns:
69
78
  pandas.DataFrame: The lineups data for the specified season, season type, and per mode.
70
79
  """
71
- self.lineups = nba.LeagueDashLineups(
80
+ df = nba.LeagueDashLineups(
72
81
  season=self.season,
73
82
  season_type_all_star=self.season_type,
74
83
  per_mode_detailed=self.permode,
75
84
  ).get_data_frames()[0]
85
+
86
+ if standardize:
87
+ df = standardize_dataframe(
88
+ df,
89
+ data_type="season",
90
+ season=self.season,
91
+ playoffs=(self.season_type == "Playoffs"),
92
+ )
93
+
94
+ self.lineups = df
76
95
  return self.lineups
77
96
 
78
- def get_lineup_details(self):
97
+ def get_lineup_details(self, standardize: bool = False):
79
98
  """
80
99
  Retrieves the lineup details for the specified season.
81
100
 
101
+ Args:
102
+ standardize: Whether to apply data standardization
103
+
82
104
  Returns:
83
105
  pandas.DataFrame: The lineup details for the specified season.
84
106
  """
85
- self.lineup_details = nba.LeagueLineupViz(
107
+ df = nba.LeagueLineupViz(
86
108
  season=self.season,
87
109
  season_type_all_star=self.season_type,
88
110
  minutes_min=1,
89
111
  per_mode_detailed=self.permode,
90
112
  ).get_data_frames()[0]
113
+
114
+ if standardize:
115
+ df = standardize_dataframe(
116
+ df,
117
+ data_type="season",
118
+ season=self.season,
119
+ playoffs=(self.season_type == "Playoffs"),
120
+ )
121
+
122
+ self.lineup_details = df
91
123
  return self.lineup_details
92
124
 
93
- def get_opponent_shooting(self):
125
+ def get_opponent_shooting(self, standardize: bool = False):
94
126
  """
95
127
  Retrieves the opponent shooting statistics for the specified season.
96
128
 
129
+ Args:
130
+ standardize: Whether to apply data standardization
131
+
97
132
  Returns:
98
133
  pandas.DataFrame: The opponent shooting statistics for the season.
99
134
  """
100
- self.opponent_shooting = nba.LeagueDashOppPtShot(
135
+ df = nba.LeagueDashOppPtShot(
101
136
  season=self.season,
102
137
  season_type_all_star=self.season_type,
103
138
  per_mode_simple=self.permode,
104
139
  ).get_data_frames()[0]
140
+
141
+ if standardize:
142
+ df = standardize_dataframe(
143
+ df,
144
+ data_type="season",
145
+ season=self.season,
146
+ playoffs=(self.season_type == "Playoffs"),
147
+ )
148
+
149
+ self.opponent_shooting = df
105
150
  return self.opponent_shooting
106
151
 
107
- def get_player_clutch(self):
152
+ def get_player_clutch(self, standardize: bool = False):
108
153
  """
109
154
  Retrieves the player clutch data for the specified season.
110
155
 
156
+ Args:
157
+ standardize: Whether to apply data standardization
158
+
111
159
  Returns:
112
160
  pandas.DataFrame: The player clutch data for the specified season.
113
161
  """
114
- self.player_clutch = nba.LeagueDashPlayerClutch(
162
+ df = nba.LeagueDashPlayerClutch(
115
163
  season=self.season,
116
164
  season_type_all_star=self.season_type,
117
165
  per_mode_detailed=self.permode,
118
166
  ).get_data_frames()[0]
167
+
168
+ if standardize:
169
+ df = standardize_dataframe(
170
+ df,
171
+ data_type="season",
172
+ season=self.season,
173
+ playoffs=(self.season_type == "Playoffs"),
174
+ )
175
+
176
+ self.player_clutch = df
119
177
  return self.player_clutch
120
178
 
121
- def get_player_shots(self):
179
+ def get_player_shots(self, standardize: bool = False):
122
180
  """
123
181
  Retrieves the player shots data for the specified season, season type, and per mode.
124
182
 
183
+ Args:
184
+ standardize: Whether to apply data standardization
185
+
125
186
  Returns:
126
187
  pandas.DataFrame: The player shots data.
127
188
  """
128
- self.player_shots = nba.LeagueDashPlayerPtShot(
189
+ df = nba.LeagueDashPlayerPtShot(
129
190
  season=self.season,
130
191
  season_type_all_star=self.season_type,
131
192
  per_mode_simple=self.permode,
132
193
  ).get_data_frames()[0]
194
+
195
+ if standardize:
196
+ df = standardize_dataframe(
197
+ df,
198
+ data_type="season",
199
+ season=self.season,
200
+ playoffs=(self.season_type == "Playoffs"),
201
+ )
202
+
203
+ self.player_shots = df
133
204
  return self.player_shots
134
205
 
135
- def get_player_shot_locations(self):
206
+ def get_player_shot_locations(self, standardize: bool = False):
136
207
  """
137
208
  Retrieves the shot locations data for the players in the specified season.
138
209
 
210
+ Args:
211
+ standardize: Whether to apply data standardization
212
+
139
213
  Returns:
140
214
  pandas.DataFrame: A DataFrame containing the shot locations data for the players.
141
215
  """
142
- self.player_shot_locations = nba.LeagueDashPlayerShotLocations(
216
+ df = nba.LeagueDashPlayerShotLocations(
143
217
  season=self.season,
144
218
  season_type_all_star=self.season_type,
145
219
  per_mode_detailed=self.permode,
146
220
  ).get_data_frames()[0]
221
+
222
+ if standardize:
223
+ df = standardize_dataframe(
224
+ df,
225
+ data_type="season",
226
+ season=self.season,
227
+ playoffs=(self.season_type == "Playoffs"),
228
+ )
229
+
230
+ self.player_shot_locations = df
147
231
  return self.player_shot_locations
148
232
 
149
- def get_player_stats(self):
233
+ def get_player_stats(self, standardize: bool = False):
150
234
  """
151
235
  Retrieves the player statistics for the specified season.
152
236
 
237
+ Args:
238
+ standardize: Whether to apply data standardization
239
+
153
240
  Returns:
154
241
  pandas.DataFrame: A DataFrame containing the player statistics.
155
242
  """
156
- self.player_stats = nba.LeagueDashPlayerStats(
243
+ df = nba.LeagueDashPlayerStats(
157
244
  season=self.season,
158
245
  season_type_all_star=self.season_type,
159
246
  per_mode_detailed=self.permode,
160
247
  ).get_data_frames()[0]
248
+
249
+ if standardize:
250
+ df = standardize_dataframe(
251
+ df,
252
+ data_type="season",
253
+ season=self.season,
254
+ playoffs=(self.season_type == "Playoffs"),
255
+ )
256
+
257
+ self.player_stats = df
161
258
  return self.player_stats
162
259
 
163
- def get_team_clutch(self):
260
+ def get_team_clutch(self, standardize: bool = False):
164
261
  """
165
262
  Retrieves the clutch statistics for teams in the specified season.
166
263
 
264
+ Args:
265
+ standardize: Whether to apply data standardization
266
+
167
267
  Returns:
168
268
  pandas.DataFrame: A DataFrame containing the clutch statistics for teams.
169
269
  """
170
- self.team_clutch = nba.LeagueDashTeamClutch(
270
+ df = nba.LeagueDashTeamClutch(
171
271
  season=self.season,
172
272
  season_type_all_star=self.season_type,
173
273
  per_mode_detailed=self.permode,
174
274
  ).get_data_frames()[0]
275
+
276
+ if standardize:
277
+ df = standardize_dataframe(
278
+ df,
279
+ data_type="season",
280
+ season=self.season,
281
+ playoffs=(self.season_type == "Playoffs"),
282
+ )
283
+
284
+ self.team_clutch = df
175
285
  return self.team_clutch
176
286
 
177
- def get_team_shots_bypoint(self):
287
+ def get_team_shots_bypoint(self, standardize: bool = False):
178
288
  """
179
289
  Retrieves the team shots by point data for the specified season.
180
290
 
291
+ Args:
292
+ standardize: Whether to apply data standardization
293
+
181
294
  Returns:
182
295
  pandas.DataFrame: The team shots by point data.
183
296
  """
184
- self.team_shots_bypoint = nba.LeagueDashTeamPtShot(
297
+ df = nba.LeagueDashTeamPtShot(
185
298
  season=self.season,
186
299
  season_type_all_star=self.season_type,
187
300
  per_mode_simple=self.permode,
188
301
  ).get_data_frames()[0]
302
+
303
+ if standardize:
304
+ df = standardize_dataframe(
305
+ df,
306
+ data_type="season",
307
+ season=self.season,
308
+ playoffs=(self.season_type == "Playoffs"),
309
+ )
310
+
311
+ self.team_shots_bypoint = df
189
312
  return self.team_shots_bypoint
190
313
 
191
- def get_team_shot_locations(self):
314
+ def get_team_shot_locations(self, standardize: bool = False):
192
315
  """
193
316
  Retrieves the shot locations data for teams in a specific season.
194
317
 
318
+ Args:
319
+ standardize: Whether to apply data standardization
320
+
195
321
  Returns:
196
322
  pandas.DataFrame: The shot locations data for teams.
197
323
  """
198
- self.team_shot_locations = nba.LeagueDashTeamShotLocations(
324
+ df = nba.LeagueDashTeamShotLocations(
199
325
  season=self.season,
200
326
  season_type_all_star=self.season_type,
201
327
  per_mode_detailed=self.permode,
202
328
  ).get_data_frames()[0]
329
+
330
+ if standardize:
331
+ df = standardize_dataframe(
332
+ df,
333
+ data_type="season",
334
+ season=self.season,
335
+ playoffs=(self.season_type == "Playoffs"),
336
+ )
337
+
338
+ self.team_shot_locations = df
203
339
  return self.team_shot_locations
204
340
 
205
- def get_team_stats(self):
341
+ def get_team_stats(self, standardize: bool = False):
206
342
  """
207
343
  Retrieves the team statistics for the specified season.
208
344
 
345
+ Args:
346
+ standardize: Whether to apply data standardization
347
+
209
348
  Returns:
210
349
  pandas.DataFrame: A DataFrame containing the team statistics.
211
350
  """
212
- self.team_stats = nba.LeagueDashTeamStats(
351
+ df = nba.LeagueDashTeamStats(
213
352
  season=self.season,
214
353
  season_type_all_star=self.season_type,
215
354
  per_mode_detailed=self.permode,
216
355
  ).get_data_frames()[0]
356
+
357
+ if standardize:
358
+ df = standardize_dataframe(
359
+ df,
360
+ data_type="season",
361
+ season=self.season,
362
+ playoffs=(self.season_type == "Playoffs"),
363
+ )
364
+
365
+ self.team_stats = df
217
366
  return self.team_stats
218
367
 
219
- def get_player_games(self) -> pd.DataFrame:
368
+ def get_player_games(self, standardize: bool = False) -> pd.DataFrame:
220
369
  """
221
370
  Retrieves the player games data for the specified season, season type, and per mode.
222
371
 
372
+ Args:
373
+ standardize: Whether to apply data standardization
374
+
223
375
  Returns:
224
376
  pd.DataFrame: A DataFrame containing the player games data.
225
377
  """
226
- self.player_games = nba.PlayerGameLogs(
378
+ df = nba.PlayerGameLogs(
227
379
  season_nullable=self.season,
228
380
  season_type_nullable=self.season_type,
229
381
  per_mode_simple_nullable=self.permode,
230
382
  ).get_data_frames()[0]
383
+
384
+ if standardize:
385
+ df = standardize_dataframe(
386
+ df,
387
+ data_type="season",
388
+ season=self.season,
389
+ playoffs=(self.season_type == "Playoffs"),
390
+ )
391
+
392
+ self.player_games = df
231
393
  return self.player_games
232
394
 
233
- def get_team_games(self):
395
+ def get_team_games(self, standardize: bool = False):
234
396
  """
235
397
  Retrieves the game log for a specific team in a given season.
236
398
 
399
+ Args:
400
+ standardize: Whether to apply data standardization
401
+
237
402
  Returns:
238
403
  pandas.DataFrame: The game log data for the team.
239
404
  """
240
- self.team_games = nba.LeagueGameLog(
405
+ df = nba.LeagueGameLog(
241
406
  season=self.season,
242
407
  season_type_all_star=self.season_type,
243
408
  player_or_team_abbreviation="T",
244
409
  ).get_data_frames()[0]
410
+
411
+ if standardize:
412
+ df = standardize_dataframe(
413
+ df,
414
+ data_type="season",
415
+ season=self.season,
416
+ playoffs=(self.season_type == "Playoffs"),
417
+ )
418
+
419
+ self.team_games = df
245
420
  return self.team_games
246
421
 
247
- def get_player_hustle(self):
422
+ def get_player_hustle(self, standardize: bool = False):
248
423
  """
249
424
  Retrieves the hustle stats for players in the specified season and season type.
250
425
 
426
+ Args:
427
+ standardize: Whether to apply data standardization
428
+
251
429
  Returns:
252
430
  pandas.DataFrame: A DataFrame containing the player hustle stats.
253
431
  """
254
- self.player_hustle = nba.LeagueHustleStatsPlayer(
432
+ df = nba.LeagueHustleStatsPlayer(
255
433
  season=self.season,
256
434
  season_type_all_star=self.season_type,
257
435
  ).get_data_frames()[0]
436
+
437
+ if standardize:
438
+ df = standardize_dataframe(
439
+ df,
440
+ data_type="season",
441
+ season=self.season,
442
+ playoffs=(self.season_type == "Playoffs"),
443
+ )
444
+
445
+ self.player_hustle = df
258
446
  return self.player_hustle
259
447
 
260
- def get_team_hustle(self):
448
+ def get_team_hustle(self, standardize: bool = False):
261
449
  """
262
450
  Retrieves the team hustle stats for the specified season and season type.
263
451
 
452
+ Args:
453
+ standardize: Whether to apply data standardization
454
+
264
455
  Returns:
265
456
  pandas.DataFrame: The team hustle stats for the specified season and season type.
266
457
  """
267
- self.team_hustle = nba.LeagueHustleStatsTeam(
458
+ df = nba.LeagueHustleStatsTeam(
268
459
  season=self.season,
269
460
  season_type_all_star=self.season_type,
270
461
  ).get_data_frames()[0]
462
+
463
+ if standardize:
464
+ df = standardize_dataframe(
465
+ df,
466
+ data_type="season",
467
+ season=self.season,
468
+ playoffs=(self.season_type == "Playoffs"),
469
+ )
470
+
471
+ self.team_hustle = df
271
472
  return self.team_hustle
272
473
 
273
- def get_player_matchups(self):
474
+ def get_player_matchups(self, standardize: bool = False):
274
475
  """
275
476
  Retrieves the player matchups for the current season.
276
477
 
478
+ Args:
479
+ standardize: Whether to apply data standardization
480
+
277
481
  Returns:
278
482
  pandas.DataFrame: The player matchups data for the current season.
279
483
  """
280
- self.player_matchups = nba.LeagueSeasonMatchups(
484
+ df = nba.LeagueSeasonMatchups(
281
485
  season=self.season,
282
486
  season_type_playoffs=self.season_type,
283
487
  per_mode_simple=self.permode,
284
488
  ).get_data_frames()[0]
489
+
490
+ if standardize:
491
+ df = standardize_dataframe(
492
+ df,
493
+ data_type="season",
494
+ season=self.season,
495
+ playoffs=(self.season_type == "Playoffs"),
496
+ )
497
+
498
+ self.player_matchups = df
285
499
  return self.player_matchups
286
500
 
287
- def get_player_estimated_metrics(self):
501
+ def get_player_estimated_metrics(self, standardize: bool = False):
288
502
  """
289
503
  Retrieves the estimated metrics for players in the specified season and season type.
290
504
 
505
+ Args:
506
+ standardize: Whether to apply data standardization
507
+
291
508
  Returns:
292
509
  pandas.DataFrame: A DataFrame containing the estimated metrics for players.
293
510
  """
294
- self.player_estimated_metrics = nba.PlayerEstimatedMetrics(
511
+ df = nba.PlayerEstimatedMetrics(
295
512
  season=self.season,
296
513
  season_type=self.season_type,
297
514
  ).get_data_frames()[0]
515
+
516
+ if standardize:
517
+ df = standardize_dataframe(
518
+ df,
519
+ data_type="season",
520
+ season=self.season,
521
+ playoffs=(self.season_type == "Playoffs"),
522
+ )
523
+
524
+ self.player_estimated_metrics = df
298
525
  return self.player_estimated_metrics
299
526
 
300
527
  def get_synergy_player(
@@ -331,7 +558,6 @@ class Season:
331
558
  else:
332
559
  df_list = []
333
560
  for play in track(self.play_type):
334
-
335
561
  temp_df = nba.SynergyPlayTypes(
336
562
  season=self.season,
337
563
  per_mode_simple=self.permode,
@@ -429,7 +655,6 @@ class Season:
429
655
  else:
430
656
  df_list = []
431
657
  for play in track(self.play_type):
432
-
433
658
  temp_df = nba.LeagueDashPtStats(
434
659
  season=self.season,
435
660
  per_mode_simple=self.permode,
@@ -471,7 +696,6 @@ class Season:
471
696
  else:
472
697
  df_list = []
473
698
  for play in track(self.play_type):
474
-
475
699
  temp_df = nba.LeagueDashPtStats(
476
700
  season=self.season,
477
701
  per_mode_simple=self.permode,
@@ -515,7 +739,6 @@ class Season:
515
739
  else:
516
740
  df_list = []
517
741
  for play in track(self.play_type):
518
-
519
742
  temp_df = nba.LeagueDashPtDefend(
520
743
  season=self.season,
521
744
  per_mode_simple=self.permode,
@@ -557,7 +780,6 @@ class Season:
557
780
  else:
558
781
  df_list = []
559
782
  for play in track(self.play_type):
560
-
561
783
  temp_df = nba.LeagueDashPtTeamDefend(
562
784
  season=self.season,
563
785
  per_mode_simple=self.permode,