2

Im trying to add additional rows to a data frame where the index is DateTime and am not sure how to go about doing this

AAPL=yf.download('AAPL')
AAPL=AAPL['Adj Close']
AAPL.loc[len(AAPL.index)]=['2021-12-04',0]

This is the error message i recieved

TypeError: cannot insert DatetimeArray with incompatible label

1 Answer 1

2

You had better first convert your date string to a datetime object type in pandas and then do whatever you want. This is your method:

import pandas as pd
import yfinance as yf

AAPL = yf.download('AAPL')
AAPL = AAPL['Adj Close']
AAPL.loc[pd.to_datetime('2021-12-04')] = 0
# This is also acceptable
# AAPL.loc['2021-12-04'] = 0
print(AAPL)

And Here is my method to cope with this:

import pandas as pd
import yfinance as yf

AAPL = yf.download('AAPL')
AAPL = AAPL[['Adj Close']]
AAPL = AAPL.append(pd.DataFrame(0, index=[pd.to_datetime('2021-12-04')], columns=AAPL.columns))
print(AAPL)

Output

             Adj Close
1980-12-12    0.100453
1980-12-15    0.095213
1980-12-16    0.088224
1980-12-17    0.090408
1980-12-18    0.093029
...                ...
2021-12-30  178.199997
2021-12-31  177.570007
2022-01-03  182.009995
2022-01-04  179.699997
2021-12-04    0.000000
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.