5

I have such time series of data, where the 3rd row represents the close value of an index.

DAX 20150728 11173.910156
DAX 20150727 11056.400391
DAX 20150724 11347.450195
DAX 20150723 11512.110352

How can I calculate the log returns of the index using pandas python?

Thank you very much!

Regards

1

3 Answers 3

10

Be careful with

np.log(df['close']).diff() 

as this will fail for indices which can become negative as well as risk factors e.g. negative interest rates. In these cases

np.log(df['close']/df['close'].shift(1)).dropna()

is preferred and based on my experience generally the safer approach. Whether you use +1 or -1 depends on the ordering of your time series. Use -1 for descending and +1 for ascending dates - in both cases the shift provides the preceding date's value.

In this specific example you need to set up the date column as index first, otherwise divide operation will fail:

df['close'].set_index("date",inplace=True)
2
  • Having thought about this, allow me to explain why the second way is better: consider the case where lots of consecutive prices are negative (e.g. ++----++). The first way np.log(df['close']).diff() gives lots of NaNs, but the second way np.log(df['close']/df['close'].shift(1)) gives non-NaN answers when there are consecutive negative prices (e.g. --), only giving NaNs at the boundaries (e.g. +- or -+). May 7, 2022 at 16:50
  • 1
    Thanks Benjamin for the additional explanation!
    – Robert
    May 18, 2022 at 22:28
3

If I understand log returns correctly then the following is what you want:

In [155]:

t="""DAX 20150728 11173.910156
DAX 20150727 11056.400391
DAX 20150724 11347.450195
DAX 20150723 11512.110352"""
df = pd.read_csv(io.StringIO(t), header=None, sep='\s+',names=['exchange', 'date', 'close'], parse_dates=[1])
df
Out[155]:
  exchange       date         close
0      DAX 2015-07-28  11173.910156
1      DAX 2015-07-27  11056.400391
2      DAX 2015-07-24  11347.450195
3      DAX 2015-07-23  11512.110352
In [157]:

df['log return'] = np.log(df['close']) - np.log(df['close'].iloc[0])
df
Out[157]:
  exchange       date         close  log return
0      DAX 2015-07-28  11173.910156    0.000000
1      DAX 2015-07-27  11056.400391   -0.010572
2      DAX 2015-07-24  11347.450195    0.015411
3      DAX 2015-07-23  11512.110352    0.029818

EDIT

OK if it's intra log difference then you can do this succinctly using diff:

In [161]:
df['log return'] = np.log(df['close']).diff()
df

Out[161]:
  exchange       date         close  log return
0      DAX 2015-07-28  11173.910156         NaN
1      DAX 2015-07-27  11056.400391   -0.010572
2      DAX 2015-07-24  11347.450195    0.025984
3      DAX 2015-07-23  11512.110352    0.014406
4
  • log return is logarithm of values ratio. And I think it's more likely day-by-day than day-to-first.
    – hvedrung
    Jul 31, 2015 at 9:30
  • EdChum thank you, this is exactly, what I need, actually I just want to keep the data format, and the name and only change the values of the 3rd column to the log returns.
    – Jorko12
    Jul 31, 2015 at 9:38
  • so you want to overwrite that column? so something like df['close'] = np.log(df['close']).diff()?
    – EdChum
    Jul 31, 2015 at 9:39
  • Yes this is exactly, what I need: just to calculate the log returns in the 3rd column. All other columns should stay as they are.
    – Jorko12
    Jul 31, 2015 at 9:40
-2
    import numpy as np
    df['log return'] = np.log(df[2]/df[2].shift(-1)) 

df is your dataframe which is descending sorted by date.

1
  • 2
    should be np.log(df[2] / df[2].shift(1))
    – BCR
    Mar 1, 2016 at 4:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.