From 93db6e17bc58f09efcb8861b89e6b7f7dd0120bf Mon Sep 17 00:00:00 2001 From: sinhrks Date: Sun, 29 Jun 2014 17:00:45 +0900 Subject: [PATCH] API: Add PeriodIndex.resolution --- doc/source/v0.15.0.txt | 2 ++ pandas/core/base.py | 14 ++++++++++++++ pandas/tests/test_base.py | 16 ++++++++++++++++ pandas/tseries/frequencies.py | 23 ++++++++++++++++++++--- pandas/tseries/index.py | 8 -------- 5 files changed, 52 insertions(+), 11 deletions(-) diff --git a/doc/source/v0.15.0.txt b/doc/source/v0.15.0.txt index eb58f46f0f3fe..e11a3730cd95a 100644 --- a/doc/source/v0.15.0.txt +++ b/doc/source/v0.15.0.txt @@ -117,6 +117,8 @@ Enhancements - Added support for bool, uint8, uint16 and uint32 datatypes in ``to_stata`` (:issue:`7097`, :issue:`7365`) +- ``PeriodIndex`` supports ``resolution`` as the same as ``DatetimeIndex`` (:issue:`7708`) + diff --git a/pandas/core/base.py b/pandas/core/base.py index 72fcfbff677ab..4035627b98458 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -498,3 +498,17 @@ def __unicode__(self): summary += self._format_footer() return summary + @cache_readonly + def _resolution(self): + from pandas.tseries.frequencies import Resolution + return Resolution.get_reso_from_freq(self.freqstr) + + @cache_readonly + def resolution(self): + """ + Returns day, hour, minute, second, millisecond or microsecond + """ + from pandas.tseries.frequencies import get_reso_string + return get_reso_string(self._resolution) + + diff --git a/pandas/tests/test_base.py b/pandas/tests/test_base.py index 832671521c815..761d79a288df3 100644 --- a/pandas/tests/test_base.py +++ b/pandas/tests/test_base.py @@ -605,6 +605,14 @@ def test_representation(self): result = getattr(idx, func)() self.assertEqual(result, expected) + def test_resolution(self): + for freq, expected in zip(['A', 'Q', 'M', 'D', 'H', 'T', 'S', 'L', 'U'], + ['day', 'day', 'day', 'day', + 'hour', 'minute', 'second', 'millisecond', 'microsecond']): + for tz in [None, 'Asia/Tokyo', 'US/Eastern']: + idx = pd.date_range(start='2013-04-01', periods=30, freq=freq, tz=tz) + self.assertEqual(idx.resolution, expected) + class TestPeriodIndexOps(Ops): _allowed = '_allow_period_index_ops' @@ -729,6 +737,14 @@ def test_representation(self): result = getattr(idx, func)() self.assertEqual(result, expected) + def test_resolution(self): + for freq, expected in zip(['A', 'Q', 'M', 'D', 'H', 'T', 'S', 'L', 'U'], + ['day', 'day', 'day', 'day', + 'hour', 'minute', 'second', 'millisecond', 'microsecond']): + + idx = pd.period_range(start='2013-04-01', periods=30, freq=freq) + self.assertEqual(idx.resolution, expected) + if __name__ == '__main__': import nose diff --git a/pandas/tseries/frequencies.py b/pandas/tseries/frequencies.py index fe61e5f0acd9b..4beccaa758006 100644 --- a/pandas/tseries/frequencies.py +++ b/pandas/tseries/frequencies.py @@ -45,7 +45,9 @@ class Resolution(object): RESO_HR: 'hour', RESO_DAY: 'day'} - _reso_period_map = { + _str_reso_map = dict([(v, k) for k, v in compat.iteritems(_reso_str_map)]) + + _reso_freq_map = { 'year': 'A', 'quarter': 'Q', 'month': 'M', @@ -57,13 +59,28 @@ class Resolution(object): 'microsecond': 'U', 'nanosecond': 'N'} + _freq_reso_map = dict([(v, k) for k, v in compat.iteritems(_reso_freq_map)]) + @classmethod def get_str(cls, reso): return cls._reso_str_map.get(reso, 'day') + @classmethod + def get_reso(cls, resostr): + return cls._str_reso_map.get(resostr, cls.RESO_DAY) + @classmethod def get_freq(cls, resostr): - return cls._reso_period_map[resostr] + return cls._reso_freq_map[resostr] + + @classmethod + def get_str_from_freq(cls, freq): + return cls._freq_reso_map.get(freq, 'day') + + @classmethod + def get_reso_from_freq(cls, freq): + return cls.get_reso(cls.get_str_from_freq(freq)) + def get_reso_string(reso): return Resolution.get_str(reso) @@ -593,7 +610,7 @@ def _period_alias_dictionary(): def _infer_period_group(freqstr): - return _period_group(Resolution._reso_period_map[freqstr]) + return _period_group(Resolution._reso_freq_map[freqstr]) def _period_group(freqstr): diff --git a/pandas/tseries/index.py b/pandas/tseries/index.py index dca2947f6a7a6..9423037844e74 100644 --- a/pandas/tseries/index.py +++ b/pandas/tseries/index.py @@ -1536,14 +1536,6 @@ def is_normalized(self): """ return tslib.dates_normalized(self.asi8, self.tz) - @cache_readonly - def resolution(self): - """ - Returns day, hour, minute, second, or microsecond - """ - reso = self._resolution - return get_reso_string(reso) - @cache_readonly def _resolution(self): return tslib.resolution(self.asi8, self.tz)