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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12834568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:27:00  来源:igfitidea点击:

How to convert a Pandas DatetimeIndex to string accordingly

pythonpandas

提问by bigbug

I define a DatetimeIndex as below.

我定义了一个 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>

And each element in the 'date_rng' is 'Timestamp', How can I convert it to a string series like below ?

'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

回答by eumiro

>>> 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