Spurious Spurious - 3 months ago 10
Python Question

Simple calculation function not working

I have the following code but I cannot get it to work:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

def test_calc(date, price, performance):
test = pd.DataFrame(columns=('date'), index=('date'))
test['date'] = date
test['new_value'] = price * (1 + performance)
return(test)

print(test_calc(1, 100, 0.05))


The problem seems to be:
TypeError: Index(...) must be called with a collection of some kind, 'date' was passed


I don't need it to be a
DataFrame
by the way. I just chose it because I used it before. Everything else failed as well, e.g.
test = []
.

Answer

Use list at pd.DataFrame(columns=[], index=[])

def test_calc(date, price, performance):
    test = pd.DataFrame(columns=['date'], index=['date'])
    test['date'] = date
    test['new_value'] = price * (1 + performance)
    return(test)