MikeRand
MikeRand

Reputation: 4838

Why am I getting an is_monotonic assertion error using pandas Series.asfreq

I have a Python function historical_data that pulls daily historical price and dividend data from Yahoo Finance and outputs it into a pandas DataFrame.

>>> nlsn = y.historical_data('NLSN')
>>> nlsn
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 366 entries, 2012-07-10 00:00:00 to 2011-01-27 00:00:00
Data columns:
Open         366  non-null values
High         366  non-null values
Low          366  non-null values
Close        366  non-null values
Volume       366  non-null values
Adj Close    366  non-null values
Dividends    366  non-null values
dtypes: float64(6), int64(1)
>>> nlsn['Adj Close']
Date
2012-07-10    26.77
2012-07-09    26.77
2012-07-06    26.64
2012-07-05    26.56
2012-07-03    26.57
...
2011-02-01    25.75
2011-01-31    26.07
2011-01-28    25.00
2011-01-27    25.40
Name: Adj Close, Length: 366

I only want to store daily data persistently (vs. having to store daily, monthly, weekly, etc.). The following daily-to-monthly conversion doesn't seem to work, though:

>>> nlsn['Adj Close'].asfreq('M', method='bfill')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/michael/Projects/envs/fintools32/lib/python3.2/site-packages/pandas/core/generic.py", line 156, in asfreq
    return asfreq(self, freq, method=method, how=how)
  File "/home/michael/Projects/envs/fintools32/lib/python3.2/site-packages/pandas/tseries/resample.py", line 329, in asfreq
    return obj.reindex(dti, method=method)
  File "/home/michael/Projects/envs/fintools32/lib/python3.2/site-packages/pandas/core/series.py", line 2053, in reindex
    level=level, limit=limit)
  File "/home/michael/Projects/envs/fintools32/lib/python3.2/site-packages/pandas/core/index.py", line 791, in reindex
    limit=limit)
  File "/home/michael/Projects/envs/fintools32/lib/python3.2/site-packages/pandas/core/index.py", line 719, in get_indexer
    assert(self.is_monotonic)
AssertionError

What's the right way for me to aggregate these stock prices into Monthly?

What I've tried

I tried all different method arguments (ffill, pad, bfill), all of which seem to raise the same assertion error.

I tried checking the source code index.py, but there seems to be a Strategy pattern in effect where the class in question delegates is_monotonic to its _engine attribute, and I can't find where the _engine attribute is actually assigned.

Upvotes: 1

Views: 1211

Answers (1)

Chang She
Chang She

Reputation: 16960

Try nlsn['Adj Close'][::-1].asfreq('M', method='ffill')

And if you can get your function to return ascending order DatetimeIndex that would allow you to skip the extra sorting here.

Upvotes: 2

Related Questions