Help with creating a data series of differences

Which way is the most elegant way to create a series of differences on PMI data. How do i get a change in the PMI data istead of the PMI data it self?


fetch_csv("https://www.quandl.com/api/v1/datasets/ISM/MAN_PMI.csv?&trim_start=1948-01-01&trim_end=2017-12-13&sort_order=desc",
symbol = "PMI",
date_column = "Date",
date_format = "%d/$m/%Y")   def my_rebalance(context, data): PMI = data.current("PMI","Index") weights= 0 if PMI >50: weights = ((float(1)/len(context.longs))) for stock in context.longs.index: if stock in context.longs.index and data.can_trade(stock): order_target_percent(stock, weights) for stock in context.portfolio.positions: if stock not in context.longs.index and data.can_trade(stock) and PMI: order_target_percent(stock, 0)  1 response Hi Soren, The best way to do it would be to pre-process your data using the pre_func parameter of fetch_csv. This parameter allows you to specify a function that takes in your imported data as a DataFrame, preforms some pre-processing logic and returns it to fetch_csv. The example below takes in your PMI data, calculates the percentage change and appends it as a new column to your imported data. I hope this helps. 1 Loading... Total Returns -- Alpha -- Beta -- Sharpe -- Sortino -- Max Drawdown -- Benchmark Returns -- Volatility --  Returns 1 Month 3 Month 6 Month 12 Month  Alpha 1 Month 3 Month 6 Month 12 Month  Beta 1 Month 3 Month 6 Month 12 Month  Sharpe 1 Month 3 Month 6 Month 12 Month  Sortino 1 Month 3 Month 6 Month 12 Month  Volatility 1 Month 3 Month 6 Month 12 Month  Max Drawdown 1 Month 3 Month 6 Month 12 Month def initialize(context): fetch_csv( "https://www.quandl.com/api/v1/datasets/ISM/MAN_PMI.csv?&trim_start=1948-01-01&trim_end=2017-12-13&sort_order=desc", pre_func=pct_change, post_func=preview, symbol = "PMI", date_column = "Date", date_format = "%d/$m/%Y")

def preview(df):
log.info(df.tail())
return df

def pct_change(df):
df['pct_change'] = (df.Index - df.Index.shift(-1)) / df.Index.shift(-1)
return df

record(PMI=data.current("PMI","pct_change"))`