Skip to content

Commit ee6db80

Browse files
committed
BUG: properly convert PeriodIndex in to_datetime close pandas-dev#1703
1 parent 1dd2c4f commit ee6db80

File tree

5 files changed

+14
-2
lines changed

5 files changed

+14
-2
lines changed

RELEASE.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pandas 0.8.2
8181
- Fix performance problem in infer_freq with lots of non-unique stamps (#1686)
8282
- Fix handling of PeriodIndex as argument to create MultiIndex (#1705)
8383
- Fix re: unicode MultiIndex level names in Series/DataFrame repr (#1736)
84+
- Handle PeriodIndex in to_datetime instance method (#1703)
8485

8586
pandas 0.8.1
8687
============

pandas/core/index.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,6 @@ def to_datetime(self, dayfirst=False):
150150
parser = lambda x: parse(x, dayfirst=dayfirst)
151151
parsed = lib.try_parse_dates(self.values, parser=parser)
152152
return DatetimeIndex(parsed)
153-
elif isinstance(self, DatetimeIndex):
154-
return self.copy()
155153
else:
156154
return DatetimeIndex(self.values)
157155

pandas/tseries/index.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,9 @@ def __contains__(self, key):
499499
except (KeyError, TypeError):
500500
return False
501501

502+
def to_datetime(self, dayfirst=False):
503+
return self.copy()
504+
502505
def groupby(self, f):
503506
objs = self.asobject
504507
return _algos.groupby_object(objs, f)

pandas/tseries/period.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,9 @@ def asfreq(self, freq=None, how='E'):
691691
result.freq = freq
692692
return result
693693

694+
def to_datetime(self, dayfirst=False):
695+
return self.to_timestamp()
696+
694697
year = _field_accessor('year', 0)
695698
month = _field_accessor('month', 3)
696699
day = _field_accessor('day', 4)

pandas/tseries/tests/test_period.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from numpy.ma.testutils import assert_equal
1414

15+
from pandas import Timestamp
1516
from pandas.tseries.frequencies import MONTHS, DAYS
1617
from pandas.tseries.period import Period, PeriodIndex, period_range
1718
from pandas.tseries.index import DatetimeIndex, date_range
@@ -1780,6 +1781,12 @@ def test_with_multi_index(self):
17801781

17811782
self.assert_(isinstance(s.index.values[0][0], Period))
17821783

1784+
def test_to_datetime_1703(self):
1785+
index = period_range('1/1/2012',periods=4,freq='D')
1786+
1787+
result = index.to_datetime()
1788+
self.assertEquals(result[0], Timestamp('1/1/2012'))
1789+
17831790
def _permute(obj):
17841791
return obj.take(np.random.permutation(len(obj)))
17851792

0 commit comments

Comments
 (0)