Packet aggregation and the time series Pandas

Time Series # 
Import PANDAS AS PD Import numpy NP AS # generation period of time '' 'This function is mainly used to generate a time index of a fixed frequency, the constructor is invoked, must specify the start, end, periods of the two parameters value, otherwise an error. Time Series frequency: per day per hour H or T B min per minute daily calendar day on weekdays D S per L or the U-MS M the BM the MS the BMS per millisecond per microsecond date calendar days at the end of the working end of a date calendar day the early date early days of the date '' ' dATE = pd.date_range (= Start' 20,190,501 ', End =' 20.19053 million ') Print (dATE) Print ( "-" * 20 is) #freq: date offset, taking value or string DateOffset, the default is 'D', freq = '1h30min ' freq = '10D' # periods: a fixed period, or an integer value of None DATE = pd.date_range (= Start '20,190,501', = 10 periods, freq = '10D' # Time series dataFrame role20 *) # by between_time () Returns the specified data set period of time # Time can be used as an index index = pd.date_range (= Start '20,190,101', periods = 10) DF = pd.Series (np.random.randint (0,10, size = 10), index = index) Print (DF ) Print ( "-" * 20 is) long_ts = pd.Series (np.random.randn (1000), pd.date_range index = ( '. 1 /. 1/2019', periods = 1000)) Print (long_ts) Print ( " - "* 20) # acquired according Year Result = long_ts [ '2020'] Print (Result) Print (" - "20 *) # year and date acquired Result = long_ts [ '2020-05'] Print (Result) Print ( "-" 20 *) # using a microtome Result = long_ts [ '2020-05-01': '2020-05-06'] Print (Result) Print ( "-" 20 *) index = pd.date_range ( "2018- 03-17 ","2018-03-30",freq="2H") pd.Series = TS (np.random.randn (157), index = index) Print (ts.between_time ( "7:00", "17:00")) Print ( "-" 20 is *) # operations can also are suitable for dataframe index = pd.date_range ( '. 1 /. 1/2019', = 100 periods) DF = pd.DataFrame (np.random.randn (100,4), index = index) Print (df.loc [ ' 2019-04 ']) output: / the Users / the lazy / PycharmProjects / matplotlib / Venv / bin / Python /Users/lazy/PycharmProjects/matplotlib/drawing.py DatetimeIndex ([' 2019-05-01 ',' 2019-05- 02 ',' 2019-05-03 ',' 2019-05-04 ', ' 2019-05-05 ',' 2019-05-06 ',' 2019-05-07 ',' 2019-05-08 ' , '2019-05-09', '2019-05-10', '2019-05-11', '2019-05-12', '2019-05-13', '2019-05-14', '2019-05-15', '2019-05-16', '2019-05-17', '2019-05-18', '2019-05-19', '2019-05-20', '2019-05-21', '2019-05-22', '2019-05-23', '2019-05-24', '2019-05-25', '2019-05-26', '2019-05-27', '2019-05-28', '2019-05-29', '2019-05-30'], dtype='datetime64[ns]', freq='D') -------------------- DatetimeIndex(['2019-05-01', '2019-05-11', '2019-05-21', '2019-05-31', '2019-06-10', '2019-06-20', '2019-06-30', '2019-07-10', '2019-07-20', '2019-07-30'], dtype='datetime64[ns]', freq='10D') -------------------- 2019-01-01 9 2019-01-02 8 2019-01-03 9 2019-01-04 2 2019-01-05 4 2019-01-06 4 2019-01-07 0 2019-01-08 1 2019-01-09 4 2019-01-10 1 Freq: D, dtype: int64 -------------------- 2019-01-01 1.161118 2019-01-02 0.342857 2019-01-03 1.581292 2019-01-04 -0.928493 2019-01-05 -1.406328 ... 2021-09-22 0.106048 2021-09-23 0.228015 2021-09-24 -0.201558 2021-09-25 1.136008 2021-09-26 -0.947871 Freq: D, Length: 1000, dtype: float64 -------------------- 2020-01-01 1.828810 2020-01-02 1.425193 2020-01-03 -0.258607 2020-01-04 -0.390869 2020-01-05 -0.509062 ... 2020-12-27 0.155428 2020-12-28 -0.450071 2020-12-29 -0.050287 2020-12-30 0.033996 2020-12-31 -0.783760 Freq: D, Length: 366, dtype: float64 -------------------- 2020-05-01 0.843815 2020-05-02 -0.189866 2020-05-03 0.206807 2020-05-04 -0.279099 2020-05-05 0.575256 2020-05-06 -0.163009 2020-05-07 -0.850285 2020-05-08 -0.602792 2020-05-09 -0.630393 2020-05-10 -1.447383 2020-05-11 0.664726 2020-05-12 -0.108902 2020-05-13 0.333349 2020-05-14 1.068075 2020-05-15 -0.004767 2020-05-16 0.178172 2020-05-17 1.189467 2020-05-18 2.149068 2020-05-19 0.501122 2020-05-20 0.025200 2020-05-21 0.459819 2020-05-22 -0.688207 2020-05-23 -0.560723 2020-05-24 -0.448853 2020-05-25 0.612620 2020-05-26 0.781641 2020-05-27 0.225619 2020-05-28 -0.026749 2020-05-29 -0.020273 2020-05-30 0.812233 2020-05-31 -1.258738 Freq: D, dtype: float64 -------------------- 2020-05-01 0.843815 2020-05-02 -0.189866 2020-05-03 0.206807 2020-05-04 -0.279099 2020-05-05 0.575256 2020-05-06 -0.163009 Freq: D, dtype: float64 -------------------- 2018-03-17 08:00:00 0.704187 2018-03-17 10:00:00 0.496051 2018-03-17 12:00:00 1.828923 2018-03-17 14:00:00 -0.096337 2018-03-17 16:00:00 1.584530 ... 2018-03-29 08:00:00 0.779002 2018-03-29 10:00:00 -0.244056 2018-03-29 12:00:00 -0.428603 2018-03-29 14:00:00 1.297126 2018-03-29 16:00:00 0.482789 Length: 65, dtype: float64 -------------------- 0 1 2 3 2019-04-01 -2.074822 -0.939817 0.321402 -0.627823 2019-04-02 1.368356 0.150809 1.102027 -0.286527 2019-04-03 0.422506 -0.024193 -0.857528 1.061103 2019-04-04 -0.324066 -0.764358 -0.586841 1.520979 2019-04-05 1.398816 1.088023 -0.940833 1.249962 2019-04-06 -0.031951 0.905921 0.455782 -0.968012 2019-04-07 1.421253 -0.786199 0.875216 0.551437 2019-04-08 1.015066 -1.051041 0.430193 -0.014169 2019-04-09 0.279851 0.824598 -0.606735 -1.411600 2019-04-10 -0.252020 -0.408230 -0.698608 0.158843

 

 
 
