@@ -1094,37 +1094,56 @@ def unique(self, level=None):
1094
1094
1095
1095
def union (self , other ):
1096
1096
"""
1097
- Specialized union for DatetimeIndex objects. If combine
1098
- overlapping ranges with the same DateOffset, will be much
1099
- faster than Index.union
1097
+ Specialized union for `DatetimeIndex` objects.
1098
+
1099
+ If overlapping ranges with the same `DateOffset` are combined, this
1100
+ will be much faster than `Index.union`.
1100
1101
1101
1102
Parameters
1102
1103
----------
1103
1104
other : DatetimeIndex or array-like
1104
1105
1105
1106
Returns
1106
1107
-------
1107
- y : Index or DatetimeIndex
1108
+ index_union : Index or DatetimeIndex
1108
1109
"""
1110
+
1109
1111
self ._assert_can_do_setop (other )
1112
+
1110
1113
if not isinstance (other , DatetimeIndex ):
1111
1114
try :
1112
1115
other = DatetimeIndex (other )
1113
1116
except TypeError :
1114
1117
pass
1115
1118
1116
- this , other = self . _maybe_utc_convert ( other )
1119
+ this = self
1117
1120
1118
- if this ._can_fast_union (other ):
1119
- return this ._fast_union (other )
1120
- else :
1121
- result = Index .union (this , other )
1122
- if isinstance (result , DatetimeIndex ):
1123
- result ._tz = timezones .tz_standardize (this .tz )
1124
- if (result .freq is None and
1125
- (this .freq is not None or other .freq is not None )):
1126
- result .freq = to_offset (result .inferred_freq )
1127
- return result
1121
+ try :
1122
+ this , other = this ._maybe_utc_convert (other )
1123
+
1124
+ if this ._can_fast_union (other ):
1125
+ return this ._fast_union (other )
1126
+ except TypeError as e :
1127
+ if "join tz-naive with tz-aware" in str (e ):
1128
+ # see gh-21671
1129
+ #
1130
+ # If one Index is TZ-aware, and the other
1131
+ # is not, we just return an Index object.
1132
+ this = this .astype (object )
1133
+ other = other .astype (object )
1134
+ else :
1135
+ raise
1136
+
1137
+ result = Index .union (this , other )
1138
+
1139
+ if isinstance (result , DatetimeIndex ):
1140
+ result ._tz = timezones .tz_standardize (this .tz )
1141
+
1142
+ if (result .freq is None and
1143
+ (this .freq is not None or other .freq is not None )):
1144
+ result .freq = to_offset (result .inferred_freq )
1145
+
1146
+ return result
1128
1147
1129
1148
def to_perioddelta (self , freq ):
1130
1149
"""
@@ -1193,18 +1212,18 @@ def join(self, other, how='left', level=None, return_indexers=False,
1193
1212
1194
1213
def _maybe_utc_convert (self , other ):
1195
1214
this = self
1215
+
1196
1216
if isinstance (other , DatetimeIndex ):
1197
- if self .tz is not None :
1198
- if other .tz is None :
1199
- raise TypeError ('Cannot join tz-naive with tz-aware '
1200
- 'DatetimeIndex' )
1201
- elif other .tz is not None :
1202
- raise TypeError ('Cannot join tz-naive with tz-aware '
1203
- 'DatetimeIndex' )
1217
+ if ((self .tz is not None and other .tz is None ) or
1218
+ (self .tz is None and other .tz is not None )):
1219
+ raise TypeError ("Cannot join tz-naive with "
1220
+ "tz-aware DatetimeIndex" )
1204
1221
1205
1222
if not timezones .tz_compare (self .tz , other .tz ):
1206
- this = self .tz_convert ('UTC' )
1207
- other = other .tz_convert ('UTC' )
1223
+ utc_tz = "UTC"
1224
+ this = self .tz_convert (utc_tz )
1225
+ other = other .tz_convert (utc_tz )
1226
+
1208
1227
return this , other
1209
1228
1210
1229
def _wrap_joined_index (self , joined , other ):
0 commit comments