Skip to content

Commit

Permalink
TST: unit test for #1459
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Jun 14, 2012
1 parent e95be2d commit 7b7c0a0
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 45 deletions.
88 changes: 44 additions & 44 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2562,6 +2562,50 @@ def rename(self, mapper, inplace=False):
def weekday(self):
return Series([d.weekday() for d in self.index], index=self.index)

def tz_convert(self, tz, copy=True):
"""
Convert TimeSeries to target time zone
Parameters
----------
tz : string or pytz.timezone object
copy : boolean, default True
Also make a copy of the underlying data
Returns
-------
converted : TimeSeries
"""
new_index = self.index.tz_convert(tz)

new_values = self.values
if copy:
new_values = new_values.copy()

return Series(new_values, index=new_index, name=self.name)

def tz_localize(self, tz, copy=True):
"""
Localize tz-naive TimeSeries to target time zone
Parameters
----------
tz : string or pytz.timezone object
copy : boolean, default True
Also make a copy of the underlying data
Returns
-------
localized : TimeSeries
"""
new_index = self.index.tz_localize(tz)

new_values = self.values
if copy:
new_values = new_values.copy()

return Series(new_values, index=new_index, name=self.name)


_INDEX_TYPES = ndarray, Index, list, tuple

Expand Down Expand Up @@ -2766,50 +2810,6 @@ def between_time(self, start_time, end_time, include_start=True,
include_start=include_start,
include_end=include_end)

def tz_convert(self, tz, copy=True):
"""
Convert TimeSeries to target time zone
Parameters
----------
tz : string or pytz.timezone object
copy : boolean, default True
Also make a copy of the underlying data
Returns
-------
converted : TimeSeries
"""
new_index = self.index.tz_convert(tz)

new_values = self.values
if copy:
new_values = new_values.copy()

return Series(new_values, index=new_index, name=self.name)

def tz_localize(self, tz, copy=True):
"""
Localize tz-naive TimeSeries to target time zone
Parameters
----------
tz : string or pytz.timezone object
copy : boolean, default True
Also make a copy of the underlying data
Returns
-------
localized : TimeSeries
"""
new_index = self.index.tz_localize(tz)

new_values = self.values
if copy:
new_values = new_values.copy()

return Series(new_values, index=new_index, name=self.name)

def to_timestamp(self, freq=None, how='start', copy=True):
"""
Cast to datetimeindex of timestamps, at *beginning* of period
Expand Down
11 changes: 10 additions & 1 deletion pandas/tseries/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,16 @@ def equals(self, other):
except:
return False

return self.tz == other.tz and np.array_equal(self.asi8, other.asi8)
if self.tz is not None:
if other.tz is None:
return False
same_zone = self.tz.zone == other.tz.zone
else:
if other.tz is not None:
return False
same_zone = True

return same_zone and np.array_equal(self.asi8, other.asi8)

def insert(self, loc, item):
"""
Expand Down
25 changes: 25 additions & 0 deletions pandas/tseries/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@

bday = BDay()


def _skip_if_no_pytz():
try:
import pytz
except ImportError:
raise nose.SkipTest


class TestResample(unittest.TestCase):

def setUp(self):
Expand Down Expand Up @@ -421,6 +429,7 @@ def test_weekly_resample_buglet(self):
expected = ts.resample('W-SUN')
assert_series_equal(resampled, expected)


def _simple_ts(start, end, freq='D'):
rng = date_range(start, end, freq=freq)
return Series(np.random.randn(len(rng)), index=rng)
Expand Down Expand Up @@ -634,6 +643,22 @@ def test_resample_weekly_all_na(self):
expected = ts.asfreq('W-THU', method='ffill')
assert_series_equal(result, expected)

def test_resample_tz_localized(self):
dr = date_range(start='2012-4-13', end='2012-5-1')
ts = Series(range(len(dr)), dr)

ts_utc = ts.tz_localize('UTC')
ts_local = ts_utc.tz_convert('America/Los_Angeles')

result = ts_local.resample('W')

ts_local_naive = ts_local.copy()
ts_local_naive.index = [x.replace(tzinfo=None)
for x in ts_local_naive.index.to_pydatetime()]

exp = ts_local_naive.resample('W').tz_localize('America/Los_Angeles')

assert_series_equal(result, exp)

class TestTimeGrouper(unittest.TestCase):

Expand Down

0 comments on commit 7b7c0a0

Please sign in to comment.