John
John

Reputation: 83

How to convert timedelta column in pandas to string

So I have a column in a pandas dataframe of times (from basketball data), and I would like to convert it to strings. What is the easiest way to do this? I've tried both str() and strftime to know avail (that said, I could be using the latter wrong).

The data is in the format '%H:%S'(5:34) and I would like a string formatted the same way.

Upvotes: 2

Views: 1491

Answers (1)

Marius
Marius

Reputation: 60190

As long as your columns are datetimes, they have a .dt accessor with lots of useful datetime methods attached, including strftime, so you can do:

import pandas as pd

df = pd.DataFrame({'dates': pd.date_range('2015-01-01', '2015-01-10', freq='12H')})
df.dates.dt.strftime('%H:%S')

Upvotes: 2

Related Questions