本文介绍了python大 pandas 提取独特的日期从时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含很多日内数据的DataFrame,DataFrame有几天的数据,日期不连续。

  2012-10-08 07:12:22 0.0 0 0 2315.6 0 0.0 0 
2012-10-08 09:14:00 2306.4 20 326586240 2306.4 472 2306.8 4
2012-10-08 09:15:00 2306.8 34 249805440 2306.8 361 2308.0 26
2012-10-08 09:15:01 2308.0 1 53309040 2307.4 77 2308.6 9
2012-10-08 09:15:01.500000 2308.2 1 124630140 2307.0 180 2308.4 1
2012-10-08 09:15:02 2307.0 5 85846260 2308.2 124 2308.0 9
2012-10-08 09:15:02.500000 2307.0 3 128073540 2307.0 185 2307.6 11
......
2012-10-10 07:19:30 0.0 0 0 2276.6 0 0.0 0
2012-10-10 09:14:00 2283.2 80 98634240 2283.2 144 2283.4 1
2012-10-10 09:15:00 2285.2 18 126814260 2285.2 185 2285.6 3
2012-10-10 09 :15:01 2285.8 6 98719560 2286.8 144 2287.0 25
2012-10-10 09:15:01.500000 2287.0 36 1447 59420 2288.8 211 2289.0 4
2012-10-10 09:15:02 2287.4 6 109829280 2287.4 160 2288.6 5
......

如何从上述DataFrame中提取datetime格式的唯一日期?要结果像 [2012-10-08,2012-10-10]

解决方案

如果您有系列,请执行以下操作:

  [116]:df [Date] 
输出[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
名称:日期

每个对象是一个 Timestamp

 在[117]中:df [Date] [0] 
Out [117]:< Timestamp:2012-10-08 07:12:22>

只能通过调用 .date()

 在[118]中:df [Date] [0] .date()
Out [118]:datetime.date(2012,10,8)

code> .unique()方法。所以你可以使用 map lambda

 在[126]中:df [Date]。map(lambda t:t.date())。unique()
Out [126]:array([2012- 10-08,2012-10-10],dtype = object)

或使用 Timestamp.date 方法:

 在[127]中:df [Date] .map(pd.Timestamp.date).unique()
Out [127]:array([2012-10-08,2012-10-10],dtype = object)


I have a DataFrame which contains a lot of intraday data, the DataFrame has several days of data, dates are not continuous.

 2012-10-08 07:12:22            0.0    0          0  2315.6    0     0.0    0
 2012-10-08 09:14:00         2306.4   20  326586240  2306.4  472  2306.8    4
 2012-10-08 09:15:00         2306.8   34  249805440  2306.8  361  2308.0   26
 2012-10-08 09:15:01         2308.0    1   53309040  2307.4   77  2308.6    9
 2012-10-08 09:15:01.500000  2308.2    1  124630140  2307.0  180  2308.4    1
 2012-10-08 09:15:02         2307.0    5   85846260  2308.2  124  2308.0    9
 2012-10-08 09:15:02.500000  2307.0    3  128073540  2307.0  185  2307.6   11
 ......
 2012-10-10 07:19:30            0.0    0          0  2276.6    0     0.0    0
 2012-10-10 09:14:00         2283.2   80   98634240  2283.2  144  2283.4    1
 2012-10-10 09:15:00         2285.2   18  126814260  2285.2  185  2285.6    3
 2012-10-10 09:15:01         2285.8    6   98719560  2286.8  144  2287.0   25
 2012-10-10 09:15:01.500000  2287.0   36  144759420  2288.8  211  2289.0    4
 2012-10-10 09:15:02         2287.4    6  109829280  2287.4  160  2288.6    5
 ......

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]

解决方案

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)

这篇关于python大 pandas 提取独特的日期从时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 21:02