Skip to content

Commit 7b7c0a0

Browse files
committed
TST: unit test for #1459
1 parent e95be2d commit 7b7c0a0

File tree

3 files changed

+79
-45
lines changed

3 files changed

+79
-45
lines changed

pandas/core/series.py

+44-44
Original file line numberDiff line numberDiff line change
@@ -2562,6 +2562,50 @@ def rename(self, mapper, inplace=False):
25622562
def weekday(self):
25632563
return Series([d.weekday() for d in self.index], index=self.index)
25642564

2565+
def tz_convert(self, tz, copy=True):
2566+
"""
2567+
Convert TimeSeries to target time zone
2568+
2569+
Parameters
2570+
----------
2571+
tz : string or pytz.timezone object
2572+
copy : boolean, default True
2573+
Also make a copy of the underlying data
2574+
2575+
Returns
2576+
-------
2577+
converted : TimeSeries
2578+
"""
2579+
new_index = self.index.tz_convert(tz)
2580+
2581+
new_values = self.values
2582+
if copy:
2583+
new_values = new_values.copy()
2584+
2585+
return Series(new_values, index=new_index, name=self.name)
2586+
2587+
def tz_localize(self, tz, copy=True):
2588+
"""
2589+
Localize tz-naive TimeSeries to target time zone
2590+
2591+
Parameters
2592+
----------
2593+
tz : string or pytz.timezone object
2594+
copy : boolean, default True
2595+
Also make a copy of the underlying data
2596+
2597+
Returns
2598+
-------
2599+
localized : TimeSeries
2600+
"""
2601+
new_index = self.index.tz_localize(tz)
2602+
2603+
new_values = self.values
2604+
if copy:
2605+
new_values = new_values.copy()
2606+
2607+
return Series(new_values, index=new_index, name=self.name)
2608+
25652609

25662610
_INDEX_TYPES = ndarray, Index, list, tuple
25672611

@@ -2766,50 +2810,6 @@ def between_time(self, start_time, end_time, include_start=True,
27662810
include_start=include_start,
27672811
include_end=include_end)
27682812

2769-
def tz_convert(self, tz, copy=True):
2770-
"""
2771-
Convert TimeSeries to target time zone
2772-
2773-
Parameters
2774-
----------
2775-
tz : string or pytz.timezone object
2776-
copy : boolean, default True
2777-
Also make a copy of the underlying data
2778-
2779-
Returns
2780-
-------
2781-
converted : TimeSeries
2782-
"""
2783-
new_index = self.index.tz_convert(tz)
2784-
2785-
new_values = self.values
2786-
if copy:
2787-
new_values = new_values.copy()
2788-
2789-
return Series(new_values, index=new_index, name=self.name)
2790-
2791-
def tz_localize(self, tz, copy=True):
2792-
"""
2793-
Localize tz-naive TimeSeries to target time zone
2794-
2795-
Parameters
2796-
----------
2797-
tz : string or pytz.timezone object
2798-
copy : boolean, default True
2799-
Also make a copy of the underlying data
2800-
2801-
Returns
2802-
-------
2803-
localized : TimeSeries
2804-
"""
2805-
new_index = self.index.tz_localize(tz)
2806-
2807-
new_values = self.values
2808-
if copy:
2809-
new_values = new_values.copy()
2810-
2811-
return Series(new_values, index=new_index, name=self.name)
2812-
28132813
def to_timestamp(self, freq=None, how='start', copy=True):
28142814
"""
28152815
Cast to datetimeindex of timestamps, at *beginning* of period

pandas/tseries/index.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,16 @@ def equals(self, other):
11541154
except:
11551155
return False
11561156

1157-
return self.tz == other.tz and np.array_equal(self.asi8, other.asi8)
1157+
if self.tz is not None:
1158+
if other.tz is None:
1159+
return False
1160+
same_zone = self.tz.zone == other.tz.zone
1161+
else:
1162+
if other.tz is not None:
1163+
return False
1164+
same_zone = True
1165+
1166+
return same_zone and np.array_equal(self.asi8, other.asi8)
11581167

11591168
def insert(self, loc, item):
11601169
"""

pandas/tseries/tests/test_resample.py

+25
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818

1919
bday = BDay()
2020

21+
22+
def _skip_if_no_pytz():
23+
try:
24+
import pytz
25+
except ImportError:
26+
raise nose.SkipTest
27+
28+
2129
class TestResample(unittest.TestCase):
2230

2331
def setUp(self):
@@ -421,6 +429,7 @@ def test_weekly_resample_buglet(self):
421429
expected = ts.resample('W-SUN')
422430
assert_series_equal(resampled, expected)
423431

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

646+
def test_resample_tz_localized(self):
647+
dr = date_range(start='2012-4-13', end='2012-5-1')
648+
ts = Series(range(len(dr)), dr)
649+
650+
ts_utc = ts.tz_localize('UTC')
651+
ts_local = ts_utc.tz_convert('America/Los_Angeles')
652+
653+
result = ts_local.resample('W')
654+
655+
ts_local_naive = ts_local.copy()
656+
ts_local_naive.index = [x.replace(tzinfo=None)
657+
for x in ts_local_naive.index.to_pydatetime()]
658+
659+
exp = ts_local_naive.resample('W').tz_localize('America/Los_Angeles')
660+
661+
assert_series_equal(result, exp)
637662

638663
class TestTimeGrouper(unittest.TestCase):
639664

0 commit comments

Comments
 (0)