Machine Learning pandas of time-series notes

Timestamp tiimestamp: fixed time -> pd.Timestamp

period fixed period of time: for example, in March 2016, another example, 2015 sales -> pd.Period

Interval interval: is represented by the start time and end time, a fixed period is a specific time interval

The date and time in the role of Pandas: analysis of financial data, such as stock transaction data

. 1  Import PANDAS AS PD
 2  Import numpy AS NP
 . 3  
. 4  # processing time need to use the package 
. 5  from datetime Import datetime
 . 6  from datetime Import timedelta
 . 7  
. 8 now DateTime.Now = ()   # outputs the current time 
9  # e.g. output datetime. datetime (2019,. 8, 23 is,. 17, 45, 13 is, 291738) 
10 now.year, now.month, now.day   # output date (2019,. 8, 23 is) 
. 11  
12 is DATAl = datetime (2016,. 4, 20 is )
 13 is DATA2 = datetime (2016,. 4, 16 )
14 Delta = DATAl - DATA2   # outputs the datetime.timedelta (. 4) 
15 delta.days   # output. 4 
16 delta.total_seconds ()   # output interval 345,600.0 seconds 
. 17 DATA2 Delta +   # Returns the day's date datetime.datetime (2016, 4, 20 is, 0, 0) 
18 is DATA2 + timedelta (4.5 of 5)   # 4.5 of 5 days, it outputs A datetime.datetime (2016,. 4, 20 is, 12 is, 0) 
. 19  
20 is DATE = datetime (2016,3,20,8,30 )
 21 is STR (date)   # converted into a string date '2016-03-20 08:30:00' 
22 is date.strftime ( " % the Y /% m /% D% H:% m:% S " )   #Formatted output '2016/03/20 08:30:00' 
23 is  
24 datetime.strptime ( ' 2016-03-20 09:30 ' , ' % Y-M-% D%% H:% M ' )
 25  # deformatter output A datetime.datetime (2016,. 3, 20 is,. 9, 30) 
26 is  
27 a dates = [datetime (2016,3,1), datetime (2016,3,2 ),
 28               datetime (2016,3,3) , datetime (2016,3,4 )]
 29 S = pd.Series (np.random.randn (. 4), index = a dates)  
 30  # the time becomes the index to Series way output 
31 is type (s.index)   # it the return value pandas.core.indexes.datetimes.DatetimeIndex 
32 type (s.index [0])  # The return value pandas._libs.tslibs.timestamps.Timestamp 
33 is  
34 is pd.date_range ( ' 20.16032 million ' , ' 20.16033 million ' )   # output time list, the default is the frequency interval / D 
35  
36 pd.date_range ( ' 20.16032 million 16:32 : 38 is ' , = 10 periods, = the normalize True)
 37 [  # output frequency intervals of 10 days by default / D per day i.e., using regularization normalize = True removed 16:32:38 
38 is  
39 pd.date_range ( ' 20.16032 million ' , periods = 10, FREQ = ' M ' )
 40  # output end time of the month, a total of 10 months, monthly interval frequency is 
41  #freq = BM (the last business day of each month), W weekly frequency, 4H 4 hours frequency 
42 is  
43 is  
44 is  
45  
46 is P = pd.Period (2010, FREQ = ' M ' )   # in months period output Period ( '2010-01', 'M') 
47 P + 2   # will lose output Period ( '2010-03', 'M') 
48  
49 pd.period_range ( ' 2016-01 ' , 10 periods = , FREQ = ' M ' )
 50  # at month intervals for 10 consecutive months frequency output 
51 is  
52 is pd.period_range ( ' 2016Q1 ' , = 10 periods, FREQ = ' Q ')
53 #Season for the frequency of the output at intervals of 10 consecutive seasons 
54 is  
55  
56 is A = pd.Period (2016)   # The default frequency band of 
57 is a.asfreq ( ' M ' )   # Output Period ( '2016-12', 'M ') to the last one month period default band that becomes dated 
58 a.asfreq ( ' M ' , How = ' Start ' )   # period becomes January period (' 2016-01 ',' M ') 
59 P = PD .Period ( ' 2016-04 ' , FREQ = ' M ' )   # custom period period ( '2016-04', 'M') 
60  
61 is  #  
62 is P.asfreq ( 'DEC-A ' )   # frequency is converted to an annual basis output Period ending December (' 2016 ',' A-DEC ') 
63 p.asfreq ( ' A-MAR ' )   # intervene month time becomes March to March 2017 
64-  
65 the p-pd.Period = ( ' 2016Q4 ' , ' Q-JAN ' )   # to the quarter ended January 
66  # next line can know the start time and end time 2016Q4 
67 p.asfreq ( ' M ' , How = ' Start ' ), p.asfreq ( ' M ' , How = ' End ')
68 
69  # Get the penultimate quarter days 16:20 
70 (p.asfreq ( ' B ' ) -. 1) .asfreq ( ' T ' ) + 16 * 60 +20

 

Guess you like

Origin www.cnblogs.com/yang901112/p/11426748.html
Recommended