Skip to content

Commit

Permalink
ENH: add to_datetime method to Index, close #208
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Apr 13, 2012
1 parent a89d991 commit 51250cc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ def astype(self, dtype):
return Index(self.values.astype(dtype), name=self.name,
dtype=dtype)

def to_datetime(self):
"""
For an Index containing strings or datetime.datetime objects, attempt
conversion to DatetimeIndex
"""
return DatetimeIndex(self.values)

@property
def dtype(self):
return self.values.dtype
Expand Down Expand Up @@ -1397,7 +1404,7 @@ def _mpl_repr(self):
def __repr__(self):
if self.offset is not None:
output = str(self.__class__) + '\n'
output += 'offset: %s, timezone: %s\n' % (self.offset, self.tz)
output += 'freq: %s, timezone: %s\n' % (self.offset, self.tz)
if len(self) > 0:
output += '[%s, ..., %s]\n' % (self[0], self[-1])
output += 'length: %d' % len(self)
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,12 @@ def test_string_na_nat_conversion(self):
assert_series_equal(dresult, expected)
self.assertEquals(dresult.name, 'foo')

def test_index_to_datetime(self):
idx = Index(['1/1/2000', '1/2/2000', '1/3/2000'])

result = idx.to_datetime()
expected = DatetimeIndex(datetools.to_datetime(idx.values))
self.assert_(result.equals(expected))

def _skip_if_no_pytz():
try:
Expand Down

0 comments on commit 51250cc

Please sign in to comment.