10-时间操作--数据分析

举报
brucexiaogui 发表于 2021/12/30 01:48:29 2021/12/30
【摘要】 数据表的时间类型操作 In [59]: ...

数据表的时间类型操作

In [59]:
import datetime
In [5]:
dt = datetime.datetime(year=2017,month=11,day=23,hour=7,minute=30)
dt
Out[5]:
datetime.datetime(2017, 11, 23, 7, 30)
In [6]:
print(dt)
2017-11-23 07:30:00

打印时间戳

In [19]:
import pandas as pd
ts = pd.Timestamp('2019-11-11')
ts
Out[19]:
Timestamp('2019-11-11 00:00:00')
In [20]:
ts.month
Out[20]:
11
In [21]:
ts.day
Out[21]:
11

日期相加计算

In [22]:
ts + pd.Timedelta('3 day')
Out[22]:
Timestamp('2019-11-14 00:00:00')

to_datetime() 方法构建时间

In [23]:
pd.to_datetime('2017-11-23')
Out[23]:
Timestamp('2017-11-23 00:00:00')
In [24]:
pd.to_datetime('23/11/2019')
Out[24]:
Timestamp('2019-11-23 00:00:00')

将数据类型转化为时间类型

  • s数据的类型是object ts数据的类型是datetime
In [25]:
s = pd.Series(['2019-11-23 00:00:00','2019-11-24 00:00:00','2019-11-25 00:00:00'])
s
Out[25]:
0    2019-11-23 00:00:00
1    2019-11-24 00:00:00
2    2019-11-25 00:00:00
dtype: object
In [26]:
ts = pd.to_datetime(s)
ts
Out[26]:
0   2019-11-23
1   2019-11-24
2   2019-11-25
dtype: datetime64[ns]

将数据转化为时间格式后,可以调用pandas库的时间方法。例如下面的方法:

  • 查看数据中的小时
In [27]:
ts.dt.hour
Out[27]:
0    0
1    0
2    0
dtype: int64
In [28]:
ts.dt.weekday
Out[28]:
0    5
1    6
2    0
dtype: int64

创建数据的时候,直接将需要的字段设置为时间类型

In [29]:
pd.Series(pd.date_range(start='2019-11-23',periods=10,freq='12H'))
Out[29]:
0   2019-11-23 00:00:00
1   2019-11-23 12:00:00
2   2019-11-24 00:00:00
3   2019-11-24 12:00:00
4   2019-11-25 00:00:00
5   2019-11-25 12:00:00
6   2019-11-26 00:00:00
7   2019-11-26 12:00:00
8   2019-11-27 00:00:00
9   2019-11-27 12:00:00
dtype: datetime64[ns]

操作包含时间的数据

In [30]:
data = pd.read_csv('C:/JupyterWork/data/flowdata.csv')
data.head()
Out[30]:
  Time L06_347 LS06_347 LS06_348
0 2009-01-01 00:00:00 0.137417 0.097500 0.016833
1 2009-01-01 03:00:00 0.131250 0.088833 0.016417
2 2009-01-01 06:00:00 0.113500 0.091250 0.016750
3 2009-01-01 09:00:00 0.135750 0.091500 0.016250
4 2009-01-01 12:00:00 0.140917 0.096167 0.017000

将某列的数据转换为时间类型

In [31]:
data['Time'] = pd.to_datetime(data['Time'])
data = data.set_index('Time')
data
Out[31]:
  L06_347 LS06_347 LS06_348
