diff --git a/doc/source/release.rst b/doc/source/release.rst index c6a901252903f..5f7d87ea03f67 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -75,6 +75,7 @@ Bug Fixes - Inconsistent tz parsing Timestamp/to_datetime for current year (:issue:`5958`) - Indexing bugs with reordered indexes (:issue:`6252`, :issue:`6254`) - Bug in ``.xs`` with a Series multiindex (:issue:`6258`, :issue:`5684`) +- Bug in conversion of a string types to a DatetimeIndex with a specified frequency (:issue:`6273`, :issue:`6274`) pandas 0.13.1 ------------- diff --git a/pandas/tests/test_index.py b/pandas/tests/test_index.py index b3dbfe910680a..30e9a68a88122 100644 --- a/pandas/tests/test_index.py +++ b/pandas/tests/test_index.py @@ -146,6 +146,31 @@ def test_constructor_corner(self): # corner case self.assertRaises(TypeError, Index, 0) + def test_constructor_from_series(self): + + expected = DatetimeIndex([Timestamp('20110101'),Timestamp('20120101'),Timestamp('20130101')]) + s = Series([Timestamp('20110101'),Timestamp('20120101'),Timestamp('20130101')]) + result = Index(s) + self.assertTrue(result.equals(expected)) + result = DatetimeIndex(s) + self.assertTrue(result.equals(expected)) + + # GH 6273 + # create from a series, passing a freq + s = Series(pd.to_datetime(['1-1-1990', '2-1-1990', '3-1-1990', '4-1-1990', '5-1-1990'])) + result = DatetimeIndex(s, freq='MS') + expected = DatetimeIndex(['1-1-1990', '2-1-1990', '3-1-1990', '4-1-1990', '5-1-1990'],freq='MS') + self.assertTrue(result.equals(expected)) + + df = pd.DataFrame(np.random.rand(5,3)) + df['date'] = ['1-1-1990', '2-1-1990', '3-1-1990', '4-1-1990', '5-1-1990'] + result = DatetimeIndex(df['date'], freq='MS') + + # GH 6274 + # infer freq of same + result = pd.infer_freq(df['date']) + self.assertEqual(result,'MS') + def test_index_ctor_infer_periodindex(self): from pandas import period_range, PeriodIndex xp = period_range('2012-1-1', freq='M', periods=3) diff --git a/pandas/tseries/index.py b/pandas/tseries/index.py index 5cd3b8a87c974..2762f5fca9a97 100644 --- a/pandas/tseries/index.py +++ b/pandas/tseries/index.py @@ -254,6 +254,11 @@ def __new__(cls, data=None, else: try: subarr = tools.to_datetime(data, box=False) + + # make sure that we have a index/ndarray like (and not a Series) + if isinstance(subarr, ABCSeries): + subarr = subarr.values + except ValueError: # tz aware subarr = tools.to_datetime(data, box=False, utc=True)