Skip to content

Commit 4391f3b

Browse files
committed
BUG: tz aware Timestamp field accessors returns local values (#13303)
Add whatsnew
1 parent 9ab57dc commit 4391f3b

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ Bug Fixes
796796
~~~~~~~~~
797797

798798
- Bug in ``Timestamp.replace`` now raises ``TypeError`` when incorrect argument names are given; previously this raised ``ValueError`` (:issue:`15240`)
799+
- Bug in ``Timestamp`` returning UTC based time/date attributes when a timezone was provided (:issue:`13303`)
799800
- Bug in ``Index`` power operations with reversed operands (:issue:`14973`)
800801
- Bug in ``TimedeltaIndex`` addition where overflow was being allowed without error (:issue:`14816`)
801802
- Bug in ``TimedeltaIndex`` raising a ``ValueError`` when boolean indexing with ``loc`` (:issue:`14946`)

pandas/_libs/tslib.pyx

+4-1
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,10 @@ cdef class _Timestamp(datetime):
12331233
return datetime.__sub__(self, other)
12341234

12351235
cpdef _get_field(self, field):
1236-
out = get_date_field(np.array([self.value], dtype=np.int64), field)
1236+
val = self.value
1237+
if self.tz is not None and not _is_utc(self.tz):
1238+
val = tz_convert_single(self.value, 'UTC', self.tz)
1239+
out = get_date_field(np.array([val], dtype=np.int64), field)
12371240
return int(out[0])
12381241

12391242
cpdef _get_start_end_field(self, field):

pandas/tests/scalar/test_timestamp.py

+17
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,23 @@ def check(value, equal):
550550
check(ts.daysinmonth, 31)
551551
check(ts.daysinmonth, 31)
552552

553+
# GH 13303
554+
ts = Timestamp('2014-12-31 23:59:00-05:00', tz='US/Eastern')
555+
check(ts.year, 2014)
556+
check(ts.month, 12)
557+
check(ts.day, 31)
558+
check(ts.hour, 23)
559+
check(ts.minute, 59)
560+
check(ts.second, 0)
561+
self.assertRaises(AttributeError, lambda: ts.millisecond)
562+
check(ts.microsecond, 0)
563+
check(ts.nanosecond, 0)
564+
check(ts.dayofweek, 2)
565+
check(ts.quarter, 4)
566+
check(ts.dayofyear, 365)
567+
check(ts.week, 1)
568+
check(ts.daysinmonth, 31)
569+
553570
def test_nat_fields(self):
554571
# GH 10050
555572
ts = Timestamp('NaT')

0 commit comments

Comments
 (0)