Time      
2009-01-01 00:00:00 0.137417 0.097500 0.016833
2009-01-01 03:00:00 0.131250 0.088833 0.016417
2009-01-01 06:00:00 0.113500 0.091250 0.016750
2009-01-01 09:00:00 0.135750 0.091500 0.016250
2009-01-01 12:00:00 0.140917 0.096167 0.017000
2009-01-01 15:00:00 0.099167 0.091667 0.017583
2009-01-01 18:00:00 0.132667 0.090167 0.016250
2009-01-01 21:00:00 0.109417 0.091167 0.016000
2009-01-02 00:00:00 0.133833 0.090417 0.016083
2009-01-02 03:00:00 0.092083 0.088667 0.016000
2009-01-02 06:00:00 0.112917 0.091417 0.016333
2009-01-02 09:00:00 0.141917 0.097083 0.016417
2009-01-02 12:00:00 0.147833 0.101917 0.016417
2009-01-02 15:00:00 0.107917 0.100250 0.016417
2009-01-02 18:00:00 0.143583 0.098417 0.016750
2009-01-02 21:00:00 0.113083 0.098083 0.016833
2009-01-03 00:00:00 0.135833 0.092167 0.016833
2009-01-03 03:00:00 0.083250 0.080000 0.016083
2009-01-03 06:00:00 0.119417 0.080250 0.015417
2009-01-03 09:00:00 0.124583 0.084417 0.015833
2009-01-03 12:00:00 0.091667 0.088250 0.016250
2009-01-03 15:00:00 0.125000 0.084667 0.016500
2009-01-03 18:00:00 0.121583 0.082083 0.015833
2009-01-03 21:00:00 0.107167 0.092500 0.016000
2009-01-04 00:00:00 0.135250 0.091167 0.016333
2009-01-04 03:00:00 0.135583 0.091583 0.016083
2009-01-04 06:00:00 0.117167 0.095167 0.016000
2009-01-04 09:00:00 0.109000 0.105167 0.018000
2009-01-04 12:00:00 0.157417 0.110750 0.018417
2009-01-04 15:00:00 0.160417 0.113750 0.018417
... ... ... ...
2012-12-29 09:00:00 0.786833 0.786833 0.077000
2012-12-29 12:00:00 0.723750 0.723750 0.072667
2012-12-29 15:00:00 0.690667 0.690667 0.069667
2012-12-29 18:00:00 0.663417 0.663417 0.069667
2012-12-29 21:00:00 0.735917 0.735917 0.072833
2012-12-30 00:00:00 0.753667 0.753667 0.061833
2012-12-30 03:00:00 0.663333 0.663333 0.073667
2012-12-30 06:00:00 0.796833 0.796833 0.095167
2012-12-30 09:00:00 0.916000 0.916000 0.101583
2012-12-30 12:00:00 1.465000 1.465000 0.086833
2012-12-30 15:00:00 1.314167 1.314167 0.085417
2012-12-30 18:00:00 1.239167 1.239167 0.098083
2012-12-30 21:00:00 1.069750 1.069750 0.101417
2012-12-31 00:00:00 0.973333 0.973333 0.085000
2012-12-31 03:00:00 0.850833 0.850833 0.073917
2012-12-31 06:00:00 0.735917 0.735917 0.069417
2012-12-31 09:00:00 0.682750 0.682750 0.066583
2012-12-31 12:00:00 0.651250 0.651250 0.063833
2012-12-31 15:00:00 0.629000 0.629000 0.061833
2012-12-31 18:00:00 0.617333 0.617333 0.060583
2012-12-31 21:00:00 0.846500 0.846500 0.170167
2013-01-01 00:00:00 1.688333 1.688333 0.207333
2013-01-01 03:00:00 2.693333 2.693333 0.201500
2013-01-01 06:00:00 2.220833 2.220833 0.166917
2013-01-01 09:00:00 2.055000 2.055000 0.175667
2013-01-01 12:00:00 1.710000 1.710000 0.129583
2013-01-01 15:00:00 1.420000 1.420000 0.096333
2013-01-01 18:00:00 1.178583 1.178583 0.083083
2013-01-01 21:00:00 0.898250 0.898250 0.077167
2013-01-02 00:00:00 0.860000 0.860000 0.075000

11697 rows × 3 columns

读取数据的时候,直接解析时间格式数据

