Skip to content

API: Datetime-like indexes summary should output the same format #9116

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
Jan 5, 2015
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
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v0.16.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ Backwards incompatible API changes

- ``Index.duplicated`` now returns `np.array(dtype=bool)` rather than `Index(dtype=object)` containing `bool` values. (:issue:`8875`)

- ``DatetimeIndex``, ``PeriodIndex`` and ``TimedeltaIndex.summary`` now output the same format. (:issue:`9116`)
- ``TimedeltaIndex.freqstr`` now output the same string format as ``DatetimeIndex``. (:issue:`9116`)


Deprecations
~~~~~~~~~~~~

Expand Down
27 changes: 27 additions & 0 deletions pandas/tseries/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ def __contains__(self, key):
except (KeyError, TypeError):
return False

@property
def freqstr(self):
""" return the frequency object as a string if its set, otherwise None """
if self.freq is None:
return None
return self.freq.freqstr

@cache_readonly
def inferred_freq(self):
try:
Expand Down Expand Up @@ -459,3 +466,23 @@ def repeat(self, repeats, axis=None):
"""
return self._simple_new(self.values.repeat(repeats),
name=self.name)

def summary(self, name=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you now you are modifying it also add a docstring?

"""
return a summarized representation
"""
formatter = self._formatter_func
if len(self) > 0:
index_summary = ', %s to %s' % (formatter(self[0]),
formatter(self[-1]))
else:
index_summary = ''

if name is None:
name = type(self).__name__
result = '%s: %s entries%s' % (com.pprint_thing(name),
len(self), index_summary)
if self.freq:
result += '\nFreq: %s' % self.freqstr

return result
23 changes: 0 additions & 23 deletions pandas/tseries/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,22 +682,6 @@ def _format_native_types(self, na_rep=u('NaT'),
def to_datetime(self, dayfirst=False):
return self.copy()

def summary(self, name=None):
if len(self) > 0:
index_summary = ', %s to %s' % (com.pprint_thing(self[0]),
com.pprint_thing(self[-1]))
else:
index_summary = ''

if name is None:
name = type(self).__name__
result = '%s: %s entries%s' % (com.pprint_thing(name),
len(self), index_summary)
if self.freq:
result += '\nFreq: %s' % self.freqstr

return result

def _format_footer(self):
tagline = 'Length: %d, Freq: %s, Timezone: %s'
return tagline % (len(self), self.freqstr, self.tz)
Expand Down Expand Up @@ -1392,13 +1376,6 @@ def _set_freq(self, value):
self.offset = value
freq = property(fget=_get_freq, fset=_set_freq, doc="get/set the frequncy of the Index")

@property
def freqstr(self):
""" return the frequency object as a string if its set, otherwise None """
if self.freq is None:
return None
return self.offset.freqstr

year = _field_accessor('year', 'Y', "The year of the datetime")
month = _field_accessor('month', 'M', "The month as January=1, December=12")
day = _field_accessor('day', 'D', "The days of the datetime")
Expand Down
24 changes: 0 additions & 24 deletions pandas/tseries/tdi.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,23 +410,6 @@ def f(x):
result = result.astype('int64')
return result

def summary(self, name=None):
formatter = self._formatter_func
if len(self) > 0:
index_summary = ', %s to %s' % (formatter(self[0]),
formatter(self[-1]))
else:
index_summary = ''

if name is None:
name = type(self).__name__
result = '%s: %s entries%s' % (com.pprint_thing(name),
len(self), index_summary)
if self.freq:
result += '\nFreq: %s' % self.freqstr

return result

def to_pytimedelta(self):
"""
Return TimedeltaIndex as object ndarray of datetime.timedelta objects
Expand Down Expand Up @@ -796,13 +779,6 @@ def __getitem__(self, key):

return self._simple_new(result, self.name)

@property
def freqstr(self):
""" return the frequency object as a string if its set, otherwise None """
if self.freq is None:
return None
return self.freq

def searchsorted(self, key, side='left'):
if isinstance(key, (np.ndarray, Index)):
key = np.array(key, dtype=_TD_DTYPE, copy=False)
Expand Down
96 changes: 92 additions & 4 deletions pandas/tseries/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,34 @@ def test_representation(self):
result = getattr(idx, func)()
self.assertEqual(result, expected)

def test_summary(self):
# GH9116
idx1 = DatetimeIndex([], freq='D')
idx2 = DatetimeIndex(['2011-01-01'], freq='D')
idx3 = DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D')
idx4 = DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03'], freq='D')
idx5 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00', '2011-01-01 11:00'],
freq='H', tz='Asia/Tokyo')
idx6 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00', pd.NaT],
tz='US/Eastern')

exp1 = """DatetimeIndex: 0 entries
Freq: D"""
exp2 = """DatetimeIndex: 1 entries, 2011-01-01 to 2011-01-01
Freq: D"""
exp3 = """DatetimeIndex: 2 entries, 2011-01-01 to 2011-01-02
Freq: D"""
exp4 = """DatetimeIndex: 3 entries, 2011-01-01 to 2011-01-03
Freq: D"""
exp5 = """DatetimeIndex: 3 entries, 2011-01-01 09:00:00+09:00 to 2011-01-01 11:00:00+09:00
Freq: H"""
exp6 = """DatetimeIndex: 3 entries, 2011-01-01 09:00:00-05:00 to NaT"""

