Skip to content

Commit 5433a71

Browse files
committed
privatize
1 parent 5de2d42 commit 5433a71

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

pandas/core/arrays/timedeltas.py

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

88
import numpy as np
99

10-
from pandas._libs import tslibs
10+
from pandas._libs import algos, 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
27+
from pandas.core.algorithms import checked_add_with_arr, unique1d
2828
import pandas.core.common as com
2929

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

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+
244259
# ----------------------------------------------------------------
245260
# Arithmetic Methods
246261

pandas/core/indexes/timedeltas.py

+5
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ def _format_native_types(self, na_rep=u'NaT', date_format=None, **kwargs):
242242

243243
total_seconds = wrap_array_method(TimedeltaArray.total_seconds, True)
244244

245+
# Compat for frequency inference, see GH#23789
246+
_is_monotonic_increasing = Index.is_monotonic_increasing
247+
_is_monotonic_decreasing = Index.is_monotonic_decreasing
248+
_is_unique = Index.is_unique
249+
245250
# -------------------------------------------------------------------
246251

247252
@Appender(_index_shared_docs['astype'])

pandas/tseries/frequencies.py

+4-15
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 is_monotonic, unique_deltas
8+
from pandas._libs.algos import 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,19 +295,8 @@ def __init__(self, index, warn=True):
295295
if len(index) < 3:
296296
raise ValueError('Need at least 3 dates to infer frequency')
297297

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
298+
self.is_monotonic = (self.index._is_monotonic_increasing or
299+
self.index._is_monotonic_decreasing)
311300

312301
@cache_readonly
313302
def deltas(self):
@@ -334,7 +323,7 @@ def get_freq(self): # noqa:F811
334323
-------
335324
freqstr : str or None
336325
"""
337-
if not self.strictly_monotonic:
326+
if not self.is_monotonic or not self.index._is_unique:
338327
return None
339328

340329
delta = self.deltas[0]

0 commit comments

Comments
 (0)