In [32]:
data = pd.read_csv('C:/JupyterWork/data/flowdata.csv',index_col=0,parse_dates= True)
data.head()
Out[32]:
  L06_347 LS06_347 LS06_348
Time      
2009-01-01 00:00:00 0.137417 0.097500 0.016833
2009-01-01 03:00:00 0.131250 0.088833 0.016417
2009-01-01 06:00:00 0.113500 0.091250 0.016750
2009-01-01 09:00:00 0.135750 0.091500 0.016250
2009-01-01 12:00:00 0.140917 0.096167 0.017000

使用Timestamp()对时间列做分片处理

In [34]:
data[pd.Timestamp('2012-01-01 09:00'):pd.Timestamp('2012-01-01 19:00')]
Out[34]:
  L06_347 LS06_347 LS06_348
Time      
2012-01-01 09:00:00 0.330750 0.293583 0.029750
2012-01-01 12:00:00 0.295000 0.285167 0.031750
2012-01-01 15:00:00 0.301417 0.287750 0.031417
2012-01-01 18:00:00 0.322083 0.304167 0.038083

时间类型索引,可以直接使用分片获取数据

In [36]:
data[('2012-01-01 09:10'):('2012-01-02 09:10')]
Out[36]:
  L06_347 LS06_347 LS06_348
Time      
2012-01-01 12:00:00 0.295000 0.285167 0.031750
2012-01-01 15:00:00 0.301417 0.287750 0.031417
2012-01-01 18:00:00 0.322083 0.304167 0.038083
2012-01-01 21:00:00 0.355417 0.346500 0.080917
2012-01-02 00:00:00 1.069333 0.970000 0.071917
2012-01-02 03:00:00 0.886667 0.817417 0.070833
2012-01-02 06:00:00 1.231000 1.153083 0.150750
2012-01-02 09:00:00 1.647500 1.476667 0.076583

tail()显示表格中最后的5行数据

In [37]:
 
          
data.tail()
Out[37]:
  L06_347 LS06_347 LS06_348
Time      
2013-01-01 12:00:00 1.710000 1.710000 0.129583
2013-01-01 15:00:00 1.420000 1.420000 0.096333
2013-01-01 18:00:00 1.178583 1.178583 0.083083
2013-01-01 21:00:00 0.898250 0.898250 0.077167
2013-01-02 00:00:00 0.860000 0.860000 0.075000

tail() 设置最后获取的数据量

In [38]:
 
          
data.tail(10)
Out[38]:
  L06_347 LS06_347 LS06_348
Time      
2012-12-31 21:00:00 0.846500 0.846500 0.170167
2013-01-01 00:00:00 1.688333 1.688333 0.207333
2013-01-01 03:00:00 2.693333 2.693333 0.201500
2013-01-01 06:00:00 2.220833 2.220833 0.166917
2013-01-01 09:00:00 2.055000 2.055000 0.175667
2013-01-01 12:00:00 1.710000 1.710000 0.129583
2013-01-01 15:00:00 1.420000 1.420000 0.096333
2013-01-01 18:00:00 1.178583 1.178583 0.083083
2013-01-01 21:00:00 0.898250 0.898250 0.077167
2013-01-02 00:00:00 0.860000 0.860000 0.075000

获取某一年的数据

In [41]:
data['2013']
Out[41]:
  L06_347 LS06_347 LS06_348
Time      
2013-01-01 00:00:00 1.688333 1.688333 0.207333
2013-01-01 03:00:00 2.693333 2.693333 0.201500
2013-01-01 06:00:00 2.220833 2.220833 0.166917
2013-01-01 09:00:00 2.055000 2.055000 0.175667
2013-01-01 12:00:00 1.710000 1.710000 0.129583
2013-01-01 15:00:00 1.420000 1.420000 0.096333
2013-01-01 18:00:00 1.178583 1.178583 0.083083
2013-01-01 21:00:00 0.898250 0.898250 0.077167
2013-01-02 00:00:00 0.860000 0.860000 0.075000

