Pandas:2, time series processing _ceilometer

Source: Internet
Author: User
 
#!/usr/bin/env python #-*-coding:utf-8-*-# @Time: 4/14/18 4:16 PM # @Author: Aries # @Site: # @File: t
imeseries_demo.py # @Software: Pycharm ' Pandas time Series reference: https://blog.csdn.net/ly_ysys629/article/details/73822716 https://blog.csdn.net/pipisorry/article/details/52209377 official document:http://pandas.pydata.org/pandas-docs/ stable/http://pandas.pydata.org/pandas-docs/stable/timeseries.html Index.is_unique Check that the index date is the only aggregation of data that is not unique to the timestamp. Through GroupBy, incoming level=0 (unique layer of index) meaning: Time series Type the Series type time series with the time stamp timestamp as the index element is only a special Series of index, so the normal index operation is still valid for the time series.
The special point is to optimize the operation of the time series index. The following is a data-duplication archive of commonly used operations 1 time series: usage is as follows: Dates = PD. Datetimeindex ([' 2017/06/01 ', ' 2017/06/02 ', ' 2017/06/02 ', ' 2017/06/02 ', ' 2017/06/03 ']) Dup_ts = PD. Series (Np.arange (5), index = dates) grouped = Dup_ts.groupby (level=0). Mean () 2 time series Data filtering: Usage is as follows: Dates = PD. Datetimeindex ([' 2017/06/01 ', ' 2017/06/02 ', ' 2017/06/02 ', ' 2017/06/02 ', ' 2017/06/03 ']) Dup_ts = PD. Series (Np.arange (5), index = dates) Print dup_ts[datetime (2017, 6,2)] Print dup_ts[' 2017-06-01 ': ' 2017-06-02 '] print dup_ts.truncate (after= ' 2017-06-01 ') 3 frequency converts a sequence to a fixed-frequency format by interpolation. Use the resample (rule) method as follows: Dates = PD. Datetimeindex ([' 2017-01-01 ', ' 2017-01-02 ', ' 2017-01-03 ', ' 2017-01-06 ']) ts = PD. Series (Np.arange (4), index = dates) print ts.resample (' D ') 4 move move (lead and lag) data movement (shifting): Move data forward or backward along the timeline, Series. Shift () method is used to perform but out of motion, the index remains unchanged print Ts.shift (2) Print Ts.shift (-2) Another move method is to move the index, the data is unchanged, and the Freq parameter is required to specify the mobile frequency print ts.shift (2, freq= ' D ') 5 time span generation date range, Pd.date_range () can generate a specified length of datetimeindex, parameters can be the start end date result = Pd.date_range (' 00:00 ', ' 12:00 ', freq= ' 1h20min ') result = Pd.date_range (' 20100101 ', ' 20100601 ', freq= ' M ') ran = Pd.period_range (' 2010-01 ', ' 2010-05 ', freq= ' M ') p = pd. Period (freq= ' M ') print P + 2 6 time series data aggregation processing dates = PD. Datetimeindex ([' 2017-01-01 ', ' 2017-01-02 ', ' 2017-01-03 ', ' 2017-01-06 ']) ts = PD. Series (Np.arange (4), index = dates) max = Ts.max () print max min = ts.min () print min sum = ts.sum () print sum mean = TS.M EAN () print mean count =Ts.count () Print count ' ' ' Import numpy as NP import pandas as PD from datetime import datetime def process (): Dates = [' 2017-06-20 ', ' 2017-06-21 ', ' 2017-06-22 ', ' 2017-06-23 ', ' 2017-06-24 ', ' 2017-06-25 ', ' 2017-06-26 ', ' 2017-0 6-27 '] ts = pd. Series (NP.RANDOM.RANDN (8), index = pd.to_datetime (dates)) Print TS # print Ts.index # once every two data print T

    S[::2] Print Ts[::-2] # Automatic data alignment print TS + ts[::2] Print ts[ts.index[2] [print ts[' 2017-06-21 '] # A time series with a duplicate index dates = PD. Datetimeindex ([' 2017/06/01 ', ' 2017/06/02 ', ' 2017/06/02 ', ' 2017/06/02 ', ' 2017/06/03 ']) print dates = PD. Series (Np.arange (5), index = dates) Print dup_ts print dup_ts.index.is_unique print dup_ts[' 2017-06-02 '] p
    Rint "Group" # here the GroupBy actually aggregates the data with the duplicate index in some way, and after the aggregation the result is the result of all the data grouped = Dup_ts.groupby (level=0). Mean () Print grouped # Filter data for a date print ' data:2017-6-2 ' Print dup_ts[datetime (2017,6,2) print"data:2017" Print dup_ts[' 2017 '] # can be sliced print dup_ts[' 2017-06-01 ': ' 2017-06-02 '] print ' truncate after ' Print dup_ts.truncate (after= ' 2017-06-02 ') print ' truncate before ' Print dup_ts.truncate (before= ' 2016 ') def Pro Cessfrequency (): dates = PD. Datetimeindex ([' 2017-01-01 ', ' 2017-01-02 ', ' 2017-01-03 ', ' 2017-01-06 ']) print dates ts = pd. Series (Np.arange (4), index = dates) Print TS # Converts a sequence to a fixed frequency format by interpolation, using the resample (rule) method print "Time Serize re Sample: "Print ts.resample (' D ') #生成日期范围, Pd.date_range () can generate a specified length of Datetimeindex, the parameter can be the starting end date #默认会按天计算时间点, through Freq Row change print "pandas range" result = Pd.date_range (' 20100101 ', ' 20100110 ') print result = Pd.date_rang
    E (' 20100101 ', ' 20100601 ', freq= ' M ') print result # frequency and date offset, frequency = base frequency and multiplier composition. # The base frequency is represented by a string alias, for example, BM, for each base frequency, there is an object that is called the date offset # by instantiating the date offset to create some frequency result = Pd.date_range (' 00:00 ', ' 12:00 ', freq= ' 1h20min ') Print result # move (advance and lag) data #Move (shifting): Move data forward or back along the timeline, # series has the. Shift () method for performing but out of move operations, the index remains unchanged print Ts.shift (2) Print Ts.shift (-2) # Another way to move is to move the index, the data will not change, you need the freq parameter to specify the mobile frequency print ts.shift (2, freq= ' D ') # Period and its arithmetic operation # Period refers to the time period, PD. The period constructor requires a timestamp, as well as a freq parameter, indicating the period length # timestamp indicating the position of period on the park Timeline Def processperiod (): print "period process" P = PD.P Eriod (freq= ' M ') print p print p + 2 # Period_range function can create a time range of rules ran = pd.period_range (' 2010-01 ', ' 20 10-05 ', freq= ' M ') print ran # Periodindex saves a set of period, which can be used as the frequency conversion of the axis index # period in the Pandas data structure # period and Periodindex objects can be Via the. Asfreq (Freq, Method=none, How=none) method is converted to another frequency Def processaggregation (): dates = PD. Datetimeindex ([' 2017-01-01 ', ' 2017-01-02 ', ' 2017-01-03 ', ' 2017-01-06 ']) ts = PD. Series (Np.arange (4), index = dates) Print TS print "aggregation max: max = Ts.max () Print max print T
  Ype (max) print "aggregation min: min = ts.min () print min print" aggregation sum: "sum = Ts.sum ()  Print sum print "aggregation mean:" mean = Ts.mean () print mean print "aggregation count:" Count = t S.count () print count if __name__ = = "__main__": Process () processfrequency () processperiod () proces Saggregation ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.