Converting pandas series timestamp to list of unique dates

10,562

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"]})
Share:
10,562
PyRaider
Author by

PyRaider

Updated on June 06, 2022

Comments

  • PyRaider
    PyRaider almost 2 years

    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?