I am using yahooquery to download historical data for APPL
When I print the results I can see there is the Date alongside the price. However when I download the result in a csv file the date is no longer available.
As I would need to also have the date for the price I am wondering if I am doing anything wrong and if so how can I fix it
from yahooquery import Ticker
import numpy as np
aapl = Ticker('APPL')
price = aapl.history(period='max')
np.savetxt('C:\APPL.csv', price)
Yahooquery uses multiple index, so you should reset it to single index.
Use
price = price.reset_index(level=[0,1])
This will tranform the symbol column into index and the date column into first column
Related
import pandas as pd
import yfinance as yf
import pendulum
from datetime import date
today = str(date.today())
print(today)
pd.options.display.max_rows=390
start = pendulum.parse('2022-12-5 08:30')
end = pendulum.parse('2022-12-5 15:00')
stock = input("Enter a stock ticker symbol: ")
print(stock + " 1 Minute Data")
print(start)
print(yf.download(tickers= stock, interval="1m", start=start, end=end))
Running the code and typing in "TSLA" will load up every tick for the specified date. How would I export this array in a clean fashion to excel?
Side note: I was also trying to put today's date instead of pendulum's manual date '2022-12-5'
Is there a way to also use the current date for pendulum.parse instead of manually typing it out every time? I tried making the date a variable but got an error etc.
Well, I suspect yf.download is returning a pandas dataframe, so you could just save it to Excel using panda's to_excel function, unless there is more structure or processing you need to do.
df = yf.download(...)
df.to_excel('ticker_data.xlsx')
Question 1:
It returns a dataframe but you need to reset the index to bring Datetime column from index to column. You can chain all the commands together.
Question 2:
pendulum.parse() takes a str so you just need to use an fstring.
import pendulum
import yfinance as yf
start = pendulum.parse(f"{pendulum.now().date()} 08:30")
end = pendulum.parse(f"{pendulum.now().date()} 15:00")
stock = input("Enter a stock ticker symbol: ")
(yf
.download(tickers=stock, interval="1m", start=start, end=end)
.reset_index()
.to_excel(f"/path/to/file/{stock.lower()}.xlsx", index=False, sheet_name=stock)
)
I am new to python and exploring to get data from yahoo fin for dividends but the date on the list is the announcement date not the required payment date. Can the date listed be changed by adding extra days which would be variable for different companies.
import yahoo_fin.stock_info as si
import pandas as pd
si.get_data("WBC.AX",start_date="3/02/1992", end_date="13/10/2022")
si.get_dividends("wbc.ax")
df = pd.DataFrame(div)
df.to_excel('WBC_Div.xlsx', startcol=4, startrow=12)
dividend ticker
1988-06-10 0.124664 WBC.AX
1989-01-01 0.099731 WBC.AX
1989-06-05 0.069522 WBC.AX
1989-12-27 0.274260 WBC.AX
1990-06-07 0.249328 WBC.AX
As I have got this far and have no idea how to make the date change by adding a number of days to the date listed prior sending to excel or do I have to change in excel?
I have a dataframe that contains 1 years of weekly OHLC data.
What do I need ?
list only the last monday's data of each month. For example, May has 5 weeks and I want to list the last monday's data of may and need to discard the rest. Here's the code that I tried and I'm able to list the data on weekly basis. I got stuck here!
Any help would be appreciated!
import pandas as pd
import yfinance as yf
import datetime
from datetime import date, timedelta
periods=pd.date_range(start='2021-4-30',periods=60,freq='W')
start = periods[0].strftime('%Y-%m-%d')
end = periods[-1].strftime('%Y-%m-%d')
symbol="^NSEI"
df=yf.download(symbol,start,end,interval="1wk",index=periods)
You can use groupby(pd.Grouper()) to group by month and get the latest record.
# reset index to flatten columns
df = df.reset_index()
# copy date column to label last monday of a month
df['last_monday_of_month'] = df['Date']
# groupby month and get latest record
df.groupby(pd.Grouper(freq='M', key='Date')).last().reset_index()
I am trying to download data from Yahoo Finance and I have the following code to accomplish this; the code works well.
import pandas as pd
from pandas_datareader import data as pdr
ticker=['CBRL','DRI','MSFT']
start_date = '2015-01-01'
end_date = '2016-06-30'
data=pd.DataFrame()
for x in ticker:
panel_data = pdr.DataReader(x, 'yahoo', start_date, end_date)
data = data.append(panel_data)
My only problem is that the above doesn't retain the stock identifiers, ie once I have all data in one dataframe, I can't which ticker symbol each series corresponds to. I have tried using panel_data.to_frame() after running all of the above, but nothing changes. I am still stuck with all the data, but no ticker identifiers for each of my rows. Can I please have someone's thoughts?
I'm using Python's Pandas to get some finance data from Yahoo Finance, which is a builtin functionality of pandas_datareader. I wish to access the dates it prints out but it doesn't seem to be in the columns nor in the json I require but is there when I print out the object:
from pandas_datareader.data import DataReader
import datetime
start = datetime.datetime(2015, 6, 1)
goog = DataReader('GOOGL', 'yahoo', start)
goog.to_json(None, orient="records") # this doesn't include dates!
print(goog) # this prints out the dates along with the other data!
print(goog.columns) # prints every column except the date column
How can I include the dates along with the other data in the json string?
list(goog.index) gives you the dates as a list of timestamps.
For getting the dates in the json, I had a quick look at the docs. Try this:
print goog.to_json(orient="index", date_format='iso')
Pandas dataframes have an index that is used for grouping and fast lookups. The index is not considered one of the columns. To move the dates from the index to the columns, you can use reset_index(), which will make Date a column and make the index just a sequence of numbers counting up from 0.
So to export as JSON with the dates included:
goog.reset_index().to_json(None, orient='records', date_format='iso')