ENH: Convert datetimelike index to strings with astype(str) · Issue #10442 · pandas-dev/pandas (original) (raw)

xref #10110

- [ ] Add Series.dt.format() accessor to call .format() (#10110 does this with .strftime)

Recently the ability to convert a datetimelike series to strings with astype(str) was introduced (#9758 by @evanpw):

In [27]: pd.Series(pd.date_range('2012-01-01', periods=3))
Out[27]:
0   2012-01-01
1   2012-01-02
2   2012-01-03
dtype: datetime64[ns]

In [28]: pd.Series(pd.date_range('2012-01-01', periods=3)).astype(str)
Out[28]:
0    2012-01-01T01:00:00.000000000+0100
1    2012-01-02T01:00:00.000000000+0100
2    2012-01-03T01:00:00.000000000+0100
dtype: object

it would be nice if this also possible on a index (now you still get the same error as previously for a series):

In [29]: pd.date_range('2012-01-01', periods=3).astype(str)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-29-68a28f79c8e9> in <module>()
----> 1 pd.date_range('2012-01-01', periods=3).astype(str)

C:\Users\vdbosscj\Scipy\pandas-joris\pandas\tseries\index.py in astype(self, dty
pe)
    692             return self.asi8.copy()
    693         else:  # pragma: no cover
--> 694             raise ValueError('Cannot cast DatetimeIndex to dtype %s' % d
type)
    695
    696     def _get_time_micros(self):

ValueError: Cannot cast DatetimeIndex to dtype |S0