seemethere / nba_py

Python client for NBA statistics located at stats.nba.com
BSD 3-Clause "New" or "Revised" License
1.05k stars 256 forks source link

Trouble creating custom box score data frame #116

Open WillPanarese opened 6 years ago

WillPanarese commented 6 years ago

I have been trying to play around with making a model using this API and have been attempting to create a large dataframe of team and player statistics for any given game. I can create a dataframe that takes all of the advanced statistics from a number of teams and put that into an excel, but when I try to get the advanced statistics for each player from each team, I get the following error: TypeError: cannot concatenate object of type "<type 'instancemethod'>"; only pd.Series, pd.DataFrame, and pd.Panel (deprecated) objs are valid let me know if anyone can help.

rneu31 commented 6 years ago

Can you share some example code?

mneedham91 commented 6 years ago

Agree with @rneu31, please share your code @WillPanarese. Based on the error message this seems like an error in your use of pandas not a problem with nba_py, although I could be wrong!

WillPanarese commented 6 years ago

Here is a sample of code below, I know it is messy and probably redundant, but I can create the first Data frame between the two teams, i get the error message on the second dataframe i try to make with player stats.

import pandas as pd import numpy as np from nba_py import team

teamIDs = ['1610612739', '1610612761']

df_team_stats = [] for id in teamIDs: _team = team.TeamGeneralSplits(id, season='2017-18', measure_type='Advanced') Monday_Games_Teams = _team.location() df_team_stats.append(Monday_Games_Teams)

Monday_Games_Teams = pd.concat(df_team_stats)

CLE_team_stats = team.TeamGeneralSplits('1610612739', season='2017-18', measure_type='Advanced') TOR_team_stats = team.TeamGeneralSplits('1610612761', season='2017-18', measure_type='Advanced')

CLE_stats = CLE_team_stats.location()

cols = list(CLE_stats.columns.values)

print(CLE_stats)

TOR_stats = TOR_team_stats.location()

CLEvsTOR_teams = pd.concat([CLE_stats, TOR_stats])

CLEvsTOR_teams.to_csv("CLEvsTOR_teams.csv") print(CLEvsTOR_teams)

teamIDs = ['1610612739', '1610612761']

df_player_stats = [] for id in teamIDs: _players = team.TeamPlayers(id, season='2017-18', measure_type='Advanced') Monday_Games_Players = _players.season_totals df_player_stats.append(Monday_Games_Players)

Monday_Games_Players = pd.concat(df_player_stats)

CLE_Players1 = team.TeamPlayers('1610612739', season='2017-18', measure_type='Advanced') PHO_Players1 = team.TeamPlayers('1610612765', season='2017-18', measure_type='Advanced')

CLE_player_stats = CLE_Players1.season_totals

cols = list(CLE_player_stats.columns.values)

print(CLE_player_stats)

PHO_player_stats = PHO_Players1.season_totals

CLEvsPHO_players = pd.concat([CLE_player_stats, PHO_player_stats])

CLEvsPHO_players.to_csv('CLEvsPHO_players.csv') print(CLEvsPHO_players)

WillPanarese commented 6 years ago

my end goal is to try to create a custom box score in an excel spreadsheet like in this article http://blog.yhat.com/posts/visualize-nba-pipelines.html

the hope is that I can get all of the statistics I want, then start building an equation combing stats like OFF RTG DEF RTG, PIE, PER, VORP, etc. so I can compare teams playing and try to predict margin. I am just starting to play with this so bear with my mistakes, still learning.

mneedham91 commented 6 years ago

Hi @WillPanarese, I'm not sure if the output is what you were looking for but I made some edits that get the script to run without an error message: https://pastebin.com/9mYcG9C1

I believe I added comments for each change I made.

I suspect there's a lot that could be done to improve your code beyond this but this should help you.