|
32 | 32 | from pandas.core import ops
|
33 | 33 |
|
34 | 34 | from pandas.tseries.frequencies import to_offset
|
35 |
| -from pandas.tseries.offsets import Tick, Day, generate_range |
| 35 | +from pandas.tseries.offsets import Tick, generate_range |
36 | 36 |
|
37 | 37 | from pandas.core.arrays import datetimelike as dtl
|
38 | 38 |
|
@@ -239,75 +239,58 @@ def _generate_range(cls, start, end, periods, freq, tz=None,
|
239 | 239 | start, end, _normalized = _maybe_normalize_endpoints(start, end,
|
240 | 240 | normalize)
|
241 | 241 |
|
242 |
| - tz, inferred_tz = _infer_tz_from_endpoints(start, end, tz) |
| 242 | + tz, _ = _infer_tz_from_endpoints(start, end, tz) |
243 | 243 |
|
244 |
| - if hasattr(freq, 'delta') and freq != Day(): |
245 |
| - # sub-Day Tick |
246 |
| - if inferred_tz is None and tz is not None: |
247 |
| - # naive dates |
248 |
| - if start is not None and start.tz is None: |
249 |
| - start = start.tz_localize(tz, ambiguous=False) |
| 244 | + # If we have a Timedelta-like frequency (Tick) make sure tz |
| 245 | + # is set before generating the range. For relative frequencies, |
| 246 | + # generate the range with naive dates. |
| 247 | + localize_args = {'tz': None} |
| 248 | + if isinstance(freq, Tick): |
| 249 | + localize_args = {'tz': tz, 'ambiguous': False} |
| 250 | + if tz is not None: |
| 251 | + # Localize the start and end arguments |
| 252 | + if start is not None and start.tz is None: |
| 253 | + start = start.tz_localize(**localize_args) |
250 | 254 |
|
251 |
| - if end is not None and end.tz is None: |
252 |
| - end = end.tz_localize(tz, ambiguous=False) |
| 255 | + if end is not None and end.tz is None: |
| 256 | + end = end.tz_localize(**localize_args) |
253 | 257 |
|
254 |
| - if start and end: |
255 |
| - if start.tz is None and end.tz is not None: |
256 |
| - start = start.tz_localize(end.tz, ambiguous=False) |
| 258 | + if start and end: |
| 259 | + # Make sure start and end have the same tz |
| 260 | + if start.tz is None and end.tz is not None: |
| 261 | + start = start.tz_localize(**localize_args) |
257 | 262 |
|
258 |
| - if end.tz is None and start.tz is not None: |
259 |
| - end = end.tz_localize(start.tz, ambiguous=False) |
| 263 | + if end.tz is None and start.tz is not None: |
| 264 | + end = end.tz_localize(**localize_args) |
260 | 265 |
|
| 266 | + if freq is not None: |
261 | 267 | if cls._use_cached_range(freq, _normalized, start, end):
|
| 268 | + # Currently always False; never hit |
262 | 269 | index = cls._cached_range(start, end, periods=periods,
|
263 | 270 | freq=freq)
|
264 | 271 | else:
|
265 | 272 | index = _generate_regular_range(cls, start, end, periods, freq)
|
266 | 273 |
|
267 |
| - else: |
| 274 | + # TODO: Is this ever hit? |
| 275 | + if tz is not None and getattr(index, 'tz', None) is None: |
| 276 | + arr = conversion.tz_localize_to_utc( |
| 277 | + ensure_int64(index.values), |
| 278 | + tz, ambiguous=ambiguous) |
268 | 279 |
|
269 |
| - if tz is not None: |
270 |
| - # naive dates |
271 |
| - if start is not None and start.tz is not None: |
272 |
| - start = start.replace(tzinfo=None) |
273 |
| - |
274 |
| - if end is not None and end.tz is not None: |
275 |
| - end = end.replace(tzinfo=None) |
276 |
| - |
277 |
| - if start and end: |
278 |
| - if start.tz is None and end.tz is not None: |
279 |
| - end = end.replace(tzinfo=None) |
280 |
| - |
281 |
| - if end.tz is None and start.tz is not None: |
282 |
| - start = start.replace(tzinfo=None) |
283 |
| - |
284 |
| - if freq is not None: |
285 |
| - if cls._use_cached_range(freq, _normalized, start, end): |
286 |
| - index = cls._cached_range(start, end, periods=periods, |
287 |
| - freq=freq) |
288 |
| - else: |
289 |
| - index = _generate_regular_range(cls, start, end, |
290 |
| - periods, freq) |
291 |
| - |
292 |
| - if tz is not None and getattr(index, 'tz', None) is None: |
293 |
| - arr = conversion.tz_localize_to_utc( |
294 |
| - ensure_int64(index.values), |
295 |
| - tz, ambiguous=ambiguous) |
296 |
| - |
297 |
| - index = cls(arr) |
298 |
| - |
299 |
| - # index is localized datetime64 array -> have to convert |
300 |
| - # start/end as well to compare |
301 |
| - if start is not None: |
302 |
| - start = start.tz_localize(tz).asm8 |
303 |
| - if end is not None: |
304 |
| - end = end.tz_localize(tz).asm8 |
305 |
| - else: |
306 |
| - # Create a linearly spaced date_range in local time |
307 |
| - start = start.tz_localize(tz) |
308 |
| - end = end.tz_localize(tz) |
309 |
| - arr = np.linspace(start.value, end.value, periods) |
310 |
| - index = cls._simple_new(arr.astype('M8[ns]'), freq=None, tz=tz) |
| 280 | + index = cls(arr) |
| 281 | + |
| 282 | + # index is localized datetime64 array -> have to convert |
| 283 | + # start/end as well to compare |
| 284 | + if start is not None: |
| 285 | + start = start.tz_localize(tz).asm8 |
| 286 | + if end is not None: |
| 287 | + end = end.tz_localize(tz).asm8 |
| 288 | + else: |
| 289 | + # Create a linearly spaced date_range in local time |
| 290 | + start = start.tz_localize(tz) |
| 291 | + end = end.tz_localize(tz) |
| 292 | + arr = np.linspace(start.value, end.value, periods) |
| 293 | + index = cls._simple_new(arr.astype('M8[ns]'), freq=None, tz=tz) |
311 | 294 |
|
312 | 295 | if not left_closed and len(index) and index[0] == start:
|
313 | 296 | index = index[1:]
|
@@ -1255,12 +1238,10 @@ def _generate_regular_range(cls, start, end, periods, freq):
|
1255 | 1238 | data = cls._simple_new(data.view(_NS_DTYPE), None, tz=tz)
|
1256 | 1239 | else:
|
1257 | 1240 | tz = None
|
| 1241 | + # Start and end should have the same timestamp by this point |
1258 | 1242 | if isinstance(start, Timestamp):
|
1259 | 1243 | tz = start.tz
|
1260 | 1244 |
|
1261 |
| - if isinstance(end, Timestamp): |
1262 |
| - tz = end.tz |
1263 |
| - |
1264 | 1245 | xdr = generate_range(start=start, end=end,
|
1265 | 1246 | periods=periods, offset=freq)
|
1266 | 1247 |
|
|
0 commit comments