Skip to content

Commit 29581d1

Browse files
committed
BUG: Fix subtraction with timezone Series
Fixes unusual bug with timezone Series subtraction in which single element Series objects containing tz-aware objects would return a timedelta of zero, even though it visually could not be the case. The bug was traced to the conversion of the contained timezones to UTC, in which the method call was somehow returning NaT, even though attempts to replicate that behaviour were unsuccessful. This new method call fixes the issue and is in some ways more intuitive given the comment above the conversions. Closes gh-12290.
1 parent 0c09bd1 commit 29581d1

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

pandas/core/ops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,9 @@ def _offset(lvalues, rvalues):
487487

488488
# with tz, convert to UTC
489489
if self.is_datetime64tz_lhs:
490-
lvalues = lvalues.tz_localize(None)
490+
lvalues = lvalues.tz_convert('UTC')
491491
if self.is_datetime64tz_rhs:
492-
rvalues = rvalues.tz_localize(None)
492+
rvalues = rvalues.tz_convert('UTC')
493493

494494
lvalues = lvalues.view(np.int64)
495495
rvalues = rvalues.view(np.int64)

pandas/tests/series/test_operators.py

+15
Original file line numberDiff line numberDiff line change
@@ -1375,3 +1375,18 @@ def test_datetime64_with_index(self):
13751375
df['expected'] = df['date'] - df.index.to_series()
13761376
df['result'] = df['date'] - df.index
13771377
assert_series_equal(df['result'], df['expected'], check_names=False)
1378+
1379+
# See gh-12290
1380+
def test_datetime64_single_non_native(self):
1381+
f = Series([pd.Timestamp('2016-02-08 13:43:14.605000',
1382+
tz='America/Sao_Paulo')])
1383+
s = Series([pd.Timestamp('2016-02-10 13:43:14.605000',
1384+
tz='America/Sao_Paulo')])
1385+
1386+
out = f - s
1387+
expected = Series(pd.Timedelta('-2days'))
1388+
assert_series_equal(out, expected)
1389+
1390+
out = s - f
1391+
expected = Series(pd.Timedelta('2days'))
1392+
assert_series_equal(out, expected)

0 commit comments

Comments
 (0)