James McCool commited on
Commit
2cd1df4
·
1 Parent(s): f7c99c4

Refactor combined dataframe handling in app.py for improved clarity and performance

Browse files

- Simplified the logic for managing the combined dataframe by removing unnecessary checks for 'portfolio_df', ensuring consistent handling of contest data.
- Streamlined vectorized string operations to directly apply to the 'Contest' dataframe, enhancing data integrity.
- Updated the working dataframe assignment to always reference 'Contest', improving code readability and maintainability.

Files changed (1) hide show
  1. app.py +9 -20
app.py CHANGED
@@ -212,10 +212,11 @@ with tab1:
212
  if 'portfolio_df' in st.session_state:
213
  st.write('Portfolio data:')
214
  st.dataframe(st.session_state['portfolio_df'].head(25))
215
- st.session_state['combined_df'] = pd.concat([st.session_state['Contest'], st.session_state['portfolio_df']], ignore_index=True)
216
- st.session_state['combined_df'] = st.session_state['combined_df'].reset_index(drop=True)
217
- st.dataframe(st.session_state['combined_df'][st.session_state['combined_df']['BaseName'] == 'Backtesting_upload'].head(25))
218
  st.session_state['entry_list'].append('Backtesting_upload')
 
219
  else:
220
  pass
221
 
@@ -230,20 +231,13 @@ with tab2:
230
  excluded_cols = ['BaseName', 'EntryCount']
231
  exclude_stacks = ['BaseName', 'EntryCount', 'SP', 'SP1', 'SP2', 'P1', 'P2']
232
  if 'Contest' in st.session_state and 'display_contest_info' not in st.session_state:
233
- if 'portfolio_df' in st.session_state:
234
- st.session_state['player_columns'] = [col for col in st.session_state['combined_df'].columns if col not in excluded_cols]
235
- st.session_state['stack_columns'] = [col for col in st.session_state['combined_df'].columns if col not in exclude_stacks]
236
- else:
237
- st.session_state['player_columns'] = [col for col in st.session_state['Contest'].columns if col not in excluded_cols]
238
- st.session_state['stack_columns'] = [col for col in st.session_state['Contest'].columns if col not in exclude_stacks]
239
  print(st.session_state['player_columns'])
240
 
241
  # Vectorized string operations
242
  for col in st.session_state['player_columns']:
243
- if 'portfolio_df' in st.session_state:
244
- st.session_state['combined_df'][col] = st.session_state['combined_df'][col].astype(str).str.strip()
245
- else:
246
- st.session_state['Contest'][col] = st.session_state['Contest'][col].astype(str).str.strip()
247
 
248
  # Create mapping dictionaries
249
  st.session_state['map_dict'] = {
@@ -254,13 +248,8 @@ with tab2:
254
  'own_percent_rank': dict(zip(st.session_state['ownership_df']['Player'], st.session_state['ownership_df']['Own'].rank(pct=True)))
255
  }
256
 
257
- # Create a copy of the dataframe for calculations
258
- if 'portfolio_df' in st.session_state:
259
- working_df = st.session_state['combined_df'].copy()
260
- else:
261
- working_df = st.session_state['Contest'].copy()
262
-
263
- st.write(working_df[working_df['BaseName'] == 'Backtesting_upload'])
264
 
265
  # Pre-compute lookup arrays for vectorized operations
266
  team_map = st.session_state['map_dict']['team_map']
 
212
  if 'portfolio_df' in st.session_state:
213
  st.write('Portfolio data:')
214
  st.dataframe(st.session_state['portfolio_df'].head(25))
215
+ combined_df = pd.concat([st.session_state['Contest'], st.session_state['portfolio_df']], ignore_index=True)
216
+ combined_df = combined_df.reset_index(drop=True)
217
+ st.dataframe(combined_df[combined_df['BaseName'] == 'Backtesting_upload'].head(25))
218
  st.session_state['entry_list'].append('Backtesting_upload')
219
+ st.session_state['Contest'] = combined_df
220
  else:
221
  pass
222
 
 
231
  excluded_cols = ['BaseName', 'EntryCount']
232
  exclude_stacks = ['BaseName', 'EntryCount', 'SP', 'SP1', 'SP2', 'P1', 'P2']
233
  if 'Contest' in st.session_state and 'display_contest_info' not in st.session_state:
234
+ st.session_state['player_columns'] = [col for col in st.session_state['Contest'].columns if col not in excluded_cols]
235
+ st.session_state['stack_columns'] = [col for col in st.session_state['Contest'].columns if col not in exclude_stacks]
 
 
 
 
236
  print(st.session_state['player_columns'])
237
 
238
  # Vectorized string operations
239
  for col in st.session_state['player_columns']:
240
+ st.session_state['Contest'][col] = st.session_state['Contest'][col].astype(str).str.strip()
 
 
 
241
 
242
  # Create mapping dictionaries
243
  st.session_state['map_dict'] = {
 
248
  'own_percent_rank': dict(zip(st.session_state['ownership_df']['Player'], st.session_state['ownership_df']['Own'].rank(pct=True)))
249
  }
250
 
251
+
252
+ working_df = st.session_state['Contest'].copy()
 
 
 
 
 
253
 
254
  # Pre-compute lookup arrays for vectorized operations
255
  team_map = st.session_state['map_dict']['team_map']