python pandas extract unique dates from time series
If you have a Series
like:
In [116]: df["Date"]
Out[116]:
0 2012-10-08 07:12:22
1 2012-10-08 09:14:00
2 2012-10-08 09:15:00
3 2012-10-08 09:15:01
4 2012-10-08 09:15:01.500000
5 2012-10-08 09:15:02
6 2012-10-08 09:15:02.500000
7 2012-10-10 07:19:30
8 2012-10-10 09:14:00
9 2012-10-10 09:15:00
10 2012-10-10 09:15:01
11 2012-10-10 09:15:01.500000
12 2012-10-10 09:15:02
Name: Date
where each object is a Timestamp
:
In [117]: df["Date"][0]
Out[117]: <Timestamp: 2012-10-08 07:12:22>
you can get only the date by calling .date()
:
In [118]: df["Date"][0].date()
Out[118]: datetime.date(2012, 10, 8)
and Series have a .unique()
method. So you can use map
and a lambda
:
In [126]: df["Date"].map(lambda t: t.date()).unique()
Out[126]: array([2012-10-08, 2012-10-10], dtype=object)
or use the Timestamp.date
method:
In [127]: df["Date"].map(pd.Timestamp.date).unique()
Out[127]: array([2012-10-08, 2012-10-10], dtype=object)
Just to give an alternative answer to @DSM, look at this other answer from @Psidom
It would be something like:
pd.to_datetime(df['DateTime']).dt.date.unique()
It seems to me that it performs slightly better