for idx, expected in zip([idx1, idx2, idx3, idx4, idx5, idx6],
[exp1, exp2, exp3, exp4, exp5, exp6]):
result = idx.summary()
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',
Expand Down Expand Up @@ -336,16 +364,16 @@ def test_representation(self):


exp1 = """<class 'pandas.tseries.tdi.TimedeltaIndex'>
Length: 0, Freq: <Day>"""
Length: 0, Freq: D"""
exp2 = """<class 'pandas.tseries.tdi.TimedeltaIndex'>
['1 days']
Length: 1, Freq: <Day>"""
Length: 1, Freq: D"""
exp3 = """<class 'pandas.tseries.tdi.TimedeltaIndex'>
['1 days', '2 days']
Length: 2, Freq: <Day>"""
Length: 2, Freq: D"""
exp4 = """<class 'pandas.tseries.tdi.TimedeltaIndex'>
['1 days', ..., '3 days']
Length: 3, Freq: <Day>"""
Length: 3, Freq: D"""
exp5 = """<class 'pandas.tseries.tdi.TimedeltaIndex'>
['1 days 00:00:01', ..., '3 days 00:00:00']
Length: 3, Freq: None"""
Expand All @@ -356,6 +384,29 @@ def test_representation(self):
result = getattr(idx, func)()
self.assertEqual(result, expected)

def test_summary(self):
# GH9116
idx1 = TimedeltaIndex([], freq='D')
idx2 = TimedeltaIndex(['1 days'], freq='D')
idx3 = TimedeltaIndex(['1 days', '2 days'], freq='D')
idx4 = TimedeltaIndex(['1 days', '2 days', '3 days'], freq='D')
idx5 = TimedeltaIndex(['1 days 00:00:01', '2 days', '3 days'])

exp1 = """TimedeltaIndex: 0 entries
Freq: D"""
exp2 = """TimedeltaIndex: 1 entries, '1 days' to '1 days'
Freq: D"""
exp3 = """TimedeltaIndex: 2 entries, '1 days' to '2 days'
Freq: D"""
exp4 = """TimedeltaIndex: 3 entries, '1 days' to '3 days'
Freq: D"""
exp5 = """TimedeltaIndex: 3 entries, '1 days 00:00:01' to '3 days 00:00:00'"""

for idx, expected in zip([idx1, idx2, idx3, idx4, idx5],
[exp1, exp2, exp3, exp4, exp5]):
result = idx.summary()
self.assertEqual(result, expected)

def test_add_iadd(self):

# only test adding/sub offsets as + is now numeric
Expand Down Expand Up @@ -755,6 +806,43 @@ def test_representation(self):
result = getattr(idx, func)()
self.assertEqual(result, expected)

def test_summary(self):
# GH9116
idx1 = PeriodIndex([], freq='D')
idx2 = PeriodIndex(['2011-01-01'], freq='D')
idx3 = PeriodIndex(['2011-01-01', '2011-01-02'], freq='D')
idx4 = PeriodIndex(['2011-01-01', '2011-01-02', '2011-01-03'], freq='D')
idx5 = PeriodIndex(['2011', '2012', '2013'], freq='A')
idx6 = PeriodIndex(['2011-01-01 09:00', '2012-02-01 10:00', 'NaT'], freq='H')

idx7 = pd.period_range('2013Q1', periods=1, freq="Q")
idx8 = pd.period_range('2013Q1', periods=2, freq="Q")
idx9 = pd.period_range('2013Q1', periods=3, freq="Q")

exp1 = """PeriodIndex: 0 entries
Freq: D"""
exp2 = """PeriodIndex: 1 entries, 2011-01-01 to 2011-01-01
Freq: D"""
exp3 = """PeriodIndex: 2 entries, 2011-01-01 to 2011-01-02
Freq: D"""
exp4 = """PeriodIndex: 3 entries, 2011-01-01 to 2011-01-03
Freq: D"""
exp5 = """PeriodIndex: 3 entries, 2011 to 2013
Freq: A-DEC"""
exp6 = """PeriodIndex: 3 entries, 2011-01-01 09:00 to NaT
Freq: H"""
exp7 = """PeriodIndex: 1 entries, 2013Q1 to 2013Q1
Freq: Q-DEC"""
exp8 = """PeriodIndex: 2 entries, 2013Q1 to 2013Q2
Freq: Q-DEC"""
exp9 = """PeriodIndex: 3 entries, 2013Q1 to 2013Q3
Freq: Q-DEC"""

for idx, expected in zip([idx1, idx2, idx3, idx4, idx5, idx6, idx7, idx8, idx9],
[exp1, exp2, exp3, exp4, exp5, exp6, exp7, exp8, exp9]):
result = idx.summary()
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',
Expand Down
2 changes: 1 addition & 1 deletion pandas/tseries/tests/test_timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ def conv(v):
expected = np.array([0, 500000000, 800000000, 1200000000], dtype='timedelta64[ns]')
result = to_timedelta([0., 0.5, 0.8, 1.2], unit='s', box=False)
tm.assert_numpy_array_equal(expected, result)

def testit(unit, transform):

# array
Expand Down