获取一个时间段的数据

In [44]:
data['2012-02':'2012-03']
Out[44]:
  L06_347 LS06_347 LS06_348
Time      
2012-02-01 00:00:00 0.150917 0.208083 0.022250
2012-02-01 03:00:00 0.140917 0.200250 0.022083
2012-02-01 06:00:00 0.130667 0.191250 0.020250
2012-02-01 09:00:00 0.135583 0.186750 0.020000
2012-02-01 12:00:00 0.131750 0.183750 0.020917
2012-02-01 15:00:00 0.133333 0.177417 0.020667
2012-02-01 18:00:00 0.119333 0.168917 0.020667
2012-02-01 21:00:00 0.124417 0.174500 0.019333
2012-02-02 00:00:00 0.116167 0.167500 0.019000
2012-02-02 03:00:00 0.107333 0.157167 0.017417
2012-02-02 06:00:00 0.147750 0.217750 0.017167
2012-02-02 09:00:00 0.230583 0.313333 0.017000
2012-02-02 12:00:00 0.122250 0.174333 0.018000
2012-02-02 15:00:00 0.104083 0.152583 0.017500
2012-02-02 18:00:00 0.090917 0.150250 0.017083
2012-02-02 21:00:00 0.090667 0.145750 0.015750
2012-02-03 00:00:00 0.093833 0.153250 0.014417
2012-02-03 03:00:00 0.113083 0.193250 0.014667
2012-02-03 06:00:00 0.121333 0.221750 0.014083
2012-02-03 09:00:00 0.122917 0.195750 0.014000
2012-02-03 12:00:00 0.086667 0.140250 0.015500
2012-02-03 15:00:00 0.087833 0.133250 0.016083
2012-02-03 18:00:00 0.088417 0.138833 0.016667
2012-02-03 21:00:00 0.079000 0.137333 0.012833
2012-02-04 00:00:00 0.076333 0.131333 0.013917
2012-02-04 03:00:00 0.051750 0.131500 0.011417
2012-02-04 06:00:00 0.068667 0.169250 0.012333
2012-02-04 09:00:00 0.104750 0.178917 0.014333
2012-02-04 12:00:00 0.106500 0.165833 0.012667
2012-02-04 15:00:00 0.083000 0.140000 0.011583
... ... ... ...
2012-03-28 06:00:00 0.079583 0.122333 0.010750
2012-03-28 09:00:00 0.095333 0.120167 0.013250
2012-03-28 12:00:00 0.081500 0.121000 0.011417
2012-03-28 15:00:00 0.090917 0.119583 0.011250
2012-03-28 18:00:00 0.068417 0.122667 0.010500
2012-03-28 21:00:00 0.050583 0.123083 0.009083
2012-03-29 00:00:00 0.059083 0.121417 0.009917
2012-03-29 03:00:00 0.063833 0.121750 0.009250
2012-03-29 06:00:00 0.088500 0.122583 0.010917
2012-03-29 09:00:00 0.104917 0.121250 0.013250
2012-03-29 12:00:00 0.090167 0.121083 0.012083
2012-03-29 15:00:00 0.083667 0.121333 0.011583
2012-03-29 18:00:00 0.076833 0.120750 0.011667
2012-03-29 21:00:00 0.064667 0.119750 0.010667
2012-03-30 00:00:00 0.059083 0.118250 0.009500
2012-03-30 03:00:00 0.065583 0.119083 0.009417
2012-03-30 06:00:00 0.088500 0.122167 0.010750
2012-03-30 09:00:00 0.108500 0.121250 0.013500
2012-03-30 12:00:00 0.097417 0.123083 0.012750
2012-03-30 15:00:00 0.092833 0.121083 0.012167
2012-03-30 18:00:00 0.091083 0.121417 0.012417
2012-03-30 21:00:00 0.088500 0.123250 0.012083
2012-03-31 00:00:00 0.087417 0.123000 0.011000
2012-03-31 03:00:00 0.090833 0.123167 0.012500
2012-03-31 06:00:00 0.099417 0.124167 0.011667
2012-03-31 09:00:00 0.104917 0.125000 0.012417
2012-03-31 12:00:00 0.098333 0.124417 0.011833
2012-03-31 15:00:00 0.091917 0.123917 0.011500
2012-03-31 18:00:00 0.085750 0.121417 0.011000
2012-03-31 21:00:00 0.068417 0.119750 0.010417