PD PANDAS AS Import 
Import numpy AS NP

TS = pd.Series (np.random.randn (10), pd.date_range index = ( '. 1 /. 1/2019', periods = 10))
Print (TS)
Print ( "- "20 *)
# mobile data, the index change, the default NaN filled
# periods: negative bits are moved upward movement
# fill_value: filling data movement
Print (ts.shift (= 2 periods, fill_value = 100))
Print ( "-" 20 *)
# tShift by () move the index specified time:
Print (ts.tshift (2))
Print ( "-" 20 *)
# the time stamp is converted into radical
print (pd.to_datetime ( 1,554,970,740,000, Unit = 'MS'))
Print ( "-" * 20)
# UTC is coordinated Universal time, the time zone in the form of offset from UTC represented, but pay attention to set utc = True, let pandas object has a time zone properties, for a conversion will result in a conversion error
# unit = 'ms' is set to the millisecond level granularity
print (pd.to_datetime (1554970740000, unit = 'ms'). tz_localize ( 'UTC').tz_convert('Asia/Shanghai'))
print("-"*20)
# Processing a
DF = pd.DataFrame ([1,554,970,740,000, 1,554,970,800,000, 1554970860000], Columns = [ 'TIME_STAMP'])
Print (pd.to_datetime (DF [ 'TIME_STAMP'], Unit = 'MS'). Dt.tz_localize ( ' UTC '.) dt.tz_convert (' Asia / Shanghai ')) # when first given the standard time zone, and then converted to the East eight districts
Print ( "-" * 20)
# deal with Chinese
print (pd.to_datetime (' 2019 October 10 ', format ='% Y% m on day month% d '))
output:
/ the Users / the lazy / PycharmProjects / matplotlib / Venv / bin / Python /Users/lazy/PycharmProjects/matplotlib/drawing.py
2019-01- 01 -2.679356
2019-01-02 0.775274
2019-01-03 -0.045711
2019-01-04 0.883532
2019-01-05 -0.941213
2019-01-06 -1.461701
2019-01-07 0.149344
2019-01-08 -0.185037
2019 -01-09-0.754532
2019-01-10 0.561909
Freq: D, dtype: float64
--------------------
2019-01-01 100.000000
2019-01-02 100.000000
2019-01-03 -2.679356
2019-01-04 0.775274
2019-01-05 -0.045711
2019-01-06 0.883532
2019-01-07 -0.941213
2019-01-08 -1.461701
2019-01-09 0.149344
2019-01-10 -0.185037
Freq: D, dtype: float64
--------------------
2019-01-03 -2.679356
2019-01-04 0.775274
2019-01-05 -0.045711
2019-01-06 0.883532
2019-01-07 -0.941213
2019-01-08 -1.461701
2019-01-09 0.149344
2019-01-10 -0.185037
2019-01-11 -0.754532
2019-01-12 0.561909
Freq: D, dtype: float64
--------------------
2019-04-11 08:19:00
--------------------
2019-04-11 16:19:00+08:00
--------------------
0 2019-04-11 16:19:00+08:00
1 2019-04-11 16:20:00+08:00
2 2019-04-11 16:21:00+08:00
Name: time_stamp, dtype: datetime64[ns, Asia/Shanghai]
--------------------
2019-10-10 00:00:00

 

Guess you like

Origin www.cnblogs.com/imcati/p/11294989.html