Skip to content

Commit fb94d95

Browse files
committed
Merge pull request #6275 from jreback/series_to_index
BUG: Bug in conversion of a string types to a DatetimeIndex with a specifed frequency (GH6273)
2 parents e76ec60 + 7ab99ca commit fb94d95

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ Bug Fixes
7575
- Inconsistent tz parsing Timestamp/to_datetime for current year (:issue:`5958`)
7676
- Indexing bugs with reordered indexes (:issue:`6252`, :issue:`6254`)
7777
- Bug in ``.xs`` with a Series multiindex (:issue:`6258`, :issue:`5684`)
78+
- Bug in conversion of a string types to a DatetimeIndex with a specified frequency (:issue:`6273`, :issue:`6274`)
7879

7980
pandas 0.13.1
8081
-------------

pandas/tests/test_index.py

+25
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,31 @@ def test_constructor_corner(self):
146146
# corner case
147147
self.assertRaises(TypeError, Index, 0)
148148

149+
def test_constructor_from_series(self):
150+
151+
expected = DatetimeIndex([Timestamp('20110101'),Timestamp('20120101'),Timestamp('20130101')])
152+
s = Series([Timestamp('20110101'),Timestamp('20120101'),Timestamp('20130101')])
153+
result = Index(s)
154+
self.assertTrue(result.equals(expected))
155+
result = DatetimeIndex(s)
156+
self.assertTrue(result.equals(expected))
157+
158+
# GH 6273
159+
# create from a series, passing a freq
160+
s = Series(pd.to_datetime(['1-1-1990', '2-1-1990', '3-1-1990', '4-1-1990', '5-1-1990']))
161+
result = DatetimeIndex(s, freq='MS')
162+
expected = DatetimeIndex(['1-1-1990', '2-1-1990', '3-1-1990', '4-1-1990', '5-1-1990'],freq='MS')
163+
self.assertTrue(result.equals(expected))
164+
165+
df = pd.DataFrame(np.random.rand(5,3))
166+
df['date'] = ['1-1-1990', '2-1-1990', '3-1-1990', '4-1-1990', '5-1-1990']
167+
result = DatetimeIndex(df['date'], freq='MS')
168+
169+
# GH 6274
170+
# infer freq of same
171+
result = pd.infer_freq(df['date'])
172+
self.assertEqual(result,'MS')
173+
149174
def test_index_ctor_infer_periodindex(self):
150175
from pandas import period_range, PeriodIndex
151176
xp = period_range('2012-1-1', freq='M', periods=3)

pandas/tseries/index.py

+5
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ def __new__(cls, data=None,
254254
else:
255255
try:
256256
subarr = tools.to_datetime(data, box=False)
257+
258+
# make sure that we have a index/ndarray like (and not a Series)
259+
if isinstance(subarr, ABCSeries):
260+
subarr = subarr.values
261+
257262
except ValueError:
258263
# tz aware
259264
subarr = tools.to_datetime(data, box=False, utc=True)

0 commit comments

Comments
 (0)