tidy-finance / website

This repository hosts the source code for the website tidy-finance.org
https://tidy-finance.org
Other
82 stars 47 forks source link

.melt() returns ValueError in pandas 2.2.2 #110

Closed soryko closed 4 months ago

soryko commented 4 months ago

When running the melt() function in the index_prices. .melt(ignore_index=False, var_name=["variable", "symbol"])

it returns a ValueError:

ValueError: var_name=['variable', 'symbol'] must be a scalar.

christophscheuch commented 4 months ago

Thank you for raising this issue, @soryko. Can you please give us your pandas version?

soryko commented 4 months ago

Thank you for getting back to me. I'm using version 2.2.2

christophscheuch commented 4 months ago

Thank you for the information. I can replicate your issue once I upgrade to pandas 2.2.2. I think the original approach worked by accident because there was a missing validation in the old pandas version. We will check all instances of melt() soon, but in the meantime, the following approach does the trick:

index_prices = (yf.download(
    tickers=symbols, 
    start="2000-01-01", 
    end="2022-12-31", 
    progress=False
  ))

index_prices = (index_prices
  .stack()
  .reset_index(level=1, drop=False)
  .reset_index()
  .rename(columns={
    "Date": "date",
    "level_1": "symbol",
    "Open": "open",
    "High": "high",
    "Low": "low",
    "Close": "close",
    "Adj Close": "adjusted",
    "Volume": "volume"}
  )
)