I define a DatetimeIndex as below.
>>> 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 ?
>>> pandas.Series(['2006-01-01','2006-01-02','2006-01-03'])
0 2006-01-01
1 2006-01-02
2 2006-01-03
To get a new datetime column and set it as DatetimeIndex we can use the format parameter of the to_datetime function followed by the set_index function. The output above shows our DataFrame with DatetimeIndex. That's it! Now we have an adapted pandas DataFrame with DatetimeIndex.
Use pandas DataFrame. astype() function to convert a column from int to string, you can apply this on a specific column or on an entire DataFrame.
>>> 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With