Skip to content

BUG: start_time end_time to_timestamp bugs #2124 #2125 #1764 #2170

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 3 commits into from
Nov 4, 2012
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
46 changes: 24 additions & 22 deletions pandas/tseries/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from datetime import datetime, date
import numpy as np

from pandas.tseries.frequencies import (get_freq_code as _gfc, to_offset,
import pandas.tseries.offsets as offsets
from pandas.tseries.frequencies import (get_freq_code as _gfc,
_month_numbers, FreqGroup)
from pandas.tseries.index import DatetimeIndex, Int64Index, Index
from pandas.tseries.tools import parse_time_string
Expand Down Expand Up @@ -180,19 +181,21 @@ def asfreq(self, freq, how='E'):

@property
def start_time(self):
return self.to_timestamp(how='S')
return self.to_timestamp('s', how='S')

@property
def end_time(self):
return self.to_timestamp(how='E')
return self.to_timestamp('s', how='E')

def to_timestamp(self, freq=None, how='S'):
def to_timestamp(self, freq=None, how='start'):
"""
Return the Timestamp at the start/end of the period
Return the Timestamp representation of the Period at the target
frequency at the specified end (how) of the Period

Parameters
----------
freq : string or DateOffset, default frequency of PeriodIndex
freq : string or DateOffset, default is 'D' if self.freq is week or
longer and 'S' otherwise
Target frequency
how: str, default 'S' (start)
'S', 'E'. Can be aliased as case insensitive
Expand All @@ -202,20 +205,16 @@ def to_timestamp(self, freq=None, how='S'):
-------
Timestamp
"""
how = _validate_end_alias(how)

if freq is None:
base, mult = _gfc(self.freq)
how = _validate_end_alias(how)
if how == 'S':
base = _freq_mod.get_to_timestamp_base(base)
freq = _freq_mod._get_freq_str(base)
new_val = self.asfreq(freq, how)
else:
new_val = self
else:
base, mult = _gfc(freq)
new_val = self.asfreq(freq, how)
freq = _freq_mod.get_to_timestamp_base(base)

base, mult = _gfc(freq)
val = self.asfreq(freq, how)

dt64 = plib.period_ordinal_to_dt64(new_val.ordinal, base)
dt64 = plib.period_ordinal_to_dt64(val.ordinal, base)
return Timestamp(dt64)

year = _period_field_accessor('year', 0)
Expand Down Expand Up @@ -765,20 +764,23 @@ def to_timestamp(self, freq=None, how='start'):

Parameters
----------
freq : string or DateOffset, default 'D'
freq : string or DateOffset, default 'D' for week or longer, 'S'
otherwise
Target frequency
how : {'s', 'e', 'start', 'end'}

Returns
-------
DatetimeIndex
"""
how = _validate_end_alias(how)

if freq is None:
base, mult = _gfc(self.freq)
new_data = self
else:
base, mult = _gfc(freq)
new_data = self.asfreq(freq, how)
freq = _freq_mod.get_to_timestamp_base(base)

base, mult = _gfc(freq)
new_data = self.asfreq(freq, how)

new_data = plib.periodarr_to_dt64arr(new_data.values, base)
return DatetimeIndex(new_data, freq='infer', name=self.name)
Expand Down
45 changes: 28 additions & 17 deletions pandas/tseries/tests/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,12 @@ def test_to_timestamp(self):
start_ts = p.to_timestamp(how='S')
aliases = ['s', 'StarT', 'BEGIn']
for a in aliases:
self.assertEquals(start_ts, p.to_timestamp(how=a))
self.assertEquals(start_ts, p.to_timestamp('D', how=a))

end_ts = p.to_timestamp(how='E')
aliases = ['e', 'end', 'FINIsH']
for a in aliases:
self.assertEquals(end_ts, p.to_timestamp(how=a))
self.assertEquals(end_ts, p.to_timestamp('D', how=a))

from_lst = ['A', 'Q', 'M', 'W', 'B',
'D', 'H', 'Min', 'S']
Expand All @@ -231,7 +231,7 @@ def test_to_timestamp(self):

self.assertEquals(p.start_time, p.to_timestamp(how='S'))

self.assertEquals(p.end_time, p.to_timestamp(how='E'))
self.assertEquals(p.end_time, p.to_timestamp('s', how='E'))

# Frequency other than daily

Expand All @@ -245,8 +245,8 @@ def test_to_timestamp(self):
expected = datetime(1985, 12, 31, 23, 59)
self.assertEquals(result, expected)

result = p.to_timestamp('S', how='end')
expected = datetime(1985, 12, 31, 23, 59, 59)
result = p.to_timestamp(how='end')
expected = datetime(1985, 12, 31)
self.assertEquals(result, expected)

expected = datetime(1985, 1, 1)
Expand All @@ -272,28 +272,30 @@ def test_start_time(self):

def test_end_time(self):
p = Period('2012', freq='A')
xp = datetime(2012, 12, 31)
xp = datetime(2012, 12, 31, 23, 59, 59)
self.assertEquals(xp, p.end_time)

p = Period('2012', freq='Q')
xp = datetime(2012, 3, 31)
xp = datetime(2012, 3, 31, 23, 59, 59)
self.assertEquals(xp, p.end_time)

p = Period('2012', freq='M')
xp = datetime(2012, 1, 31)
xp = datetime(2012, 1, 31, 23, 59, 59)
self.assertEquals(xp, p.end_time)

xp = datetime(2012, 1, 1)
freq_lst = ['D', 'H', 'T', 'S']
for f in freq_lst:
p = Period('2012', freq=f)
self.assertEquals(p.end_time, xp)
xp = datetime(2012, 1, 1, 23, 59, 59)
p = Period('2012', freq='D')
self.assertEquals(p.end_time, xp)

xp = datetime(2012, 1, 1, 0, 59, 59)
p = Period('2012', freq='H')
self.assertEquals(p.end_time, xp)

self.assertEquals(Period('2012', freq='B').end_time,
datetime(2011, 12, 30))
datetime(2011, 12, 30, 23, 59, 59))

self.assertEquals(Period('2012', freq='W').end_time,
datetime(2012, 1, 1))
datetime(2012, 1, 1, 23, 59, 59))


def test_properties_annually(self):
Expand Down Expand Up @@ -1200,12 +1202,12 @@ def test_to_timestamp(self):
series = Series(1, index=index, name='foo')

exp_index = date_range('1/1/2001', end='12/31/2009', freq='A-DEC')
result = series.to_timestamp('D', 'end')
result = series.to_timestamp(how='end')
self.assert_(result.index.equals(exp_index))
self.assertEquals(result.name, 'foo')

exp_index = date_range('1/1/2001', end='1/1/2009', freq='AS-DEC')
result = series.to_timestamp('D', 'start')
result = series.to_timestamp(how='start')
self.assert_(result.index.equals(exp_index))


Expand All @@ -1230,6 +1232,15 @@ def _get_with_delta(delta, freq='A-DEC'):

self.assertRaises(ValueError, index.to_timestamp, '5t')

index = PeriodIndex(freq='H', start='1/1/2001', end='1/2/2001')
series = Series(1, index=index, name='foo')

exp_index = date_range('1/1/2001 00:59:59', end='1/2/2001 00:59:59',
freq='H')
result = series.to_timestamp(how='end')
self.assert_(result.index.equals(exp_index))
self.assertEquals(result.name, 'foo')

def test_to_timestamp_quarterly_bug(self):
years = np.arange(1960, 2000).repeat(4)
quarters = np.tile(range(1, 5), 40)
Expand Down