python pandas extract unique dates from time series python pandas extract unique dates from time series pandas pandas

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:221           2012-10-08 09:14:002           2012-10-08 09:15:003           2012-10-08 09:15:014    2012-10-08 09:15:01.5000005           2012-10-08 09:15:026    2012-10-08 09:15:02.5000007           2012-10-10 07:19:308           2012-10-10 09:14:009           2012-10-10 09:15:0010          2012-10-10 09:15:0111   2012-10-10 09:15:01.50000012          2012-10-10 09:15:02Name: 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)


Using regex:

(\d{4}-\d{2}-\d{2})

Run it with re.findall function to get all matches:

result = re.findall(r"(\d{4}-\d{2}-\d{2})", subject)


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