BSADF Help in maybe DataFrame code

What am I missing?

I have the following code:

import numpy as np
import pandas as pd
import yfinance as yf
import statsmodels.api as sm
import matplotlib.pyplot as plt

ticker = ‘DOGE-USD’
start = ‘2020-09-01’
end = ‘2021-06-15’
prices = yf.download(ticker, start, end)[‘Close’]
r0 = int(len(prices)*0.1)
adf_lags = 3
crit = 1.49
log_prices = np.array(np.log(prices))
delta_log_prices = log_prices[1:] - log_prices[:-1]
n = len(delta_log_prices)
BSADF = np.array()
for r2 in range(r0,n):
ADFS = np.array()
for r1 in range(0,r2-r0+1):
X0 = log_prices[r1:r2+1]
X = pd.DataFrame()
X[0] = X0
for j in range(1,adf_lags+1):
X[j] = np.append(np.zeros(j),delta_log_prices[r1:r2+1-j])
X = np.array(X)
Y = delta_log_prices[r1:r2+1]
reg = sm.OLS(Y,sm.add_constant(X))
res = reg.fit()
ADFS = np.append(ADFS, res.params[1]/res.bse[1])
BSADF = np.append(BSADF, max(ADFS))
plt.rc(‘xtick’, labelsize=14)
plt.plot(prices.index[r0+1:], BSADF)
plt.plot(prices.index[r0+1:],np.ones(len(BSADF))*crit)
print(prices.index[r0+1:][BSADF > crit])

Error message: Traceback (most recent call last):
File “/Users/frederickadjei/PycharmProjects/PythonProject/Trial/BSADF.py”, line 23, in
X[0] = X0
~^^^
File “/Users/frederickadjei/PycharmProjects/PythonProject/Trial/.venv/lib/python3.14/site-packages/pandas/core/frame.py”, line 4672, in setitem
self._set_item(key, value)
~~~~~~~~~~~~~~^^^^^^^^^^^^
File “/Users/frederickadjei/PycharmProjects/PythonProject/Trial/.venv/lib/python3.14/site-packages/pandas/core/frame.py”, line 4872, in _set_item
value, refs = self._sanitize_column(value)
~~~~~~~~~~~~~~~~~~~~~^^^^^^^
File “/Users/frederickadjei/PycharmProjects/PythonProject/Trial/.venv/lib/python3.14/site-packages/pandas/core/frame.py”, line 5744, in _sanitize_column
self._ensure_valid_index(value)
~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
File “/Users/frederickadjei/PycharmProjects/PythonProject/Trial/.venv/lib/python3.14/site-packages/pandas/core/frame.py”, line 4942, in _ensure_valid_index
raise ValueError(
…<2 lines>…
) from err
ValueError: Cannot set a frame with no defined index and a value that cannot be converted to a Series

Thank you in advance!Preformatted text

What is missing is formatting your script. :wink:

Paste your code, then select what you have just posted such that it is highlighted (only the code and not the associated error messeges) then click the </> symbol on the top tool bar. This will auto-format the script so that it appears as it does on your computer terminal.

So that it appears something like this:

def foo(var1, var2):
    
    sqr1 = np.sqrt(var1)
    sqr2 = np.sqrt(var2)
    
    print(sqr1)
    print(sqr2)

as opposed to un-formatted like this:

def foo(var1, var2):

sqr1 = np.sqrt(var1)
sqr2 = np.sqrt(var2)

print(sqr1)
print(sqr2)

If it is formatted correctly, it will be easier for people to read your code and in a better position to help you.

Here is the code:

import numpy as np
import pandas as pd
import yfinance as yf
import statsmodels.api as sm
import matplotlib.pyplot as plt

