Chubaka
Chubaka

Reputation: 3145

Pandas: convert series of time YYYY-MM-DD hh:mm:ss.0 keeping the YYYY-MM-DD format only

Pandas: convert series of time YYYY-MM-DD hh:mm:ss.0 keeping the YYYY-MM-DD format only

python 3.6, pandas 0.19.0

     timestamp
0    2013-01-14 21:19:42.0
1    2013-01-16 09:04:37.0
2    2013-03-20 12:50:49.0
3    2013-01-03 17:02:53.0
4    2013-04-13 16:44:20.0

I tried:

df['timestamp'] = df['timestamp'].dt.strftime('%Y-%m-%d')

`AttributeError: Can only use .dt accessor with datetimelike values.`

Any thoughts? Thank you!

Upvotes: 1

Views: 4445

Answers (3)

Richa Monga
Richa Monga

Reputation: 71

Using the below shown method also helps to achieve the same.

df['timestamp'] = pd.to_datetime(df['timestamp']).dt.date

You can refer to the documentation provided in the below link as a handy guide for date time handling: https://pandas.pydata.org/pandas-docs/stable/api.html#datetimelike-properties

Upvotes: 1

Pyd
Pyd

Reputation: 6159

convert the series into datetime datatype and try,

df['timestamp'] = pd.to_datetime(df['timestamp']).dt.strftime('%Y-%m-%d')

Upvotes: 2

pwxcoo
pwxcoo

Reputation: 3253

it may satisfy your demand

df['timestamp'] = pd.to_datetime(df['timestamp']).dt.strftime('%Y-%m-%d')

Upvotes: 1

Related Questions