Skip to content

BUG: fix alignment in series ops (GH14227) #14230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
is_numeric_dtype,
is_datetime64_dtype,
is_timedelta64_dtype,
is_datetime64tz_dtype,
is_list_like,
is_dict_like,
is_re_compilable)
Expand Down Expand Up @@ -4438,13 +4439,23 @@ def _align_frame(self, other, join='outer', axis=None, level=None,
left = left.fillna(axis=fill_axis, method=method, limit=limit)
right = right.fillna(axis=fill_axis, method=method, limit=limit)

# if DatetimeIndex have different tz, convert to UTC
if is_datetime64tz_dtype(left.index):
if left.index.tz != right.index.tz:
if join_index is not None:
left.index = join_index
right.index = join_index

return left.__finalize__(self), right.__finalize__(other)

def _align_series(self, other, join='outer', axis=None, level=None,
copy=True, fill_value=None, method=None, limit=None,
fill_axis=0):

is_series = isinstance(self, ABCSeries)

# series/series compat, other must always be a Series
if isinstance(self, ABCSeries):
if is_series:
if axis:
raise ValueError('cannot align series to a series other than '
'axis 0')
Expand Down Expand Up @@ -4503,6 +4514,15 @@ def _align_series(self, other, join='outer', axis=None, level=None,
left = left.fillna(fill_value, method=method, limit=limit,
axis=fill_axis)
right = right.fillna(fill_value, method=method, limit=limit)

# if DatetimeIndex have different tz, convert to UTC
if is_series or (not is_series and axis == 0):
if is_datetime64tz_dtype(left.index):
if left.index.tz != right.index.tz:
if join_index is not None:
left.index = join_index
right.index = join_index

return left.__finalize__(self), right.__finalize__(other)

def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
Expand Down
6 changes: 0 additions & 6 deletions pandas/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,12 +622,6 @@ def _align_method_SERIES(left, right, align_asobject=False):

left, right = left.align(right, copy=False)

index, lidx, ridx = left.index.join(right.index, how='outer',
return_indexers=True)
# if DatetimeIndex have different tz, convert to UTC
left.index = index
right.index = index

return left, right


Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/series/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1810,3 +1810,11 @@ def test_dti_tz_convert_to_utc(self):

res = Series([1, 2], index=idx1) + Series([1, 1], index=idx2)
assert_series_equal(res, Series([np.nan, 3, np.nan], index=base))

def test_op_duplicate_index(self):
# GH14227
s1 = Series([1, 2], index=[1, 1])
s2 = Series([10, 10], index=[1, 2])
result = s1 + s2
expected = pd.Series([11, 12, np.nan], index=[1, 1, 2])
assert_series_equal(result, expected)
22 changes: 22 additions & 0 deletions pandas/tseries/tests/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,28 @@ def test_align_aware(self):
self.assertEqual(df1.index.tz, new1.index.tz)
self.assertEqual(df2.index.tz, new2.index.tz)

# # different timezones convert to UTC

# frame
df1_central = df1.tz_convert('US/Central')
new1, new2 = df1.align(df1_central)
self.assertEqual(new1.index.tz, pytz.UTC)
self.assertEqual(new2.index.tz, pytz.UTC)

# series
new1, new2 = df1[0].align(df1_central[0])
self.assertEqual(new1.index.tz, pytz.UTC)
self.assertEqual(new2.index.tz, pytz.UTC)

# combination
new1, new2 = df1.align(df1_central[0], axis=0)
self.assertEqual(new1.index.tz, pytz.UTC)
self.assertEqual(new2.index.tz, pytz.UTC)

df1[0].align(df1_central, axis=0)
self.assertEqual(new1.index.tz, pytz.UTC)
self.assertEqual(new2.index.tz, pytz.UTC)

def test_append_aware(self):
rng1 = date_range('1/1/2011 01:00', periods=1, freq='H',
tz='US/Eastern')
Expand Down