480 rows × 3 columns

显示数据表中每年中月份为1月的数据

In [45]:
 
          
data[data.index.month == 1]
Out[45]:
  L06_347 LS06_347 LS06_348
Time      
2009-01-01 00:00:00 0.137417 0.097500 0.016833
2009-01-01 03:00:00 0.131250 0.088833 0.016417
2009-01-01 06:00:00 0.113500 0.091250 0.016750
2009-01-01 09:00:00 0.135750 0.091500 0.016250
2009-01-01 12:00:00 0.140917 0.096167 0.017000
2009-01-01 15:00:00 0.099167 0.091667 0.017583
2009-01-01 18:00:00 0.132667 0.090167 0.016250
2009-01-01 21:00:00 0.109417 0.091167 0.016000
2009-01-02 00:00:00 0.133833 0.090417 0.016083
2009-01-02 03:00:00 0.092083 0.088667 0.016000
2009-01-02 06:00:00 0.112917 0.091417 0.016333
2009-01-02 09:00:00 0.141917 0.097083 0.016417
2009-01-02 12:00:00 0.147833 0.101917 0.016417
2009-01-02 15:00:00 0.107917 0.100250 0.016417
2009-01-02 18:00:00 0.143583 0.098417 0.016750
2009-01-02 21:00:00 0.113083 0.098083 0.016833
2009-01-03 00:00:00 0.135833 0.092167 0.016833
2009-01-03 03:00:00 0.083250 0.080000 0.016083
2009-01-03 06:00:00 0.119417 0.080250 0.015417
2009-01-03 09:00:00 0.124583 0.084417 0.015833
2009-01-03 12:00:00 0.091667 0.088250 0.016250
2009-01-03 15:00:00 0.125000 0.084667 0.016500
2009-01-03 18:00:00 0.121583 0.082083 0.015833
2009-01-03 21:00:00 0.107167 0.092500 0.016000
2009-01-04 00:00:00 0.135250 0.091167 0.016333
2009-01-04 03:00:00 0.135583 0.091583 0.016083
2009-01-04 06:00:00 0.117167 0.095167 0.016000
2009-01-04 09:00:00 0.109000 0.105167 0.018000
2009-01-04 12:00:00 0.157417 0.110750 0.018417
2009-01-04 15:00:00 0.160417 0.113750 0.018417
... ... ... ...
2012-01-29 09:00:00 0.296833 0.315833 0.034750
2012-01-29 12:00:00 0.294000 0.311917 0.034333
2012-01-29 15:00:00 0.269500 0.308000 0.033000
2012-01-29 18:00:00 0.259417 0.304417 0.031833
2012-01-29 21:00:00 0.254583 0.296250 0.031333
2012-01-30 00:00:00 0.243500 0.287333 0.030917
2012-01-30 03:00:00 0.236250 0.281667 0.030250
2012-01-30 06:00:00 0.230333 0.272167 0.029417
2012-01-30 09:00:00 0.221833 0.263250 0.027833
2012-01-30 12:00:00 0.224250 0.262583 0.029250
2012-01-30 15:00:00 0.206000 0.256750 0.028917
2012-01-30 18:00:00 0.200417 0.258417 0.028250
2012-01-30 21:00:00 0.192750 0.251083 0.027250
2012-01-31 00:00:00 0.191250 0.247417 0.025917
2012-01-31 03:00:00 0.181083 0.241583 0.025833
2012-01-31 06:00:00 0.188750 0.236750 0.026000
2012-01-31 09:00:00 0.191000 0.231250 0.025583
2012-01-31 12:00:00 0.183333 0.227167 0.025917
2012-01-31 15:00:00 0.163417 0.221000 0.023750
2012-01-31 18:00:00 0.157083 0.220667 0.023167
2012-01-31 21:00:00 0.160083 0.214750 0.023333
2013-01-01 00:00:00 1.688333 1.688333 0.207333
2013-01-01 03:00:00 2.693333 2.693333 0.201500
2013-01-01 06:00:00 2.220833 2.220833 0.166917
2013-01-01 09:00:00 2.055000 2.055000 0.175667
2013-01-01 12:00:00 1.710000 1.710000 0.129583
2013-01-01 15:00:00 1.420000 1.420000 0.096333
2013-01-01 18:00:00 1.178583 1.178583 0.083083
2013-01-01 21:00:00 0.898250 0.898250 0.077167
2013-01-02 00:00:00 0.860000 0.860000 0.075000

