I have a DataFrame which contains a lot of intraday data, the DataFrame has several days of data, dates are not continuous.
JavaScript
x
16
16
1
2012-10-08 07:12:22 0.0 0 0 2315.6 0 0.0 0
2
2012-10-08 09:14:00 2306.4 20 326586240 2306.4 472 2306.8 4
3
2012-10-08 09:15:00 2306.8 34 249805440 2306.8 361 2308.0 26
4
2012-10-08 09:15:01 2308.0 1 53309040 2307.4 77 2308.6 9
5
2012-10-08 09:15:01.500000 2308.2 1 124630140 2307.0 180 2308.4 1
6
2012-10-08 09:15:02 2307.0 5 85846260 2308.2 124 2308.0 9
7
2012-10-08 09:15:02.500000 2307.0 3 128073540 2307.0 185 2307.6 11
8
9
2012-10-10 07:19:30 0.0 0 0 2276.6 0 0.0 0
10
2012-10-10 09:14:00 2283.2 80 98634240 2283.2 144 2283.4 1
11
2012-10-10 09:15:00 2285.2 18 126814260 2285.2 185 2285.6 3
12
2012-10-10 09:15:01 2285.8 6 98719560 2286.8 144 2287.0 25
13
2012-10-10 09:15:01.500000 2287.0 36 144759420 2288.8 211 2289.0 4
14
2012-10-10 09:15:02 2287.4 6 109829280 2287.4 160 2288.6 5
15
16
How can I extract the unique date in the datetime format from the above DataFrame? To have result like [2012-10-08, 2012-10-10]
Advertisement
Answer
If you have a Series
like:
JavaScript
1
17
17
1
In [116]: df["Date"]
2
Out[116]:
3
0 2012-10-08 07:12:22
4
1 2012-10-08 09:14:00
5
2 2012-10-08 09:15:00
6
3 2012-10-08 09:15:01
7
4 2012-10-08 09:15:01.500000
8
5 2012-10-08 09:15:02
9
6 2012-10-08 09:15:02.500000
10
7 2012-10-10 07:19:30
11
8 2012-10-10 09:14:00
12
9 2012-10-10 09:15:00
13
10 2012-10-10 09:15:01
14
11 2012-10-10 09:15:01.500000
15
12 2012-10-10 09:15:02
16
Name: Date
17
where each object is a Timestamp
:
JavaScript
1
3
1
In [117]: df["Date"][0]
2
Out[117]: <Timestamp: 2012-10-08 07:12:22>
3
you can get only the date by calling .date()
:
JavaScript
1
3
1
In [118]: df["Date"][0].date()
2
Out[118]: datetime.date(2012, 10, 8)
3
and Series have a .unique()
method. So you can use map
and a lambda
:
JavaScript
1
3
1
In [126]: df["Date"].map(lambda t: t.date()).unique()
2
Out[126]: array([2012-10-08, 2012-10-10], dtype=object)
3
or use the Timestamp.date
method:
JavaScript
1
3
1
In [127]: df["Date"].map(pd.Timestamp.date).unique()
2
Out[127]: array([2012-10-08, 2012-10-10], dtype=object)
3