Skip to content

Commit b08d599

Browse files
committed
Merge pull request #7708 from sinhrks/dtmixin_reso
API: Add PeriodIndex.resolution
2 parents 7f10211 + 93db6e1 commit b08d599

File tree

5 files changed

+52
-11
lines changed

5 files changed

+52
-11
lines changed

doc/source/v0.15.0.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ Enhancements
122122
- Added support for bool, uint8, uint16 and uint32 datatypes in ``to_stata`` (:issue:`7097`, :issue:`7365`)
123123

124124

125+
- ``PeriodIndex`` supports ``resolution`` as the same as ``DatetimeIndex`` (:issue:`7708`)
126+
125127

126128

127129

pandas/core/base.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,17 @@ def __unicode__(self):
498498
summary += self._format_footer()
499499
return summary
500500

501+
@cache_readonly
502+
def _resolution(self):
503+
from pandas.tseries.frequencies import Resolution
504+
return Resolution.get_reso_from_freq(self.freqstr)
505+
506+
@cache_readonly
507+
def resolution(self):
508+
"""
509+
Returns day, hour, minute, second, millisecond or microsecond
510+
"""
511+
from pandas.tseries.frequencies import get_reso_string
512+
return get_reso_string(self._resolution)
513+
514+

pandas/tests/test_base.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,14 @@ def test_representation(self):
605605
result = getattr(idx, func)()
606606
self.assertEqual(result, expected)
607607

608+
def test_resolution(self):
609+
for freq, expected in zip(['A', 'Q', 'M', 'D', 'H', 'T', 'S', 'L', 'U'],
610+
['day', 'day', 'day', 'day',
611+
'hour', 'minute', 'second', 'millisecond', 'microsecond']):
612+
for tz in [None, 'Asia/Tokyo', 'US/Eastern']:
613+
idx = pd.date_range(start='2013-04-01', periods=30, freq=freq, tz=tz)
614+
self.assertEqual(idx.resolution, expected)
615+
608616

609617
class TestPeriodIndexOps(Ops):
610618
_allowed = '_allow_period_index_ops'
@@ -729,6 +737,14 @@ def test_representation(self):
729737
result = getattr(idx, func)()
730738
self.assertEqual(result, expected)
731739

740+
def test_resolution(self):
741+
for freq, expected in zip(['A', 'Q', 'M', 'D', 'H', 'T', 'S', 'L', 'U'],
742+
['day', 'day', 'day', 'day',
743+
'hour', 'minute', 'second', 'millisecond', 'microsecond']):
744+
745+
idx = pd.period_range(start='2013-04-01', periods=30, freq=freq)
746+
self.assertEqual(idx.resolution, expected)
747+
732748

733749
if __name__ == '__main__':
734750
import nose

pandas/tseries/frequencies.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ class Resolution(object):
4545
RESO_HR: 'hour',
4646
RESO_DAY: 'day'}
4747

48-
_reso_period_map = {
48+
_str_reso_map = dict([(v, k) for k, v in compat.iteritems(_reso_str_map)])
49+
50+
_reso_freq_map = {
4951
'year': 'A',
5052
'quarter': 'Q',
5153
'month': 'M',
@@ -57,13 +59,28 @@ class Resolution(object):
5759
'microsecond': 'U',
5860
'nanosecond': 'N'}
5961

62+
_freq_reso_map = dict([(v, k) for k, v in compat.iteritems(_reso_freq_map)])
63+
6064
@classmethod
6165
def get_str(cls, reso):
6266
return cls._reso_str_map.get(reso, 'day')
6367

68+
@classmethod
69+
def get_reso(cls, resostr):
70+
return cls._str_reso_map.get(resostr, cls.RESO_DAY)
71+
6472
@classmethod
6573
def get_freq(cls, resostr):
66-
return cls._reso_period_map[resostr]
74+
return cls._reso_freq_map[resostr]
75+
76+
@classmethod
77+
def get_str_from_freq(cls, freq):
78+
return cls._freq_reso_map.get(freq, 'day')
79+
80+
@classmethod
81+
def get_reso_from_freq(cls, freq):
82+
return cls.get_reso(cls.get_str_from_freq(freq))
83+
6784

6885
def get_reso_string(reso):
6986
return Resolution.get_str(reso)
@@ -593,7 +610,7 @@ def _period_alias_dictionary():
593610

594611

595612
def _infer_period_group(freqstr):
596-
return _period_group(Resolution._reso_period_map[freqstr])
613+
return _period_group(Resolution._reso_freq_map[freqstr])
597614

598615

599616
def _period_group(freqstr):

pandas/tseries/index.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,14 +1536,6 @@ def is_normalized(self):
15361536
"""
15371537
return tslib.dates_normalized(self.asi8, self.tz)
15381538

1539-
@cache_readonly
1540-
def resolution(self):
1541-
"""
1542-
Returns day, hour, minute, second, or microsecond
1543-
"""
1544-
reso = self._resolution
1545-
return get_reso_string(reso)
1546-
15471539
@cache_readonly
15481540
def _resolution(self):
15491541
return tslib.resolution(self.asi8, self.tz)

0 commit comments

Comments
 (0)