In [22]:
from datetime import datetime

import numpy as np
import pandas as pd

r = np.arange(1, 7).reshape((3, 2))
df = pd.DataFrame(r, columns=list('AB'))

df


Out[22]:
A B
0 1 2
1 3 4
2 5 6

In [23]:
df.pct_change()


Out[23]:
A B
0 NaN NaN
1 2.000000 1.0
2 0.666667 0.5

In [24]:
df = pd.DataFrame(
    [
        [datetime(2018, 1, 1), 12],
        [datetime(2018, 1, 2), 15], 
        [datetime(2018, 1, 3), 10]
    ],
    columns=['Date', 'Price'])

df.set_index('Date')


Out[24]:
Price
Date
2018-01-01 12
2018-01-02 15
2018-01-03 10

In [25]:
df.pct_change()


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/usr/local/lib/python3.6/site-packages/pandas/core/ops.py in na_op(x, y)
   1201         try:
-> 1202             result = expressions.evaluate(op, str_rep, x, y, **eval_kwargs)
   1203         except TypeError:

/usr/local/lib/python3.6/site-packages/pandas/core/computation/expressions.py in evaluate(op, op_str, a, b, use_numexpr, **eval_kwargs)
    203     if use_numexpr:
--> 204         return _evaluate(op, op_str, a, b, **eval_kwargs)
    205     return _evaluate_standard(op, op_str, a, b)

/usr/local/lib/python3.6/site-packages/pandas/core/computation/expressions.py in _evaluate_standard(op, op_str, a, b, **eval_kwargs)
     63     with np.errstate(all='ignore'):
---> 64         return op(a, b)
     65 

TypeError: ufunc true_divide cannot use operands with types dtype('<M8[ns]') and dtype('<M8[ns]')

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-25-b804940da547> in <module>()
----> 1 df.pct_change()

/usr/local/lib/python3.6/site-packages/pandas/core/generic.py in pct_change(self, periods, fill_method, limit, freq, **kwargs)
   6889 
   6890         rs = (data.div(data.shift(periods=periods, freq=freq, axis=axis,
-> 6891                                   **kwargs)) - 1)
   6892         if freq is None:
   6893             mask = isna(_values_from_object(self))

/usr/local/lib/python3.6/site-packages/pandas/core/ops.py in f(self, other, axis, level, fill_value)
   1258 
   1259         if isinstance(other, ABCDataFrame):  # Another DataFrame
-> 1260             return self._combine_frame(other, na_op, fill_value, level)
   1261         elif isinstance(other, ABCSeries):
   1262             return self._combine_series(other, na_op, fill_value, axis, level)

/usr/local/lib/python3.6/site-packages/pandas/core/frame.py in _combine_frame(self, other, func, fill_value, level, try_cast)
   3904                                                     dtype=r.dtype)
   3905 
-> 3906                 result = dict([(col, f(col)) for col in this])
   3907 
   3908             # non-unique

/usr/local/lib/python3.6/site-packages/pandas/core/frame.py in <listcomp>(.0)
   3904                                                     dtype=r.dtype)
   3905 
-> 3906                 result = dict([(col, f(col)) for col in this])
   3907 
   3908             # non-unique

/usr/local/lib/python3.6/site-packages/pandas/core/frame.py in f(col)
   3900 
   3901                 def f(col):
-> 3902                     r = _arith_op(this[col].values, other[col].values)
   3903                     return self._constructor_sliced(r, index=new_index,
   3904                                                     dtype=r.dtype)

/usr/local/lib/python3.6/site-packages/pandas/core/frame.py in _arith_op(left, right)
   3892                 right[right_mask & mask] = fill_value
   3893 
-> 3894             return func(left, right)
   3895 
   3896         if this._is_mixed_type or other._is_mixed_type:

/usr/local/lib/python3.6/site-packages/pandas/core/ops.py in na_op(x, y)
   1219                 if np.prod(xrav.shape) and np.prod(yrav.shape):
   1220                     with np.errstate(all='ignore'):
-> 1221                         result[mask] = op(xrav, yrav)
   1222             elif hasattr(x, 'size'):
   1223                 result = np.empty(x.size, dtype=x.dtype)

TypeError: ufunc true_divide cannot use operands with types dtype('<M8[ns]') and dtype('<M8[ns]')

