Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERR: Resample pad on existing freq causes recursion error #12770

Closed
max-sixty opened this issue Apr 1, 2016 · 3 comments

Comments

Projects
None yet
3 participants
@max-sixty
Copy link
Contributor

commented Apr 1, 2016

Not a big bug, but not ideal behavior:

In [26]: series=pd.Series(range(10), pd.period_range(start='2000', periods=10, name='date', freq='M'))

In [25]: series
Out[25]: 
date
2000-01    0
2000-02    1
2000-03    2
2000-04    3
2000-05    4
2000-06    5
2000-07    6
2000-08    7
2000-09    8
2000-10    9
Freq: M, dtype: int64


In [30]: series.resample('M').first()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-30-4637af8f5d6f> in <module>()
----> 1 series.resample('M').first()

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/tseries/resample.py in f(self, _method)
    465 
    466     def f(self, _method=method):
--> 467         return self._downsample(_method)
    468     f.__doc__ = getattr(GroupBy, method).__doc__
    469     setattr(Resampler, method, f)

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/tseries/resample.py in _downsample(self, how, **kwargs)
    709                          'resampled to {freq}'.format(
    710                              axfreq=ax.freq,
--> 711                              freq=self.freq))
    712 
    713     def _upsample(self, method, limit=None):

ValueError: Frequency <MonthEnd> cannot be resampled to <MonthEnd>

# --> which is fair, even if I could imagine it returning itself

In [31]: series.resample('M').pad()

---------------------------------------------------------------------------
RecursionError                            Traceback (most recent call last)
<ipython-input-24-9df8c7209780> in <module>()
----> 1 series.resample('M').pad()

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/tseries/resample.py in pad(self, limit)
    393         DataFrame.fillna
    394         """
--> 395         return self._upsample('pad', limit=limit)
    396     ffill = pad
    397 

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/tseries/resample.py in _upsample(self, method, limit)
    736 
    737         if not is_superperiod(ax.freq, self.freq):
--> 738             return self.asfreq()
    739 
    740         # Start vs. end of period

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/tseries/resample.py in asfreq(self)
    435         essentially a reindex with (no filling)
    436         """
--> 437         return self._upsample(None)
    438 
    439     def std(self, ddof=1):

# ...............

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/tseries/period.py in _generate_range(cls, start, end, periods, freq, fields)
    200                 raise ValueError('Can either instantiate from fields '
    201                                  'or endpoints, but not both')
--> 202             subarr, freq = _get_ordinal_range(start, end, periods, freq)
    203         elif field_count > 0:
    204             subarr, freq = _range_from_fields(freq=freq, **fields)

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/tseries/period.py in _get_ordinal_range(start, end, periods, freq, mult)
    995 
    996     if start is not None:
--> 997         start = Period(start, freq)
    998     if end is not None:
    999         end = Period(end, freq)

pandas/src/period.pyx in pandas._period.Period.__init__ (pandas/src/period.c:10867)()

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/importlib/_bootstrap.py in _find_and_load(name, import_)

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/importlib/_bootstrap.py in __enter__(self)

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/importlib/_bootstrap.py in _get_module_lock(name)

RecursionError: maximum recursion depth exceeded

Expected Output

Raise on the initial resample? Return itself? Deliberately raise on pad?

output of pd.show_versions()

18.0

@max-sixty max-sixty changed the title BUG: Resample on existing freq causes recursion error BUG: Resample pad on existing freq causes recursion error Apr 1, 2016

@max-sixty max-sixty changed the title BUG: Resample pad on existing freq causes recursion error ERR: Resample pad on existing freq causes recursion error Apr 1, 2016

@jreback jreback added this to the 0.18.1 milestone Apr 1, 2016

@jreback

This comment has been minimized.

Copy link
Contributor

commented Apr 1, 2016

this is a 1-line fix.

elif ax.freq == self.freq:
    return self

in _downsample.

may need testing with both period & datetime

@jaibot

This comment has been minimized.

Copy link

commented Apr 7, 2016

any workaround right now?

@jreback

This comment has been minimized.

Copy link
Contributor

commented Apr 7, 2016

change the index to a DateimeIndex (you can anchor at how='start' or 'end'

In [8]: series.index = series.index.to_timestamp()

In [9]: series
Out[9]: 
date
2000-01-01    0
2000-02-01    1
2000-03-01    2
2000-04-01    3
2000-05-01    4
2000-06-01    5
2000-07-01    6
2000-08-01    7
2000-09-01    8
2000-10-01    9
Freq: MS, dtype: int64

In [10]: series.resample('M').first()
Out[10]: 
date
2000-01-31    0
2000-02-29    1
2000-03-31    2
2000-04-30    3
2000-05-31    4
2000-06-30    5
2000-07-31    6
2000-08-31    7
2000-09-30    8
2000-10-31    9
Freq: M, dtype: int64

In [11]: series.resample('M').pad()
Out[11]: 
date
2000-01-31    0
2000-02-29    1
2000-03-31    2
2000-04-30    3
2000-05-31    4
2000-06-30    5
2000-07-31    6
2000-08-31    7
2000-09-30    8
2000-10-31    9
Freq: M, dtype: int64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.