如何将Pandas DatetimeIndex相应地转换为字符串

时间:2012-10-11 07:41:37

标签: python pandas

我将DatetimeIndex定义如下。

>>> date_rng = pandas.date_range('20060101','20121231',freq='D')

>>> type(date_rng)
<class 'pandas.tseries.index.DatetimeIndex'>
>>> date_rng[0]
<Timestamp: 2006-01-01 00:00:00>

'date_rng'中的每个元素都是'Timestamp',如何将其转换为如下字符串系列?

>>> pandas.Series(['2006-01-01','2006-01-02','2006-01-03'])
0    2006-01-01
1    2006-01-02
2    2006-01-03

1 个答案:

答案 0 :(得分:15)

>>> date_rng = pd.date_range('20060101','20060105',freq='D')
>>> pd.Series(date_rng.format())

0    2006-01-01
1    2006-01-02
2    2006-01-03
3    2006-01-04
4    2006-01-05