PyRaider
PyRaider

Reputation: 689

Converting pandas series timestamp to list of unique dates

I have a column in pandas dataframe in timestamp format and want to extract unique dates (no time) into a list. I tried following ways doesn't really work,

1. dates =  datetime.datetime(df['EventTime'].tolist()).date()
2. dates =  pd.to_datetime(df['EventTime']).date().tolist()
3. dates =  pd.to_datetime(df['EventTime']).tolist().date()

can anyone help?

Upvotes: 4

Views: 14324

Answers (1)

akuiper
akuiper

Reputation: 214957

You can use dt to access the date time object in a Series, try this:

pd.to_datetime(df['EventTime']).dt.date.unique().tolist()
# [datetime.date(2014, 1, 1), datetime.date(2014, 1, 2)]

df = pd.DataFrame({"EventTime": ["2014-01-01", "2014-01-01", "2014-01-02 10:12:00", "2014-01-02 09:12:00"]})

Upvotes: 14

Related Questions