In [26]:
df = pd.read_pickle('/Users/myles/Projects/pandas/doc/data/fx_prices')

df.tail()


Out[26]:
EU FR GR IT
DATE
2012-01-01 1.2910 NaN NaN NaN
2012-02-01 1.3238 NaN NaN NaN
2012-03-01 1.3208 NaN NaN NaN
2012-04-01 1.3160 NaN NaN NaN
2012-05-01 1.2806 NaN NaN NaN

In [32]:
df[['FR', 'GR', 'IT']].head(5)

df[['FR', 'GR', 'IT']].head(5).to_dict()


Out[32]:
{'FR': {Timestamp('1980-01-01 00:00:00', freq='MS'): 4.0405,
  Timestamp('1980-02-01 00:00:00', freq='MS'): 4.0963,
  Timestamp('1980-03-01 00:00:00', freq='MS'): 4.3149,
  Timestamp('1980-04-01 00:00:00', freq='MS'): 4.3536,
  Timestamp('1980-05-01 00:00:00', freq='MS'): 4.1808},
 'GR': {Timestamp('1980-01-01 00:00:00', freq='MS'): 1.7246,
  Timestamp('1980-02-01 00:00:00', freq='MS'): 1.7482,
  Timestamp('1980-03-01 00:00:00', freq='MS'): 1.8519,
  Timestamp('1980-04-01 00:00:00', freq='MS'): 1.8776,
  Timestamp('1980-05-01 00:00:00', freq='MS'): 1.7913},
 'IT': {Timestamp('1980-01-01 00:00:00', freq='MS'): 804.74,
  Timestamp('1980-02-01 00:00:00', freq='MS'): 810.01,
  Timestamp('1980-03-01 00:00:00', freq='MS'): 860.13,
  Timestamp('1980-04-01 00:00:00', freq='MS'): 876.41,
  Timestamp('1980-05-01 00:00:00', freq='MS'): 843.23}}

In [31]:
df[['FR', 'GR', 'IT']].pct_change().head(5)


Out[31]:
FR GR IT
DATE
1980-01-01 NaN NaN NaN
1980-02-01 0.013810 0.013684 0.006549
1980-03-01 0.053365 0.059318 0.061876
1980-04-01 0.008969 0.013878 0.018927
1980-05-01 -0.039691 -0.045963 -0.037859

In [34]:
df = pd.DataFrame({
    'FR': {'1980-01-01': 4.0405, '1980-02-01': 4.0963, '1980-03-01': 4.3149, '1980-04-01': 4.3536, '1980-05-01': 4.18080},
    'GR': {'1980-01-01': 1.7246, '1980-02-01': 1.7482, '1980-03-01': 1.8519, '1980-04-01': 1.8776, '1980-05-01': 1.7913},
    'IT': {'1980-01-01': 804.74, '1980-02-01': 810.01, '1980-03-01': 860.13, '1980-04-01': 876.41, '1980-05-01': 876.41},
})

df


Out[34]:
FR GR IT
1980-01-01 4.0405 1.7246 804.74
1980-02-01 4.0963 1.7482 810.01
1980-03-01 4.3149 1.8519 860.13
1980-04-01 4.3536 1.8776 876.41
1980-05-01 4.1808 1.7913 876.41

In [35]:
df.pct_change()


Out[35]:
FR GR IT
1980-01-01 NaN NaN NaN
1980-02-01 0.013810 0.013684 0.006549
1980-03-01 0.053365 0.059318 0.061876
1980-04-01 0.008969 0.013878 0.018927
1980-05-01 -0.039691 -0.045963 0.000000

In [37]:
df = pd.DataFrame({
    'FR': [4.0405, 4.0963, 4.3149],
    'GR': [1.7246, 1.7482, 1.8519],
    'IT': [804.74, 810.01, 860.13]},
    index=['1980-01-01', '1980-02-01', '1980-03-01'])

df


Out[37]:
FR GR IT
1980-01-01 4.0405 1.7246 804.74
1980-02-01 4.0963 1.7482 810.01
1980-03-01 4.3149 1.8519 860.13

In [38]:
df.pct_change()


Out[38]:
FR GR IT
1980-01-01 NaN NaN NaN
1980-02-01 0.013810 0.013684 0.006549
1980-03-01 0.053365 0.059318 0.061876

In [ ]: