|
7 | 7 |
|
8 | 8 | import numpy as np
|
9 | 9 |
|
10 |
| -from pandas._libs import tslibs |
| 10 | +from pandas._libs import algos, tslibs |
11 | 11 | from pandas._libs.tslibs import NaT, Timedelta, Timestamp, iNaT
|
12 | 12 | from pandas._libs.tslibs.fields import get_timedelta_field
|
13 | 13 | from pandas._libs.tslibs.timedeltas import (
|
|
24 | 24 | from pandas.core.dtypes.missing import isna
|
25 | 25 |
|
26 | 26 | from pandas.core import ops
|
27 |
| -from pandas.core.algorithms import checked_add_with_arr |
| 27 | +from pandas.core.algorithms import checked_add_with_arr, unique1d |
28 | 28 | import pandas.core.common as com
|
29 | 29 |
|
30 | 30 | from pandas.tseries.frequencies import to_offset
|
@@ -162,15 +162,29 @@ def _simple_new(cls, values, freq=None, dtype=_TD_DTYPE):
|
162 | 162 | result._freq = freq
|
163 | 163 | return result
|
164 | 164 |
|
165 |
| - def __new__(cls, values, freq=None, dtype=_TD_DTYPE): |
| 165 | + def __new__(cls, values, freq=None, dtype=_TD_DTYPE, copy=False): |
166 | 166 |
|
167 | 167 | freq, freq_infer = dtl.maybe_infer_freq(freq)
|
168 | 168 |
|
169 |
| - values = np.array(values, copy=False) |
170 |
| - if values.dtype == np.object_: |
171 |
| - values = array_to_timedelta64(values) |
| 169 | + values, inferred_freq = sequence_to_td64ns( |
| 170 | + values, copy=copy, unit=None) |
| 171 | + if inferred_freq is not None: |
| 172 | + if freq is not None and freq != inferred_freq: |
| 173 | + raise ValueError('Inferred frequency {inferred} from passed ' |
| 174 | + 'values does not conform to passed frequency ' |
| 175 | + '{passed}' |
| 176 | + .format(inferred=inferred_freq, |
| 177 | + passed=freq.freqstr)) |
| 178 | + elif freq is None: |
| 179 | + freq = inferred_freq |
| 180 | + freq_infer = False |
172 | 181 |
|
173 | 182 | result = cls._simple_new(values, freq=freq)
|
| 183 | + # check that we are matching freqs |
| 184 | + if inferred_freq is None and len(result) > 0: |
| 185 | + if freq is not None and not freq_infer: |
| 186 | + cls._validate_frequency(result, freq) |
| 187 | + |
174 | 188 | if freq_infer:
|
175 | 189 | result.freq = to_offset(result.inferred_freq)
|
176 | 190 |
|
@@ -227,6 +241,21 @@ def _validate_fill_value(self, fill_value):
|
227 | 241 | "Got '{got}'.".format(got=fill_value))
|
228 | 242 | return fill_value
|
229 | 243 |
|
| 244 | + # monotonicity/uniqueness properties are called via frequencies.infer_freq, |
| 245 | + # see GH#23789 |
| 246 | + |
| 247 | + @property |
| 248 | + def _is_monotonic_increasing(self): |
| 249 | + return algos.is_monotonic(self.asi8, timelike=True)[0] |
| 250 | + |
| 251 | + @property |
| 252 | + def _is_monotonic_decreasing(self): |
| 253 | + return algos.is_monotonic(self.asi8, timelike=True)[1] |
| 254 | + |
| 255 | + @property |
| 256 | + def _is_unique(self): |
| 257 | + return len(unique1d(self.asi8)) == len(self) |
| 258 | + |
230 | 259 | # ----------------------------------------------------------------
|
231 | 260 | # Arithmetic Methods
|
232 | 261 |
|
@@ -283,7 +312,7 @@ def _add_datetimelike_scalar(self, other):
|
283 | 312 | result = checked_add_with_arr(i8, other.value,
|
284 | 313 | arr_mask=self._isnan)
|
285 | 314 | result = self._maybe_mask_results(result)
|
286 |
| - return DatetimeArrayMixin(result, tz=other.tz) |
| 315 | + return DatetimeArrayMixin(result, tz=other.tz, freq=self.freq) |
287 | 316 |
|
288 | 317 | def _addsub_offset_array(self, other, op):
|
289 | 318 | # Add or subtract Array-like of DateOffset objects
|
|
0 commit comments