|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 | from datetime import timedelta
|
| 3 | +import warnings |
3 | 4 |
|
4 | 5 | import numpy as np
|
5 | 6 |
|
6 | 7 | from pandas._libs import tslibs
|
7 |
| -from pandas._libs.tslibs import Timedelta, Timestamp, NaT |
| 8 | +from pandas._libs.tslibs import Timedelta, Timestamp, NaT, iNaT |
8 | 9 | from pandas._libs.tslibs.fields import get_timedelta_field
|
9 |
| -from pandas._libs.tslibs.timedeltas import array_to_timedelta64 |
| 10 | +from pandas._libs.tslibs.timedeltas import ( |
| 11 | + array_to_timedelta64, parse_timedelta_unit) |
10 | 12 |
|
11 | 13 | from pandas import compat
|
12 | 14 |
|
13 | 15 | from pandas.core.dtypes.common import (
|
14 |
| - _TD_DTYPE, is_list_like) |
15 |
| -from pandas.core.dtypes.generic import ABCSeries |
| 16 | + _TD_DTYPE, |
| 17 | + is_object_dtype, |
| 18 | + is_string_dtype, |
| 19 | + is_float_dtype, |
| 20 | + is_integer_dtype, |
| 21 | + is_timedelta64_dtype, |
| 22 | + is_datetime64_dtype, |
| 23 | + is_list_like, |
| 24 | + ensure_int64) |
| 25 | +from pandas.core.dtypes.generic import ABCSeries, ABCTimedeltaIndex |
16 | 26 | from pandas.core.dtypes.missing import isna
|
17 | 27 |
|
18 | 28 | import pandas.core.common as com
|
@@ -139,9 +149,7 @@ def __new__(cls, values, freq=None):
|
139 | 149 |
|
140 | 150 | result = cls._simple_new(values, freq=freq)
|
141 | 151 | if freq_infer:
|
142 |
| - inferred = result.inferred_freq |
143 |
| - if inferred: |
144 |
| - result.freq = to_offset(inferred) |
| 152 | + result.freq = to_offset(result.inferred_freq) |
145 | 153 |
|
146 | 154 | return result
|
147 | 155 |
|
@@ -397,6 +405,163 @@ def f(x):
|
397 | 405 | # ---------------------------------------------------------------------
|
398 | 406 | # Constructor Helpers
|
399 | 407 |
|
| 408 | +def sequence_to_td64ns(data, copy=False, unit="ns", errors="raise"): |
| 409 | + """ |
| 410 | + Parameters |
| 411 | + ---------- |
| 412 | + array : list-like |
| 413 | + copy : bool, default False |
| 414 | + unit : str, default "ns" |
| 415 | + errors : {"raise", "coerce", "ignore"}, default "raise" |
| 416 | +
|
| 417 | + Returns |
| 418 | + ------- |
| 419 | + ndarray[timedelta64[ns]] |
| 420 | + inferred_freq : Tick or None |
| 421 | +
|
| 422 | + Raises |
| 423 | + ------ |
| 424 | + ValueError : data cannot be converted to timedelta64[ns] |
| 425 | +
|
| 426 | + Notes |
| 427 | + ----- |
| 428 | + Unlike `pandas.to_timedelta`, if setting `errors=ignore` will not cause |
| 429 | + errors to be ignored; they are caught and subsequently ignored at a |
| 430 | + higher level. |
| 431 | + """ |
| 432 | + inferred_freq = None |
| 433 | + unit = parse_timedelta_unit(unit) |
| 434 | + |
| 435 | + # Unwrap whatever we have into a np.ndarray |
| 436 | + if not hasattr(data, 'dtype'): |
| 437 | + # e.g. list, tuple |
| 438 | + if np.ndim(data) == 0: |
| 439 | + # i.e. generator |
| 440 | + data = list(data) |
| 441 | + data = np.array(data, copy=False) |
| 442 | + elif isinstance(data, ABCSeries): |
| 443 | + data = data._values |
| 444 | + elif isinstance(data, (ABCTimedeltaIndex, TimedeltaArrayMixin)): |
| 445 | + inferred_freq = data.freq |
| 446 | + data = data._data |
| 447 | + |
| 448 | + # Convert whatever we have into timedelta64[ns] dtype |
| 449 | + if is_object_dtype(data) or is_string_dtype(data): |
| 450 | + # no need to make a copy, need to convert if string-dtyped |
| 451 | + data = objects_to_td64ns(data, unit=unit, errors=errors) |
| 452 | + copy = False |
| 453 | + |
| 454 | + elif is_integer_dtype(data): |
| 455 | + # treat as multiples of the given unit |
| 456 | + data, copy_made = ints_to_td64ns(data, unit=unit) |
| 457 | + copy = copy and not copy_made |
| 458 | + |
| 459 | + elif is_float_dtype(data): |
| 460 | + # treat as multiples of the given unit. If after converting to nanos, |
| 461 | + # there are fractional components left, these are truncated |
| 462 | + # (i.e. NOT rounded) |
| 463 | + mask = np.isnan(data) |
| 464 | + coeff = np.timedelta64(1, unit) / np.timedelta64(1, 'ns') |
| 465 | + data = (coeff * data).astype(np.int64).view('timedelta64[ns]') |
| 466 | + data[mask] = iNaT |
| 467 | + copy = False |
| 468 | + |
| 469 | + elif is_timedelta64_dtype(data): |
| 470 | + if data.dtype != _TD_DTYPE: |
| 471 | + # non-nano unit |
| 472 | + # TODO: watch out for overflows |
| 473 | + data = data.astype(_TD_DTYPE) |
| 474 | + copy = False |
| 475 | + |
| 476 | + elif is_datetime64_dtype(data): |
| 477 | + # GH#23539 |
| 478 | + warnings.warn("Passing datetime64-dtype data to TimedeltaIndex is " |
| 479 | + "deprecated, will raise a TypeError in a future " |
| 480 | + "version", |
| 481 | + FutureWarning, stacklevel=3) |
| 482 | + data = ensure_int64(data).view(_TD_DTYPE) |
| 483 | + |
| 484 | + else: |
| 485 | + raise TypeError("dtype {dtype} cannot be converted to timedelta64[ns]" |
| 486 | + .format(dtype=data.dtype)) |
| 487 | + |
| 488 | + data = np.array(data, copy=copy) |
| 489 | + assert data.dtype == 'm8[ns]', data |
| 490 | + return data, inferred_freq |
| 491 | + |
| 492 | + |
| 493 | +def ints_to_td64ns(data, unit="ns"): |
| 494 | + """ |
| 495 | + Convert an ndarray with integer-dtype to timedelta64[ns] dtype, treating |
| 496 | + the integers as multiples of the given timedelta unit. |
| 497 | +
|
| 498 | + Parameters |
| 499 | + ---------- |
| 500 | + data : np.ndarray with integer-dtype |
| 501 | + unit : str, default "ns" |
| 502 | +
|
| 503 | + Returns |
| 504 | + ------- |
| 505 | + ndarray[timedelta64[ns]] |
| 506 | + bool : whether a copy was made |
| 507 | + """ |
| 508 | + copy_made = False |
| 509 | + unit = unit if unit is not None else "ns" |
| 510 | + |
| 511 | + if data.dtype != np.int64: |
| 512 | + # converting to int64 makes a copy, so we can avoid |
| 513 | + # re-copying later |
| 514 | + data = data.astype(np.int64) |
| 515 | + copy_made = True |
| 516 | + |
| 517 | + if unit != "ns": |
| 518 | + dtype_str = "timedelta64[{unit}]".format(unit=unit) |
| 519 | + data = data.view(dtype_str) |
| 520 | + |
| 521 | + # TODO: watch out for overflows when converting from lower-resolution |
| 522 | + data = data.astype("timedelta64[ns]") |
| 523 | + # the astype conversion makes a copy, so we can avoid re-copying later |
| 524 | + copy_made = True |
| 525 | + |
| 526 | + else: |
| 527 | + data = data.view("timedelta64[ns]") |
| 528 | + |
| 529 | + return data, copy_made |
| 530 | + |
| 531 | + |
| 532 | +def objects_to_td64ns(data, unit="ns", errors="raise"): |
| 533 | + """ |
| 534 | + Convert a object-dtyped or string-dtyped array into an |
| 535 | + timedelta64[ns]-dtyped array. |
| 536 | +
|
| 537 | + Parameters |
| 538 | + ---------- |
| 539 | + data : ndarray or Index |
| 540 | + unit : str, default "ns" |
| 541 | + errors : {"raise", "coerce", "ignore"}, default "raise" |
| 542 | +
|
| 543 | + Returns |
| 544 | + ------- |
| 545 | + ndarray[timedelta64[ns]] |
| 546 | +
|
| 547 | + Raises |
| 548 | + ------ |
| 549 | + ValueError : data cannot be converted to timedelta64[ns] |
| 550 | +
|
| 551 | + Notes |
| 552 | + ----- |
| 553 | + Unlike `pandas.to_timedelta`, if setting `errors=ignore` will not cause |
| 554 | + errors to be ignored; they are caught and subsequently ignored at a |
| 555 | + higher level. |
| 556 | + """ |
| 557 | + # coerce Index to np.ndarray, converting string-dtype if necessary |
| 558 | + values = np.array(data, dtype=np.object_, copy=False) |
| 559 | + |
| 560 | + result = array_to_timedelta64(values, |
| 561 | + unit=unit, errors=errors) |
| 562 | + return result.view('timedelta64[ns]') |
| 563 | + |
| 564 | + |
400 | 565 | def _generate_regular_range(start, end, periods, offset):
|
401 | 566 | stride = offset.nanos
|
402 | 567 | if periods is None:
|
|
0 commit comments