fmilthaler / FinQuant

A program for financial portfolio management, analysis and optimisation.
MIT License
1.38k stars 190 forks source link

ValueError: Could not find column labels in given dataframe. #59

Closed opoloko closed 4 years ago

opoloko commented 4 years ago

Hi

I'm exploring Finquant as it seems really the best library out there for quick portfolio optimisation.

I'm testing it following the examples for csv files, it works with the test provided .csv, when I try with my ones I can create a portfolio equally balanced without problems (so without providing the weights from portfolio.csv), but when I try giving custom weights I receive error:

ValueError Traceback (most recent call last) in 50 print(df_data) 51 ---> 52 pf = build_portfolio(data=df_data, pf_allocation=df_pf) 53 #pf = build_portfolio(data=df_data) 54

/usr/local/lib/python3.7/site-packages/finquant/portfolio.py in build_portfolio(kwargs) 1181 ) 1182 # get portfolio: -> 1183 pf = _build_portfolio_from_df(kwargs) 1184 1185 # final check

/usr/local/lib/python3.7/site-packages/finquant/portfolio.py in _build_portfolio_from_df(data, pf_allocation, datacolumns) 1037 # extract only "Adjusted Close" price ("Adj. Close" in quandl, "Adj Close" in yfinance) 1038 # column from DataFrame: -> 1039 data = _get_stocks_data_columns(data, pf_allocation.Name.values, datacolumns) 1040 # building portfolio: 1041 pf = Portfolio()

/usr/local/lib/python3.7/site-packages/finquant/portfolio.py in _get_stocks_data_columns(data, names, cols) 875 # else, error 876 else: --> 877 raise ValueError("Could not find column labels in given dataframe.") 878 # append correct name to list of correct names 879 reqcolnames.append(colname)

ValueError: Could not find column labels in given dataframe.

I've tried looking into portfolio.py to see why I get the error with my .csv only, but I can't see honestly where is the problem.

This is my stocks file containing the close prices (it works):

EQQQ float64 IWFM float64 IGLT float64 IBTM float64 SGLN float64 STM float64 dtype: object EQQQ IWFM IGLT IBTM SGLN STM Date
2015-01-02 6721.0 1694.75 12.44 128.51 1532.5 246.4 2015-01-05 6713.0 1690.25 12.51 130.25 1554.5 242.0 2015-01-06 6670.0 1684.75 12.61 131.75 1579.0 241.5 2015-01-07 6724.0 1706.00 12.58 132.54 1597.5 245.1 2015-01-08 6866.0 1740.00 12.55 131.73 1593.0 252.7

And this is my portfolio.csv containing the weights:

Allocation float64 Name object dtype: object Allocation Name 0 20.25 EQQQ 1 10.97 IWFM 2 29.44 IGLT 3 28.56 IBTM 4 9.67 SGLN 5 1.11 SMT

Any idea how can I make it work and what's the problem? As I said it works with test data CSV in your repo...

opoloko commented 4 years ago

Adding to that, I even tried creating manually the dataframe:

dfObj = pd.DataFrame(columns=['Allocation', 'Name']) dfObj = dfObj.append({'Allocation': 20.25, 'Name': 'EQQQ'}, ignore_index=True) dfObj = dfObj.append({'Allocation': 10.97, 'Name': 'IWFM'}, ignore_index=True) dfObj = dfObj.append({'Allocation': 29.44, 'Name': 'IGLT'}, ignore_index=True) dfObj = dfObj.append({'Allocation': 28.56, 'Name': 'IBTM'}, ignore_index=True) dfObj = dfObj.append({'Allocation': 9.67, 'Name': 'SGLN'}, ignore_index=True) dfObj = dfObj.append({'Allocation': 1.11, 'Name': 'SMT'}, ignore_index=True)

And trying to pass it as pf_allocation to build_portfolio, same error. It must be something very obvious I can't see...but I tried and I really don't understand what.

Might be a good idea to provide some function in portfolio management to manually alter weights of an existing portfolio instance?

opoloko commented 4 years ago

Found it..sorry...I knew was something stupid I couldn't see...found today, name different STM/SMT between the two .csv files!

fmilthaler commented 4 years ago

Hey there, thanks for giving FinQuant a try, and reporting an issue. Even better that you found the mistake yourself. As for your suggestion, I am going to create an issue to add said feature: allow user to alter weights/allocation of existing portfolio.

opoloko commented 4 years ago

Thanks @fmilthaler I think that would be great function also because FinQuant has most of the function needed to analyse a portfolio, so it's great as portfolio analyser/backtesting as much as it is for optimisation.

Now that I found the problem I'm greatly expanding my portfolio analysis system and FinQuant is a good part of it :-)