Skip to content

Commit

Permalink
BUG: Allow unions of TZ and non-TZ aware indices
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyoung committed Jul 4, 2018
1 parent 2b13605 commit 392bd53
Showing 1 changed file with 43 additions and 24 deletions.
67 changes: 43 additions & 24 deletions pandas/core/indexes/datetimes.py
Expand Up @@ -1094,37 +1094,56 @@ def unique(self, level=None):

def union(self, other):
"""
Specialized union for DatetimeIndex objects. If combine
overlapping ranges with the same DateOffset, will be much
faster than Index.union
Specialized union for `DatetimeIndex` objects.
If overlapping ranges with the same `DateOffset` are combined, this
will be much faster than `Index.union`.
Parameters
----------
other : DatetimeIndex or array-like
Returns
-------
y : Index or DatetimeIndex
index_union : Index or DatetimeIndex
"""

self._assert_can_do_setop(other)

if not isinstance(other, DatetimeIndex):
try:
other = DatetimeIndex(other)
except TypeError:
pass

this, other = self._maybe_utc_convert(other)
this = self

if this._can_fast_union(other):
return this._fast_union(other)
else:
result = Index.union(this, other)
if isinstance(result, DatetimeIndex):
result._tz = timezones.tz_standardize(this.tz)
if (result.freq is None and
(this.freq is not None or other.freq is not None)):
result.freq = to_offset(result.inferred_freq)
return result
try:
this, other = this._maybe_utc_convert(other)

if this._can_fast_union(other):
return this._fast_union(other)
except TypeError as e:
if "join tz-naive with tz-aware" in str(e):
# see gh-21671
#
# If one Index is TZ-aware, and the other
# is not, we just return an Index object.
this = this.astype(object)
other = other.astype(object)
else:
raise

result = Index.union(this, other)

if isinstance(result, DatetimeIndex):
result._tz = timezones.tz_standardize(this.tz)

if (result.freq is None and
(this.freq is not None or other.freq is not None)):
result.freq = to_offset(result.inferred_freq)

return result

def to_perioddelta(self, freq):
"""
Expand Down Expand Up @@ -1193,18 +1212,18 @@ def join(self, other, how='left', level=None, return_indexers=False,

def _maybe_utc_convert(self, other):
this = self

if isinstance(other, DatetimeIndex):
if self.tz is not None:
if other.tz is None:
raise TypeError('Cannot join tz-naive with tz-aware '
'DatetimeIndex')
elif other.tz is not None:
raise TypeError('Cannot join tz-naive with tz-aware '
'DatetimeIndex')
if ((self.tz is not None and other.tz is None) or
(self.tz is None and other.tz is not None)):
raise TypeError("Cannot join tz-naive with "
"tz-aware DatetimeIndex")

if not timezones.tz_compare(self.tz, other.tz):
this = self.tz_convert('UTC')
other = other.tz_convert('UTC')
utc_tz = "UTC"
this = self.tz_convert(utc_tz)
other = other.tz_convert(utc_tz)

return this, other

def _wrap_joined_index(self, joined, other):
Expand Down

0 comments on commit 392bd53

Please sign in to comment.