简体   繁体   中英

How to convert python dataframe timestamp to datetime format

I have a dataframe with date information in one column.

在此处输入图片说明

The date visually appears in the dataframe in this format: 2019-11-24

but when you print the type it shows up as:

Timestamp('2019-11-24 00:00:00')

I'd like to convert each value in the dataframe to a format like this:

24-Nov

or

7-Nov

for single digit days.

I've tried using various datetime and strptime commands to convert but I am getting errors.

Here's a way to do:

df = pd.DataFrame({'date': ["2014-10-23","2016-09-08"]}) 

df['date_new'] = pd.to_datetime(df['date'])

df['date_new'] = df['date_new'].dt.strftime("%d-%b")

         date      date_new
0  2014-10-23      23-Oct
1  2016-09-08      08-Sept

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM