Skip to content

Commit b50fedf

Browse files
committed
special casing in freq_infer
1 parent a4f9733 commit b50fedf

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

pandas/core/arrays/timedeltas.py

+2-18
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import numpy as np
99

10-
from pandas._libs import algos, tslibs
10+
from pandas._libs import tslibs
1111
from pandas._libs.tslibs import NaT, Timedelta, Timestamp, iNaT
1212
from pandas._libs.tslibs.fields import get_timedelta_field
1313
from pandas._libs.tslibs.timedeltas import (
@@ -24,7 +24,7 @@
2424
from pandas.core.dtypes.missing import isna
2525

2626
from pandas.core import ops
27-
from pandas.core.algorithms import checked_add_with_arr, unique1d
27+
from pandas.core.algorithms import checked_add_with_arr
2828
import pandas.core.common as com
2929

3030
from pandas.tseries.frequencies import to_offset
@@ -241,22 +241,6 @@ def _validate_fill_value(self, fill_value):
241241
"Got '{got}'.".format(got=fill_value))
242242
return fill_value
243243

244-
# is_monotonic_increasing, is_monotonic_decreasing, and is_unique
245-
# are needed by `frequencies.infer_freq`, which is called when accessing
246-
# the `inferred_freq` property inside the TimedeltaArray constructor
247-
248-
@property # NB: override with cache_readonly in immutable subclasses
249-
def is_monotonic_increasing(self):
250-
return algos.is_monotonic(self.asi8, timelike=True)[0]
251-
252-
@property # NB: override with cache_readonly in immutable subclasses
253-
def is_monotonic_decreasing(self):
254-
return algos.is_monotonic(self.asi8, timelike=True)[1]
255-
256-
@property # NB: override with cache_readonly in immutable subclasses
257-
def is_unique(self):
258-
return len(unique1d(self.asi8)) == len(self)
259-
260244
# ----------------------------------------------------------------
261245
# Arithmetic Methods
262246

pandas/tseries/frequencies.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy as np
66
from pytz import AmbiguousTimeError
77

8-
from pandas._libs.algos import unique_deltas
8+
from pandas._libs.algos import is_monotonic, unique_deltas
99
from pandas._libs.tslibs import Timedelta, Timestamp
1010
from pandas._libs.tslibs.ccalendar import MONTH_ALIASES, int_to_weekday
1111
from pandas._libs.tslibs.conversion import tz_convert
@@ -295,8 +295,19 @@ def __init__(self, index, warn=True):
295295
if len(index) < 3:
296296
raise ValueError('Need at least 3 dates to infer frequency')
297297

298-
self.is_monotonic = (self.index.is_monotonic_increasing or
299-
self.index.is_monotonic_decreasing)
298+
if not hasattr(index, "is_monotonic_increasing"):
299+
# i.e. TimedeltaArray, not TimedeltaIndex
300+
increasing, decreasing, strict = is_monotonic(index.asi8,
301+
timelike=True)
302+
self.is_monotonic = increasing or decreasing
303+
self.strictly_monotonic = strict
304+
else:
305+
self.is_monotonic = (index.is_monotonic_increasing or
306+
index.is_monotonic_decreasing)
307+
strict = False
308+
if self.is_monotonic and index.is_unique:
309+
strict = True
310+
self.strictly_monotonic = strict
300311

301312
@cache_readonly
302313
def deltas(self):
@@ -323,7 +334,7 @@ def get_freq(self): # noqa:F811
323334
-------
324335
freqstr : str or None
325336
"""
326-
if not self.is_monotonic or not self.index.is_unique:
337+
if not self.strictly_monotonic:
327338
return None
328339

329340
delta = self.deltas[0]

0 commit comments

Comments
 (0)