Having a little trouble. Trying to insert my yfinance data into google sheets. Any help is much appreciated.
!pip install yfinance
import time
import numpy as np
import pandas as pd
from datetime import datetime
import math
from oauth2client.service_account import ServiceAccountCredentials
import gspread
import yfinance as yf
scope = ['https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive.file', 'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('yfinancenegs.json',scope)
client = gspread.authorize(creds)
Structure = client.open('Structure').worksheet('EURUSD')
df = yf.download(tickers='EURUSD=X', period='1d', interval='5m')
df = df.to_json()
Below is where I probably need the most help. It just plots the data into 1 cell.
Structure.update_cell(1,1,df)
Related
I'm using code to copy a df to an excel spreadsheet starting in cell A2.
Here's the code snippet. I'm including all the imports used in the code.
import pandas as pd
import numpy as np
import os
import pathlib
from pathlib import Path
import win32com.client as win32
import datetime as dt
from datetime import timedelta
import openpyxl as xl
import xlwings as xw
NewPortfolio = Path.home().joinpath("Desktop", "test.xlsx")
wb1 = Path.home().joinpath("Desktop", "test2.xlsx")
NewPortfolio_pd = pd.read_excel(NewPortfolio)
app = xw.App(visible=False)
wb = xw.Book(wb1)
ws = wb.sheets['Sheet1']
ws.range('A2').options(index=False).value = NewPortfolio_pd
wb.save()
wb.close()
app.quit()
Here's the error
AttributeError: module 'win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x9' has no attribute 'CLSIDToPackageMap'
I've already tried uninstalling and reinstalling pywin32, and have not been able to find a solution online at the moment.
If you have any comments or recommendations it'd be greatly appreciated
Thanks in advance!
Just a guess
but swap
import win32com.client as win32
with
import win32api as win32
In Jupyter Notebook I created my own function in my_fk.py file like below:
import pandas as pd
def missing_val(df):
df= pd.DataFrame(df.dtypes, columns=["type"])
df["missing"] = pd.DataFrame(df.isna().any())
df["sum_miss"] = pd.DataFrame(df.isna().sum())
df["perc_miss"] = round((df.apply(pd.isna).mean()*100),2)
return df
Then when I try to import and run my function using below code:
import pandas as pd
import numpy as np
import my_fk as fk
df = pd.read_csv("my_data.csv")
fk.missing_val(df)
I have error like below. Error suggests that in my my_fk.py file there is no pandas as pd, but there IS line with code "import pandas as pd". How can I import and use my own function from python file ?
NameError: name 'pd' is not defined
Missing "as". Then place your pd.read_csv() after importing pandas, not before
import pandas as pd
import numpy as np
import my_fk as fk
df = pd.read_csv("my_data.csv")
fk.missing_val(df)
I have written a load job in python using google colab for developing purposes but every time I run the code it loads the index into the bigquery table. However, when I run it on cloud fucntions the same code it does not load the index column.
Index is the default index column pandas creates.
My code is as follow:
import pandas as pd
from google.cloud import bigquery
import time
from google.cloud import storage
import re
import os
from datetime import datetime, date, timezone
from datetime import date
from dateutil import tz
import numpy as np
job_config = bigquery.LoadJobConfig(
schema=[bigquery.SchemaField("fecha", bigquery.enums.SqlTypeNames.DATE)],
write_disposition="WRITE_TRUNCATE"
,create_disposition = "CREATE_IF_NEEDED"
,time_partitioning = bigquery.table.TimePartitioning(field="fecha")
#,schema_update_options = 'ALLOW_FIELD_ADDITION'
)
client = bigquery.Client()
job = client.load_table_from_dataframe(df, table_id,job_config=job_config)
My requirements.txt in cloud functions includes the following libraries
pandas
fsspec
gcsfs
google-cloud-bigquery
pyarrow
google-cloud-storage
openpyxl
I am trying to read a gsheet file in Google drive using Google Collab. I tried using drive.mount to get the file but I don't know how to get a dataframe with pandas from there. Here what I tried to do :
from google.colab import auth
auth.authenticate_user()
import gspread
from oauth2client.client import GoogleCredentials
import os
import pandas as pd
from google.colab import drive
# setup
gc = gspread.authorize(GoogleCredentials.get_application_default())
drive.mount('/content/drive',force_remount=True)
# read data and put it in a dataframe
gsheets = gc.open_by_url('/content/drive/MyDrive/test/myGoogleSheet.gsheet')
As you can tell, I am quite lost with the libraries. I want to use the ability to access the drive with the drive library, to get the content from gspread, and read with pandas.
Can anyone help me find a solution, please ?
I have found a solution for my problem by looking further into the library gspread. I was able to load the gsheet file by id or by url which I did not know. Then I manage to get the content of a sheet and read it as pandas dataframe. Here is the code :
from google.colab import auth
auth.authenticate_user()
import gspread
import pandas as pd
from oauth2client.client import GoogleCredentials
# setup
gc = gspread.authorize(GoogleCredentials.get_application_default())
# read data and put it in a dataframe
# spreadsheet = gc.open_by_url('https://docs.google.com/spreadsheets/d/google_sheet_id/edit#gid=0')
spreadsheet = gc.open_by_key('google_sheet_id')
wks = spreadsheet.worksheet('sheet_name')
data = wks.get_all_values()
headers = data.pop(0)
df = pd.DataFrame(data, columns=headers)
print(df)
I am a beginner in python.
I have successfully put together the script to update my google sheet from some hard coded values
Test1 = [["1/1/2020",4000],["4/4/2020",3000],["7/12/2020",'salah4-tiga1000']]
But when I try to update from a local csv file,
Test2 = pd.read_csv(ordersCSV).to_json()
I can see the csv file data running in my terminal, but it is not updating the google sheet.
Can you help me to see what I missed please.
Thank you in advance.
from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials
from google.oauth2 import service_account
from oauth2client.transport import request
from pprint import pprint
from numpy import greater
import pandas as pd
from pandas.core import series
from pandas.core.indexes.base import Index
from pyasn1.type.univ import Null
SERVICE_ACCOUNT_FILE = 'drfruit4.json'
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
creds = None
creds = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
SPREADSHEET_ID = '1rpgVUAmLojG1_U2SRQ_x9vQGI16mEYiOCGcZLrIRZOQ'
service = build('sheets', 'v4', credentials=creds)
# Call the Sheets API
sheet =service.spreadsheets()
result = sheet.values().get(spreadsheetId=SPREADSHEET_ID,
range="Detail_Exp_SiteGiant_via_Python!A1:Z1100").execute()
values = result.get('values',[])
Test1 = [["1/1/2020",4000],["4/4/2020",3000],["7/12/2020",'salah4-tiga1000']]
ordersCSV = r'Ready4Sofia.csv'
Test2 = pd.read_csv(ordersCSV).to_json()
request = sheet.values().update(spreadsheetId=SPREADSHEET_ID,
range="Detail_Exp_SiteGiant_via_Python!A1", valueInputOption="USER_ENTERED", body={'values':Test2}).execute()
I have found the answer here .
Thanks to Stack Overflow and all the contributors.