Icon

kn_​forum_​47320_​python_​finance_​data

import some finance data with Python - KNIME forum question (47320)

import some finance data with Python - KNIME forum question (47320)
https://forum.knime.com/t/python-node-5-columns-expected-but-only-2-written-to-csv/47320/3?u=mlauber71

# Copy input to output#output_table_1 = input_table_1.copy() #quasi default commando om output_table te hebbenfrom io import StringIOimport pandas as pdimport yfinance as yf# Importing libraryimport csvtickers0 = ['AALB.AS', 'FB']dfs = [] # list for each ticker's dataframetickers = [yf.Ticker(ticker) for ticker in tickers0] for ticker in tickers: bs = ticker.balancesheet fs = pd.concat([bs]) #pd.concat([pnl, bs, cf]) #print(fs) # make dataframe format nicer # Swap dates and columns #data = fs ##print(data) # reset index (date) into a column ###fs = fs.reset_index() # Rename old index from '' to Date ###fs.columns = ['Date', *fs.columns[1:]] # Add ticker to dataframe fs['Ticker'] = ticker.ticker dfs.append(fs) #print(dfs) with open("out_new.csv","w") as f: wr = csv.writer(f,delimiter="\n") wr.writerow(dfs)with open('Greenblatt_new.csv', 'w+') as f: # "w" means writing write = csv.writer(f) write.writerow(dfs) #write.writerows(data) # reads the written csv . . . fileObject = open("Greenblatt_new.csv", "r") # zie https://pythonexamples.org/python-read-text-file/import_dfs = fileObject.read()print(import_dfs)flow_variables['stdout'] = import_dfs # creates FLOWVARIABLE import some finance data with Python - KNIME forum question (47320)https://forum.knime.com/t/python-node-5-columns-expected-but-only-2-written-to-csv/47320/3?u=mlauber71 # conda env create -f /Users/m_lauber/Dropbox/knime-workspace/forum/2022/kn_forum_47320_python_finance_data/data/py39_knime_yfinance.yml# conda env create -f py39_knime_yfinance.yml# conda env update -f /Users/m_lauber/Dropbox/knime-workspace/forum/2022/kn_forum_47320_python_finance_data/data/py39_knime_yfinance.yml --prune# conda env update -f py39_knime_yfinance.yml --prune# https://forum.knime.com/t/python-environment-creation-problem/41577/7?u=mlauber71# https://docs.knime.com/latest/r_installation_guide/index.html## To activate this environment, use## $ conda activate py39_knime_yfinance## To deactivate an active environment, use## $ conda deactivate# # conda update -n base condaname: py39_knime_yfinance # Name of the created environmentchannels: # Repositories to search for packages- conda-forgedependencies: # List of packages that should be installed- python=3.9 # Python- py4j # used for KNIME <-> Python communication- nomkl # Prevents the use of Intel's MKL- pandas # Table data structures- jedi<=0.17.2 # Python script autocompletion- python-dateutil # Date and Time utilities- numpy # N-dimensional arrays- cairo # SVG support- pillow # Image inputs/outputs- matplotlib # Plotting- pyarrow>=6.0 # Arrow serialization- IPython # Notebook support- nbformat # Notebook support- scipy # Notebook support- python-flatbuffers<2.0 # because tensorflow expects a version before 2- h5py<3.0 # must be < 3.0 because they changed whether str or byte is returned- protobuf>3.12 # Lower protobuf versions do not work with TensorFlow 2- libiconv # MDF Reader node- asammdf=5.19.14 # MDF Reader node- jupyterlab- yfinance- pip- pip: - JPype1 # Databases - skimpy # clean Pandas data Path to your workflow.Makes the workflow portable.Financialdatabasepy39_knime_yfinancefirst data framebrought back for referencelist parquet files in/data/import allParquet files from /data/ticket_results.table Extract ContextProperties Python Script Conda EnvironmentPropagation Python Script(Labs) Merge Variables List Files/Folders Parquet Reader Table Writer # Copy input to output#output_table_1 = input_table_1.copy() #quasi default commando om output_table te hebbenfrom io import StringIOimport pandas as pdimport yfinance as yf# Importing libraryimport csvtickers0 = ['AALB.AS', 'FB']dfs = [] # list for each ticker's dataframetickers = [yf.Ticker(ticker) for ticker in tickers0] for ticker in tickers: bs = ticker.balancesheet fs = pd.concat([bs]) #pd.concat([pnl, bs, cf]) #print(fs) # make dataframe format nicer # Swap dates and columns #data = fs ##print(data) # reset index (date) into a column ###fs = fs.reset_index() # Rename old index from '' to Date ###fs.columns = ['Date', *fs.columns[1:]] # Add ticker to dataframe fs['Ticker'] = ticker.ticker dfs.append(fs) #print(dfs) with open("out_new.csv","w") as f: wr = csv.writer(f,delimiter="\n") wr.writerow(dfs)with open('Greenblatt_new.csv', 'w+') as f: # "w" means writing write = csv.writer(f) write.writerow(dfs) #write.writerows(data) # reads the written csv . . . fileObject = open("Greenblatt_new.csv", "r") # zie https://pythonexamples.org/python-read-text-file/import_dfs = fileObject.read()print(import_dfs)flow_variables['stdout'] = import_dfs # creates FLOWVARIABLE import some finance data with Python - KNIME forum question (47320)https://forum.knime.com/t/python-node-5-columns-expected-but-only-2-written-to-csv/47320/3?u=mlauber71 # conda env create -f /Users/m_lauber/Dropbox/knime-workspace/forum/2022/kn_forum_47320_python_finance_data/data/py39_knime_yfinance.yml# conda env create -f py39_knime_yfinance.yml# conda env update -f /Users/m_lauber/Dropbox/knime-workspace/forum/2022/kn_forum_47320_python_finance_data/data/py39_knime_yfinance.yml --prune# conda env update -f py39_knime_yfinance.yml --prune# https://forum.knime.com/t/python-environment-creation-problem/41577/7?u=mlauber71# https://docs.knime.com/latest/r_installation_guide/index.html## To activate this environment, use## $ conda activate py39_knime_yfinance## To deactivate an active environment, use## $ conda deactivate# # conda update -n base condaname: py39_knime_yfinance # Name of the created environmentchannels: # Repositories to search for packages- conda-forgedependencies: # List of packages that should be installed- python=3.9 # Python- py4j # used for KNIME <-> Python communication- nomkl # Prevents the use of Intel's MKL- pandas # Table data structures- jedi<=0.17.2 # Python script autocompletion- python-dateutil # Date and Time utilities- numpy # N-dimensional arrays- cairo # SVG support- pillow # Image inputs/outputs- matplotlib # Plotting- pyarrow>=6.0 # Arrow serialization- IPython # Notebook support- nbformat # Notebook support- scipy # Notebook support- python-flatbuffers<2.0 # because tensorflow expects a version before 2- h5py<3.0 # must be < 3.0 because they changed whether str or byte is returned- protobuf>3.12 # Lower protobuf versions do not work with TensorFlow 2- libiconv # MDF Reader node- asammdf=5.19.14 # MDF Reader node- jupyterlab- yfinance- pip- pip: - JPype1 # Databases - skimpy # clean Pandas data Path to your workflow.Makes the workflow portable.Financialdatabasepy39_knime_yfinancefirst data framebrought back for referencelist parquet files in/data/import allParquet files from /data/ticket_results.tableExtract ContextProperties Python Script Conda EnvironmentPropagation Python Script(Labs) Merge Variables List Files/Folders Parquet Reader Table Writer

Nodes

Extensions

Links