⚡ Optimize DataFrame to dict conversion in _get_stock_stats_bulk
Replaced the inefficient `iterrows()` loop with vectorized operations:
`df.set_index("Date")[indicator].fillna("N/A").astype(str).to_dict()`
This change improves performance while maintaining the exact original behavior for NaN values and string conversion.
Co-authored-by: aguzererler <6199053+aguzererler@users.noreply.github.com>
This commit is contained in:
parent
5799bb3f00
commit
ed23290b5f
|
|
@ -253,18 +253,8 @@ def _get_stock_stats_bulk(
|
||||||
df[indicator] # This triggers stockstats to calculate the indicator
|
df[indicator] # This triggers stockstats to calculate the indicator
|
||||||
|
|
||||||
# Create a dictionary mapping date strings to indicator values
|
# Create a dictionary mapping date strings to indicator values
|
||||||
result_dict = {}
|
# Optimized: replaced iterrows() with vectorized operations for performance
|
||||||
for _, row in df.iterrows():
|
return df.set_index("Date")[indicator].fillna("N/A").astype(str).to_dict()
|
||||||
date_str = row["Date"]
|
|
||||||
indicator_value = row[indicator]
|
|
||||||
|
|
||||||
# Handle NaN/None values
|
|
||||||
if pd.isna(indicator_value):
|
|
||||||
result_dict[date_str] = "N/A"
|
|
||||||
else:
|
|
||||||
result_dict[date_str] = str(indicator_value)
|
|
||||||
|
|
||||||
return result_dict
|
|
||||||
|
|
||||||
|
|
||||||
def get_stockstats_indicator(
|
def get_stockstats_indicator(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue