Python Data analysis: Time series two

Source: Internet
Author: User

Convert Timestamp to Period

By using the to_period method, the Series and DataFrame objects indexed by the timestamp can be converted to a time -indexed

Rng=pd.date_range (' 1/1/2000 ', periods=3,freq= ' M ')

Ts=series (RANDN (3), index=rng)

Print (TS)

Pts2=ts.to_period (freq= ' M ')

Print (PTS2)

The results are as follows:TS is the date of the last day of each month,pts2 is the day of the month cycle

2000-01-31 on 0.990097

2000-02-29 on 0.439761

2000-03-31-3.395317

Freq:m, Dtype:float64

2000-01 0.990097

2000-02 0.439761

2000-03-3.395317

Freq:m, Dtype:float64

If you want to convert back to timestamp, you can use pts2.to_timestamp (how= ' end ') method

2000-01-31-0.489228

2000-02-29-1.583283

2000-03-31-2.414735

Freq:m, Dtype:float64

Resampling and Frequency conversion

Converting high-frequency data to low-frequency is called de-sampling, while low-frequency data is converted to high-frequency called L-sampling. Pandas in the resample method can be used for this frequency conversion

Rng=pd.date_range (' 1/1/2000 ', periods=50,freq= ' D ')

Ts=series (Randn (), index=rng)

Print (Ts.resample (' M '). Mean ())

The results of the operation are as follows, where TS is a day-level data, but is converted to monthly data by resample (' M ') , and averaged over data belonging to the same one-month period. The average of every month is what you get.

2000-01-31-0.276265

2000-02-29-0.052926

Freq:m, Dtype:float64

Drop Sampling:

There are two things to consider when you drop a sample:

1 which side of each zone is closed

2 How to mark each aggregation polygon, with the beginning or end of the interval

Consider the following code:

Rng=pd.date_range (' 1/1/2000 ', periods=12,freq= ' T ')

Ts=series (Np.arange (), index=rng)

Print (TS)

2000-01-01 00:00:00 0

2000-01-01 00:01:00 1

2000-01-01 00:02:00 2

2000-01-01 00:03:00 3

2000-01-01 00:04:00 4

2000-01-01 00:05:00 5

2000-01-01 00:06:00 6

2000-01-01 00:07:00 7

2000-01-01 00:08:00 8

2000-01-01 00:09:00 9

2000-01-01 00:10:00 10

2000-01-01 00:11:00 11

Print (Ts.resample (' 5min ', closed= ' left '). SUM ())

When left closed, the statistic is a 5 - minute cycle starting with 00:00:00 .

2000-01-01 00:00:00 10

2000-01-01 00:05:00 35

2000-01-01 00:10:00 21

Print (Ts.resample (' 5min ', closed= ' right '). SUM ())

When closing the right, The statistic is the 5 - minute cycle with 00:00:00 as the end, because the time is ahead to 1999-12-31 23:55:00 .

1999-12-31 23:55:00 0

2000-01-01 00:00:00 15

2000-01-01 00:05:00 40

2000-01-01 00:10:00 11

So left or right closing depends on the start and end of the time

In the financial world there is an omnipresent time-series aggregation, that is, the calculation of the 4 values of each polygon , the first value open: Open, the last value close: Close, Maximum high : Highest, minimum low : Lowest

Ts.resample (' 5min ', closed= ' left '). OHLC ()

Open High Low close

2000-01-01 00:00:00 0 4 0 4

2000-01-01 00:05:00 5 9 5 9

2000-01-01 00:10:00 10 11 10 11

Python Data analysis: Time series two

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.