1001 rows × 3 columns

使用boolean表达式帅选符合条件的数据

In [46]:
 
          
data[(data.index.hour>8) & (data.index.hour<12)]
Out[46]:
  L06_347 LS06_347 LS06_348
Time      
2009-01-01 09:00:00 0.135750 0.091500 0.016250
2009-01-02 09:00:00 0.141917 0.097083 0.016417
2009-01-03 09:00:00 0.124583 0.084417 0.015833
2009-01-04 09:00:00 0.109000 0.105167 0.018000
2009-01-05 09:00:00 0.161500 0.114583 0.021583
2009-01-06 09:00:00 0.100083 0.065583 0.015500
2009-01-07 09:00:00 0.138500 0.093917 0.015000
2009-01-08 09:00:00 0.101333 0.066417 0.016833
2009-01-09 09:00:00 0.061750 0.059417 0.015167
2009-01-10 09:00:00 0.193500 0.147000 0.013000
2009-01-11 09:00:00 0.080250 0.077417 0.013583
2009-01-12 09:00:00 0.132500 0.089167 0.016833
2009-01-13 09:00:00 0.196500 0.192667 0.045333
2009-01-14 09:00:00 0.322917 0.299250 0.029333
2009-01-15 09:00:00 0.210750 0.167500 0.025000
2009-01-16 09:00:00 0.157833 0.153917 0.023000
2009-01-17 09:00:00 0.218667 0.173333 0.022917
2009-01-18 09:00:00 0.633000 0.745667 0.077000
2009-01-19 09:00:00 1.042167 1.398500 0.133667
2009-01-20 09:00:00 0.753000 0.773000 0.065583
2009-01-21 09:00:00 0.398500 0.398500 0.042500
2009-01-22 09:00:00 0.362417 0.351250 0.036667
2009-01-23 09:00:00 8.237500 8.560000 0.383750
2009-01-24 09:00:00 1.857500 2.356667 0.099750
2009-01-25 09:00:00 0.575583 0.657750 0.059000
2009-01-26 09:00:00 0.305417 0.279917 0.044167
2009-01-27 09:00:00 0.279917 0.274917 0.032500
2009-01-28 09:00:00 0.287083 0.253833 0.031083
2009-01-29 09:00:00 0.260750 0.221833 0.028167
2009-01-30 09:00:00 0.242000 0.200167 0.024750
... ... ... ...
2012-12-03 09:00:00 0.144500 0.144500 0.074667
2012-12-04 09:00:00 0.292083 0.292083 0.041083
2012-12-05 09:00:00 0.775250 0.775250 0.075667
2012-12-06 09:00:00 0.467917 0.467917 0.060750
2012-12-07 09:00:00 0.509833 0.509833 0.096583
2012-12-08 09:00:00 0.457583 0.457583 0.064667
2012-12-09 09:00:00 0.288750 0.288750 0.053167
2012-12-10 09:00:00 0.289250 0.289250 0.060083
2012-12-11 09:00:00 0.226083 0.226083 0.037833
2012-12-12 09:00:00 0.201333 0.201333 0.035167
2012-12-13 09:00:00 0.175750 0.175750 0.034500
2012-12-14 09:00:00 0.165833 0.165833 0.035417
2012-12-15 09:00:00 0.576833 0.576833 0.065083
2012-12-16 09:00:00 0.381750 0.381750 0.046417
2012-12-17 09:00:00 0.305833 0.305833 0.050917
2012-12-18 09:00:00 0.302167 0.302167 0.070667
2012-12-19 09:00:00 0.282917 0.282917 0.041333
2012-12-20 09:00:00 0.306083 0.306083 0.068250
2012-12-21 09:00:00 0.550333 0.550333 0.059250
2012-12-22 09:00:00 0.378833 0.378833 0.069667
2012-12-23 09:00:00 5.917500 5.917500 0.286583
2012-12-24 09:00:00 1.638333 1.638333 0.151333
2012-12-25 09:00:00 1.719167 1.719167 0.146250
2012-12-26 09:00:00 1.354167 1.354167 0.127583
2012-12-27 09:00:00 1.076667 1.076667 0.103000
2012-12-28 09:00:00 0.961500 0.961500 0.092417
2012-12-29 09:00:00 0.786833 0.786833 0.077000
2012-12-30 09:00:00 0.916000 0.916000 0.101583
2012-12-31 09:00:00 0.682750 0.682750 0.066583
2013-01-01 09:00:00 2.055000 2.055000 0.175667

