BUG: Period resample with length=0 doesn't set freq by sinhrks · Pull Request #13067 · pandas-dev/pandas (original) (raw)
I removed a bunch of the if/thens and generally cleared some stuff up, including solving the issues above. The thrust is to solve the root causes of why empty indexes need to be handled differently, rather than adding more special casing
@sinhrks do you want to use this PR for this, or do you want me to start another? Assuming you think it's an improvement
From f1e9a6eaa8608cc21a6ca52d5ce6888ced7f4181 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Tue, 3 May 2016 21🔞21 -0400 Subject: [PATCH] Lots of clearing up PeriodIndex resampling behaviour
pandas/core/groupby.py | 2 +- pandas/tseries/period.py | 2 +- pandas/tseries/resample.py | 18 ++++++------------ pandas/tseries/tests/test_resample.py | 4 +++- 4 files changed, 11 insertions(+), 15 deletions(-)
diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py index 7a47911..3d197ac 100644 --- a/pandas/core/groupby.py +++ b/pandas/core/groupby.py @@ -2669,7 +2669,7 @@ class SeriesGroupBy(GroupBy): def _wrap_applied_output(self, keys, values, not_indexed_same=False): if len(keys) == 0: # GH #6265
return Series([], name=self.name)
return Series([], name=self.name, index=Index(keys)) def _get_index(): if self.grouper.nkeys > 1:
diff --git a/pandas/tseries/period.py b/pandas/tseries/period.py index 478b255..61e0e85 100644 --- a/pandas/tseries/period.py +++ b/pandas/tseries/period.py @@ -271,7 +271,7 @@ class PeriodIndex(DatelikeOps, DatetimeIndexOpsMixin, Int64Index): @classmethod def _simple_new(cls, values, name=None, freq=None, **kwargs): if not getattr(values, 'dtype', None):
values = np.array(values, copy=False)
values = np.array(values, copy=False, dtype='int64') if is_object_dtype(values): return PeriodIndex(values, name=name, freq=freq, **kwargs)
diff --git a/pandas/tseries/resample.py b/pandas/tseries/resample.py index 167c6c1..2240ab6 100644 --- a/pandas/tseries/resample.py +++ b/pandas/tseries/resample.py @@ -789,19 +789,17 @@ class PeriodIndexResampler(DatetimeIndexResampler): ax = self.ax
new_index = self._get_new_index()
if len(new_index) == 0:
result = self._selected_obj
if isinstance(self._selected_obj.index, PeriodIndex):
result = result.asfreq(self.freq, how=self.convention)
return self._wrap_result(result.reindex(new_index)) # Start vs. end of period memb = ax.asfreq(self.freq, how=self.convention) if is_subperiod(ax.freq, self.freq): # Downsampling
rng = np.arange(memb.values[0], memb.values[-1] + 1)
bins = memb.searchsorted(rng, side='right')
if len(new_index) == 0:
bins = []
else:
rng = np.arange(memb.values[0], memb.values[-1] + 1)
bins = memb.searchsorted(rng, side='right') grouper = BinGrouper(bins, new_index) return self._groupby_and_aggregate(how, grouper=grouper) elif is_superperiod(ax.freq, self.freq):
@@ -811,8 +809,7 @@ class PeriodIndexResampler(DatetimeIndexResampler):
raise ValueError('Frequency {axfreq} cannot be '
'resampled to {freq}'.format(
axfreq=ax.freq,
freq=self.freq))
def _upsample(self, method, limit=None): """axfreq=ax.freq, freq=self.freq))
@@ -835,9 +832,6 @@ class PeriodIndexResampler(DatetimeIndexResampler): obj = self.obj new_index = self._get_new_index()
if len(new_index) == 0:
return self._wrap_result(self._selected_obj.reindex(new_index))
# Start vs. end of period memb = ax.asfreq(self.freq, how=self.convention)
diff --git a/pandas/tseries/tests/test_resample.py b/pandas/tseries/tests/test_resample.py index c796277..346c546 100644 --- a/pandas/tseries/tests/test_resample.py +++ b/pandas/tseries/tests/test_resample.py @@ -2053,9 +2053,11 @@ class TestPeriodIndex(Base, tm.TestCase):
for method in resample_methods:
result = getattr(s.resample(freq), method)()
assert_series_equal(result, expected)
if method!='ohlc': # returns df
assert_series_equal(result, expected, check_dtype=False) self.assertEqual(result.index.freq, freq)
def test_resample_count(self): # GH12774
-- 2.6.3