Skip to content

API: Add PeriodIndex.resolution #7708

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

Merged
merged 1 commit into from
Jul 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/v0.15.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)




Expand Down
14 changes: 14 additions & 0 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


16 changes: 16 additions & 0 deletions pandas/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
23 changes: 20 additions & 3 deletions pandas/tseries/frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
8 changes: 0 additions & 8 deletions pandas/tseries/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down