1462 rows × 3 columns

between_time() 获取一个闭合范围的数据

In [47]:
 
          
data.between_time('08:00','09:00')
Out[47]:
  L06_347 LS06_347 LS06_348
Time      
2009-01-01 09:00:00 0.135750 0.091500 0.016250
2009-01-02 09:00:00 0.141917 0.097083 0.016417
2009-01-03 09:00:00 0.124583 0.084417 0.015833
2009-01-04 09:00:00 0.109000 0.105167 0.018000
2009-01-05 09:00:00 0.161500 0.114583 0.021583
2009-01-06 09:00:00 0.100083 0.065583 0.015500
2009-01-07 09:00:00 0.138500 0.093917 0.015000
2009-01-08 09:00:00 0.101333 0.066417 0.016833
2009-01-09 09:00:00 0.061750 0.059417 0.015167
2009-01-10 09:00:00 0.193500 0.147000 0.013000
2009-01-11 09:00:00 0.080250 0.077417 0.013583
2009-01-12 09:00:00 0.132500 0.089167 0.016833
2009-01-13 09:00:00 0.196500 0.192667 0.045333
2009-01-14 09:00:00 0.322917 0.299250 0.029333
2009-01-15 09:00:00 0.210750 0.167500 0.025000
2009-01-16 09:00:00 0.157833 0.153917 0.023000
2009-01-17 09:00:00 0.218667 0.173333 0.022917
2009-01-18 09:00:00 0.633000 0.745667 0.077000
2009-01-19 09:00:00 1.042167 1.398500 0.133667
2009-01-20 09:00:00 0.753000 0.773000 0.065583
2009-01-21 09:00:00 0.398500 0.398500 0.042500
2009-01-22 09:00:00 0.362417 0.351250 0.036667
2009-01-23 09:00:00 8.237500 8.560000 0.383750
2009-01-24 09:00:00 1.857500 2.356667 0.099750
2009-01-25 09:00:00 0.575583 0.657750 0.059000
2009-01-26 09:00:00 0.305417 0.279917 0.044167
2009-01-27 09:00:00 0.279917 0.274917 0.032500
2009-01-28 09:00:00 0.287083 0.253833 0.031083
2009-01-29 09:00:00 0.260750 0.221833 0.028167
2009-01-30 09:00:00 0.242000 0.200167 0.024750
... ... ... ...
2012-12-03 09:00:00 0.144500 0.144500 0.074667
2012-12-04 09:00:00 0.292083 0.292083 0.041083
2012-12-05 09:00:00 0.775250 0.775250 0.075667
2012-12-06 09:00:00 0.467917 0.467917 0.060750
2012-12-07 09:00:00 0.509833 0.509833 0.096583
2012-12-08 09:00:00 0.457583 0.457583 0.064667
2012-12-09 09:00:00 0.288750 0.288750 0.053167
2012-12-10 09:00:00 0.289250 0.289250 0.060083
2012-12-11 09:00:00 0.226083 0.226083 0.037833
2012-12-12 09:00:00 0.201333 0.201333 0.035167
2012-12-13 09:00:00 0.175750 0.175750 0.034500
2012-12-14 09:00:00 0.165833 0.165833 0.035417
2012-12-15 09:00:00 0.576833 0.576833 0.065083
2012-12-16 09:00:00 0.381750 0.381750 0.046417
2012-12-17 09:00:00 0.305833 0.305833 0.050917
2012-12-18 09:00:00 0.302167 0.302167 0.070667
2012-12-19 09:00:00 0.282917 0.282917 0.041333
2012-12-20 09:00:00 0.306083 0.306083 0.068250
2012-12-21 09:00:00 0.550333 0.550333 0.059250
2012-12-22 09:00:00 0.378833 0.378833 0.069667
2012-12-23 09:00:00 5.917500 5.917500 0.286583
2012-12-24 09:00:00 1.638333 1.638333 0.151333
2012-12-25 09:00:00 1.719167 1.719167 0.146250
2012-12-26 09:00:00 1.354167 1.354167 0.127583
2012-12-27 09:00:00 1.076667 1.076667 0.103000
2012-12-28 09:00:00 0.961500 0.961500 0.092417
2012-12-29 09:00:00 0.786833 0.786833 0.077000
2012-12-30 09:00:00 0.916000 0.916000 0.101583
2012-12-31 09:00:00 0.682750 0.682750 0.066583
2013-01-01 09:00:00 2.055000 2.055000 0.175667

1462 rows × 3 columns

resample('D') 重采样方法,表示按照传入的条件进行数据采样。参数D:day日的意思,表示按照以天为单位进行采样

  • 例如按照每天为单位采样计算每天的平均值
In [48]:
 
          
data.resample('D').mean().head()
Out[48]:
  L06_347 LS06_347 LS06_348
Time      
2009-01-01 0.125010 0.092281 0.016635
2009-01-02 0.124146 0.095781 0.016406
2009-01-03 0.113562 0.085542 0.016094
2009-01-04 0.140198 0.102708 0.017323
2009-01-05 0.128812 0.104490 0.018167
In [49]:
data.resample('D').max().head()
Out[49]:
  L06_347 LS06_347 LS06_348
Time      
2009-01-01 0.140917 0.097500 0.017583
2009-01-02 0.147833 0.101917 0.016833
2009-01-03 0.135833 0.092500 0.016833
2009-01-04 0.160417 0.113750 0.018417
2009-01-05 0.161500 0.115167 0.021583
  • 以多天为单位进行采样
In [50]:
data.resample('3D').mean().head()
Out[50]:
  L06_347 LS06_347 LS06_348
Time      
2009-01-01 0.120906 0.091201 0.016378
2009-01-04 0.121594 0.091708 0.016670
2009-01-07 0.097042 0.070740 0.014479
2009-01-10 0.115941 0.086340 0.014545
2009-01-13 0.346962 0.364549 0.034198

###

In [ ]:

文章来源: brucelong.blog.csdn.net,作者:Bruce小鬼,版权归原作者所有,如需转载,请联系作者。

原文链接:brucelong.blog.csdn.net/article/details/80751888

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。