python - Retain original format of datetime64 when converting to list -


>>>df = pd.dataframe(index=pd.date_range(dt.datetime(2016,8,1), dt.datetime(2016,8,9)), columns=['a','b'] ) >>>df.index  datetimeindex(['2016-08-01', '2016-08-02', '2016-08-03', '2016-08-04',            '2016-08-05', '2016-08-06', '2016-08-07', '2016-08-08',            '2016-08-09'],           dtype='datetime64[ns]', freq='d', tz=none) >>>df.index.values.tolist()  [1470009600000000000l,  1470096000000000000l,  1470182400000000000l,  1470268800000000000l,  1470355200000000000l,  1470441600000000000l,  1470528000000000000l,  1470614400000000000l,  1470700800000000000l] 

basically datetime64[ns] format automatically converted long format. there way can keep format operations otherwise need convert if wanted access df content. example

>>>df.loc[df.index.values.tolist()[3]] 

does not work, while

>>>df.loc[df.index.values[3]] 

works.

you can retain original format while converting them list using .date of pandas.datetimeindex.date returns date part of timestamps.

in [14]: df.index.date.tolist() out[14]:  [datetime.date(2016, 8, 1),  datetime.date(2016, 8, 2),  datetime.date(2016, 8, 3),  datetime.date(2016, 8, 4),  datetime.date(2016, 8, 5),  datetime.date(2016, 8, 6),  datetime.date(2016, 8, 7),  datetime.date(2016, 8, 8),  datetime.date(2016, 8, 9)] 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -