Skip to content

Commit 2a50da8

Browse files
committed
COMPAT: infer_freq compat on passing an Index of strings (GH6463)
1 parent 7b46c70 commit 2a50da8

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

doc/source/release.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ API Changes
8181
- ``microsecond,nanosecond,qyear``
8282
- ``min(),max()``
8383
- ``pd.infer_freq()``
84-
- ``pd.infer_freq()`` will now raise a ``TypeError`` if given an invalid ``Series/Index`` type (:issue:`6407`)
84+
- ``pd.infer_freq()`` will now raise a ``TypeError`` if given an invalid ``Series/Index``
85+
type (:issue:`6407`, :issue:`6463`)
8586

8687
- Local variable usage has changed in
8788
:func:`pandas.eval`/:meth:`DataFrame.eval`/:meth:`DataFrame.query`

pandas/tseries/frequencies.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,11 @@ def infer_freq(index, warn=True):
655655
if isinstance(index, pd.PeriodIndex):
656656
raise TypeError("PeriodIndex given. Check the `freq` attribute "
657657
"instead of using infer_freq.")
658-
if not isinstance(index, pd.DatetimeIndex) and isinstance(index, pd.Index):
659-
raise TypeError("cannot infer freq from a non-convertible index type {0}".format(type(index)))
658+
if isinstance(index, pd.Index) and not isinstance(index, pd.DatetimeIndex):
659+
if isinstance(index, (pd.Int64Index, pd.Float64Index)):
660+
raise TypeError("cannot infer freq from a non-convertible index type {0}".format(type(index)))
661+
index = index.values
662+
660663
index = pd.DatetimeIndex(index)
661664
inferer = _FrequencyInferer(index, warn=warn)
662665
return inferer.get_freq()

pandas/tseries/tests/test_frequencies.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,20 @@ def test_invalid_index_types(self):
274274
# test all index types
275275
for i in [ tm.makeIntIndex(10),
276276
tm.makeFloatIndex(10),
277-
tm.makeStringIndex(10),
278-
tm.makeUnicodeIndex(10),
279277
tm.makePeriodIndex(10) ]:
280278
self.assertRaises(TypeError, lambda : infer_freq(i))
281279

280+
for i in [ tm.makeStringIndex(10),
281+
tm.makeUnicodeIndex(10) ]:
282+
self.assertRaises(ValueError, lambda : infer_freq(i))
283+
284+
def test_string_datetimelike_compat(self):
285+
286+
# GH 6463
287+
expected = infer_freq(['2004-01', '2004-02', '2004-03', '2004-04'])
288+
result = infer_freq(Index(['2004-01', '2004-02', '2004-03', '2004-04']))
289+
self.assertEqual(result,expected)
290+
282291
def test_series(self):
283292

284293
# GH6407

0 commit comments

Comments
 (0)