ticker = ‘DOGE-USD’
start = ‘2020-09-01’
end = ‘2021-06-15’
prices = yf.download(ticker, start, end)[‘Close’]
r0 = int(len(prices)*0.1)
adf_lags = 3
crit = 1.49
log_prices = np.array(np.log(prices))
delta_log_prices = log_prices[1:] - log_prices[:-1]
n = len(delta_log_prices)
BSADF = np.array()
for r2 in range(r0,n):
     ADFS = np.array()
     for r1 in range(0,r2-r0+1):
     X0 = log_prices[r1:r2+1]
     X = pd.DataFrame()
     X[0] = X0
     for j in range(1,adf_lags+1):
         X[j] = np.append(np.zeros(j),delta_log_prices[r1:r2+1-j])
     X = np.array(X)
     Y = delta_log_prices[r1:r2+1]
     reg = sm.OLS(Y,sm.add_constant(X))
     res = reg.fit()
     ADFS = np.append(ADFS,        res.params[1]/res.bse[1])
 BSADF = np.append(BSADF, max(ADFS))
plt.rc(‘xtick’, labelsize=14)
plt.plot(prices.index[r0+1:], BSADF)
plt.plot(prices.index[r0+1:],np.ones(len(BSADF))*crit)
print(prices.index[r0+1:][BSADF > crit])

Error message: Traceback (most recent call last):
File “/Users/frederickadjei/PycharmProjects/PythonProject/Trial/BSADF.py”, line 23, in
X[0] = X0
~^^^
File “/Users/frederickadjei/PycharmProjects/PythonProject/Trial/.venv/lib/python3.14/site-packages/pandas/core/frame.py”, line 4672, in setitem
self._set_item(key, value)

File “/Users/frederickadjei/PycharmProjects/PythonProject/Trial/.venv/lib/python3.14/site-packages/pandas/core/frame.py”, line 4872, in _set_item
value, refs = self._sanitize_column(value)
~~~~~~~~~~~~~~~~~~~~~^^^^^^^
File “/Users/frederickadjei/PycharmProjects/PythonProject/Trial/.venv/lib/python3.14/site-packages/pandas/core/frame.py”, line 5744, in _sanitize_column
self._ensure_valid_index(value)
~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
File “/Users/frederickadjei/PycharmProjects/PythonProject/Trial/.venv/lib/python3.14/site-packages/pandas/core/frame.py”, line 4942, in _ensure_valid_index
raise ValueError(
…<2 lines>…
) from err
ValueError: Cannot set a frame with no defined index and a value that cannot be converted to a Series

If you refer to your script and the error that is highlighted, it specifically points out this line:

X0 = log_prices[r1:r2+1]

This is the exact line that immediately follows the for loop statement. Please be aware thatPython is an indentation centric language. Therefore, if you want the following line statements to be part of the for loop statement, they must be indented. Because it is not indented, Python is generating an error stating in effect: ‘You’ve created a for loop here but it is missing a body to process’. Please insert a body’.

Reference this tutorial and jump to the for loops indentation section:


import numpy as np
import pandas as pd
import yfinance as yf
import statsmodels.api as sm
import matplotlib.pyplot as plt

ticker = 'DOGE-USD'
start = '2020-09-01'
end = '2021-06-15'
prices = yf.download(ticker, start, end)['Close']
r0 = int(len(prices)*0.1)
adf_lags = 3
crit = 1.49
log_prices = np.array(np.log(prices))
delta_log_prices = log_prices[1:] - log_prices[:-1]
n = len(delta_log_prices)
BSADF = np.array([])
for r2 in range(r0,n):
    ADFS = np.array([])
    for r1 in range(0,r2-r0+1):
        X0 = log_prices[r1:r2+1]
        X = pd.DataFrame()
        X[0] = X0
        for j in range(1,adf_lags+1):
            X[j] = np.append(np.zeros(j),delta_log_prices[r1:r2+1-j])
        X = np.array(X)
        Y = delta_log_prices[r1:r2+1]
        reg = sm.OLS(Y,sm.add_constant(X))
        res = reg.fit()
        ADFS = np.append(ADFS, res.params[1]/res.bse[1])
    BSADF = np.append(BSADF, max(ADFS))
plt.rc('xtick', labelsize=14)
plt.plot(prices.index[r0+1:], BSADF)
plt.plot(prices.index[r0+1:],np.ones(len(BSADF))*crit)
print(prices.index[r0+1:][BSADF > crit])

With indentation, I still have a DataFrame error.

It turns out my code also needed this:

prices = yf.download(ticker, start=start, end=end)['Close'].squeeze()

It worked after this change.
Thank you so much!