Skip to content

Commit 660823a

Browse files
committed
Merge pull request pandas-dev#8036 from sinhrks/tz_except
API: Timetamp.tz_localize and tz_convert raises TypeError rathar
2 parents 0ecb4cb + 2e6ded1 commit 660823a

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

doc/source/v0.15.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ API changes
172172

173173
- ``DataFrame.tz_localize`` and ``DataFrame.tz_convert`` now accepts an optional ``level`` argument
174174
for localizing a specific level of a MultiIndex (:issue:`7846`)
175-
175+
- ``Timestamp.tz_localize`` and ``Timestamp.tz_convert`` now raise ``TypeError`` in error cases, rather than ``Exception`` (:issue:`8025`)
176176
- ``Timestamp.__repr__`` displays ``dateutil.tz.tzoffset`` info (:issue:`7907`)
177177
- ``merge``, ``DataFrame.merge``, and ``ordered_merge`` now return the same type
178178
as the ``left`` argument. (:issue:`7737`)

pandas/tseries/tests/test_tslib.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,15 @@ def test_tz(self):
233233
self.assertEqual(conv.nanosecond, 5)
234234
self.assertEqual(conv.hour, 19)
235235

236+
# GH 8025
237+
with tm.assertRaisesRegexp(TypeError, 'Cannot localize tz-aware Timestamp, use '
238+
'tz_convert for conversions'):
239+
Timestamp('2011-01-01' ,tz='US/Eastern').tz_localize('Asia/Tokyo')
240+
241+
with tm.assertRaisesRegexp(TypeError, 'Cannot convert tz-naive Timestamp, use '
242+
'tz_localize to localize'):
243+
Timestamp('2011-01-01').tz_convert('Asia/Tokyo')
244+
236245
def test_tz_localize_roundtrip(self):
237246
for tz in ['UTC', 'Asia/Tokyo', 'US/Eastern', 'dateutil/US/Pacific']:
238247
for t in ['2014-02-01 09:00', '2014-07-08 09:00', '2014-11-01 17:00',
@@ -241,7 +250,7 @@ def test_tz_localize_roundtrip(self):
241250
localized = ts.tz_localize(tz)
242251
self.assertEqual(localized, Timestamp(t, tz=tz))
243252

244-
with tm.assertRaises(Exception):
253+
with tm.assertRaises(TypeError):
245254
localized.tz_localize(tz)
246255

247256
reset = localized.tz_localize(None)

pandas/tslib.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ class Timestamp(_Timestamp):
388388
value = tz_convert_single(self.value, 'UTC', self.tz)
389389
return Timestamp(value, tz=None)
390390
else:
391-
raise Exception('Cannot localize tz-aware Timestamp, use '
391+
raise TypeError('Cannot localize tz-aware Timestamp, use '
392392
'tz_convert for conversions')
393393

394394
def tz_convert(self, tz):
@@ -408,7 +408,7 @@ class Timestamp(_Timestamp):
408408
"""
409409
if self.tzinfo is None:
410410
# tz naive, use tz_localize
411-
raise Exception('Cannot convert tz-naive Timestamp, use '
411+
raise TypeError('Cannot convert tz-naive Timestamp, use '
412412
'tz_localize to localize')
413413
else:
414414
# Same UTC timestamp, different time zone

0 commit comments

Comments
 (0)