Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a Pandas DatetimeIndex to string accordingly

Tags:

python

pandas

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
like image 222
bigbug Avatar asked Oct 11 '12 07:10

bigbug


People also ask

How do I change DatetimeIndex?

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.

How do you convert a DataFrame column to a string object in Python?

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.


1 Answers

>>> 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
like image 57
eumiro Avatar answered Oct 23 '22 00:10

eumiro