pandas datetime64 转string

https://stackoverflow.com/questions/50449453/pandas-datetime64-to-string

You can just cast the dtype first using astype:

In[29]:
df = pd.DataFrame({'date':pd.to_datetime(['2018-05-20 10:20:00','2018-05-20 10:30:00'])})
df

Out[29]: 
                 date
0 2018-05-20 10:20:00
1 2018-05-20 10:30:00

In[30]:
df['date'].astype(str).tolist()

Out[30]: ['2018-05-20 10:20:00', '2018-05-20 10:30:00']

What you did just converted the array to a list of the original dtype:

In[31]:
df['date'].tolist()

Out[31]: [Timestamp('2018-05-20 10:20:00'), Timestamp('2018-05-20 10:30:00')]

The more formal method is to call dt.strftime to convert to string using the passed in format:

In[33]:
df['date'].dt.strftime('%Y-%m-%d %H:%M:%S').tolist()

Out[33]: ['2018-05-20 10:20:00', '2018-05-20 10:30:00']
发布了13 篇原创文章 · 获赞 7 · 访问量 5993

猜你喜欢

转载自blog.csdn.net/kangseung/article/details/89380662
今日推荐