Skip to content

Support Resampling Weeks to/from FY5253Quarter #12377

Open
@cancan101

Description

@cancan101

52–53-week fiscal year are designed to end on the same day of the week each quarter / year. Therefore it should be possible to upsample / downsample to/from weeks. I made an initial attempt at this in #5148 but that PR was never merged. This logic still makes sense.

/CC @fdion

Currently attempts to do so yield:

import pandas as pd
from pandas.tseries.offsets import FY5253Quarter, Week, WeekDay
from pandas.tseries.period import Period, PeriodIndex, period_range
from datetime import datetime

offset_fyq = FY5253Quarter(weekday=WeekDay.SAT, startingMonth=12,
                       variation="last", qtr_with_extra_week=4)
freq_week = Week(weekday=WeekDay.SAT)

prange = period_range(datetime(2013, 1, 5),
                               periods=2 * 13,
                               freq=freq_week)

df = pd.DataFrame({"A": [1] * 13 + [2] * 13}, index=prange)
resampled = df.resample(offset_fyq, fill_method="mean")
ValueError                                Traceback (most recent call last)
<ipython-input-8-38fa44432b0b> in <module>()
     13 
     14 df = pd.DataFrame({"A": [1] * 13 + [2] * 13}, index=prange)
---> 15 resampled = df.resample(offset_fyq, fill_method="mean")

/Users/alex/.virtualenvs/work/lib/python2.7/site-packages/pandas/core/generic.pyc in resample(self, rule, how, axis, fill_method, closed, label, convention, kind, loffset, limit, base)
   3641                               fill_method=fill_method, convention=convention,
   3642                               limit=limit, base=base)
-> 3643         return sampler.resample(self).__finalize__(self)
   3644 
   3645     def first(self, offset):

/Users/alex/.virtualenvs/work/lib/python2.7/site-packages/pandas/tseries/resample.pyc in resample(self, obj)
     90 
     91             if self.kind is None or self.kind == 'period':
---> 92                 rs = self._resample_periods()
     93             else:
     94                 obj = self.obj.to_timestamp(how=self.convention)

/Users/alex/.virtualenvs/work/lib/python2.7/site-packages/pandas/tseries/resample.pyc in _resample_periods(self)
    340             return obj.reindex(new_index)
    341         else:
--> 342             start = axlabels[0].asfreq(self.freq, how=self.convention)
    343             end = axlabels[-1].asfreq(self.freq, how='end')
    344 

pandas/src/period.pyx in pandas._period.Period.asfreq (pandas/src/period.c:14801)()

/Users/alex/.virtualenvs/work/lib/python2.7/site-packages/pandas/tseries/frequencies.pyc in get_freq_code(freqstr)
    253             # e.g., freqstr = ('T', 5)
    254             try:
--> 255                 code = _period_str_to_code(freqstr[0])
    256                 stride = freqstr[1]
    257             except:

/Users/alex/.virtualenvs/work/lib/python2.7/site-packages/pandas/tseries/frequencies.pyc in _period_str_to_code(freqstr)
    809                           FutureWarning, stacklevel=3)
    810         except KeyError:
--> 811             raise ValueError("Unknown freqstr: %s" % freqstr)
    812 
    813         return _period_code_map[alias]

ValueError: Unknown freqstr: REQ-L-DEC-SAT-4

Also should be able to downsample to Day and upsample to FY5254

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions