Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deal with time Series in Pandas Library

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, the editor will bring you about how to deal with the time series in the Pandas library. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

When using Python for data analysis, we often encounter time and date format processing and conversion, especially the analysis and mining of time-related data, such as quantitative trading is to find the changing rules of stock prices from historical data. The module of processing time in Python has datetime,NumPy library and also provides corresponding methods. As a data analysis library in Python environment, Pandas provides a powerful function of date data processing and is a sharp tool to deal with time series.

1. Generate a date sequence

It mainly provides two methods: pd.data_range () and pd.period_range (). The given parameters are start time, end time, number of generation periods and time frequency (freq='M' month,'D 'day,' week, week,'Y 'year) and so on.

The main difference between the two is that pd.date_range () generates a date sequence in DatetimeIndex format; pd.period_range () generates a date sequence in PeriodIndex format.

The following is compared by generating monthly time series and weekly time series:

Date_rng = pd.date_range ('2019-01-01, freq='M', periods=12) print (f'month date_range (): {date_rng}') "date_range (): DatetimeIndex (['2019-01-31,' 2019-02-28, '2019-03-31,' 2019-04-30, '2019-05-31,' 2019-06-30), '2019-07-31,' 2019-08-31' '2019-09-30,' 2019-10-31, '2019-11-30,' 2019-12-31], dtype='datetime64 [ns]', freq='M') "period_rng = pd.period_range ('2019 f'month period_range 01, freq='M', periods=12) print (f'month period_range (): {period_rng}')" period_range (): PeriodIndex (['2019-01,' 2019-02, '2019-03 ") 2019-04, 2019-05, 2019-06, 2019-07, 2019-08, 2019-09, 2019-10, 2019-11, 2019-12], dtype='period [M], freq='M') "" date_rng = pd.date_range ('2019-01-01-01, freq='W-SUN') Periods=12) print (f'week date_range (): {date_rng}') "" week date_range (): DatetimeIndex (['2019-01-06,' 2019-01-13, '2019-01-20,' 2019-01-27, '2019-02-03,' 2019-02-10, '2019-02-17,' 2019-02-24, '2019-03-03, 2019-03-10, 2019-03-17') '2019-03-24'], dtype='datetime64 [ns], freq='W-SUN') "" period_rng=pd.period_range ('2019-01-01-01-01-01) print (f'week period_range (): {period_rng}') print (): {period_rng}') "week period_range (): PeriodIndex (['2018-12-31 Universe 2019-01-06,' 2019-01-01-07deband 2019-01-13, '2019-01-14 Universe 2019-01-20' '2019-01-21, '2019-01-28, 2019-02-03, 2019-02-04, 2019-02-10, 2019-02-11, 2019-02-17, 2019-02-18, 2019-02-24, 2019-02-25, 2019-03-03, 2019-03-04, 2019-03-10, 2019-03-11, 2019-03-17, 2019-03-24, 2019-03-24, 2019-03-24, 2019-03-03, 2019-03-10, 2019-03-04, 2019-03-10, 2019-03-04, 2019-03-10, 2019-03-04, 2019-03-10, 2019-03-11, 2019-03-17, 2019-03-24, 2019-03-24, 2019-03-03, 2019-03-10, 2019-03-03, 2019-03-10, 2019-03-03, 2019-03-10, 2019 Dtype='period [W-SUN]', freq='W-SUN') "date_rng = pd.date_range ('2019-01-01 00 freq='W-SUN', freq='H', periods=12) print (f'hour date_range (): {date_rng}')" hour date_range (): DatetimeIndex (['2019-01-01 00 freq='W-SUN') [2019-01-01 00 freq='W-SUN'] " '2019-01-01-01 02,' 2019-01-01-01 03, '2019-01-01 04, 0000,' 2019-01-01 05, '2019-01-01 06, 0000,' 2019-01-01 07, 0000, '2019-01-01 08, 0000,' 2019-01-01-01 09, 0000, 2019-01-01 Dtype='datetime64 [ns]', freq='H') "period_rng=pd.period_range ('2019-01-01 0012) print (f'hour period_range (): {period_rng}')" hour period_range (): PeriodIndex (['2019-01-01 0000V],' 2019-01-01 01L), '2019-01-01 02purl' '2019-01-01-01 03,' 2019-01-01 04, '2019-01-01 05,' 2019-01-01 06, '2019-01-01 07,' 2019-01-01 08, '2019-01-01 09,' 2019-01-01 10, '2019-01-01 1100'], dtype='period [H]', freq='H') "

2. Generate Timestamp objects and transform them

Creating a Timestamp timestamp object has the pd.Timestamp () method and the pd.to_datetime () method. As follows:

Ts=pd.Timestamp (20119 ts 1) print (f'pd.Timestamp ()-1: {ts}') # pd.Timestamp ()-1 ts 2019-01-01 00:00:00ts=pd.Timestamp (dt (20119) Second=1)) print (f'pd.Timestamp ()-2: {ts}') # pd.Timestamp ()-2 ts 2019-01-01 00:01:01ts=pd.Timestamp ("2019-1-1 0:1:1") print (f'pd.Timestamp ()-3: {ts}') # pd.Timestamp ()-3 pd.Timestamp (f'pd.Timestamp ()-type: {type (ts)}') # pd.Timestamp ()-type:#dt=pd.to_datetime (20119 1) dt=pd.to_datetime is not supported (dt (2019, 1, 1, 1, 1, 1, 4, 4, 1, 0, 2, 4, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 4, 1, 1, 2, 4, 1, 1, 2, 4, 4, 1, 1, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, Second=1)) print (f'pd.to_datetime ()-1: {dt}') # pd.to_datetime ()-1dt 2019-01-01 00:01:01dt=pd.to_datetime ("2019-1-1 0:1:1") print (f'pd.to_datetime ()-2: {dt}') # pd.to_datetime ()-2pur2019-01-01 00:01:01print (f'pd.to_datetime ()-type: {type (dt)}) ') # pd.to_datetime ()-type:#pd.to_datetime generates a custom time series dtlist=pd.to_datetime (["2019-1-1 0:1:1" "2019-3-1 0:1:1"]) print (f'pd.to_datetime ()-list: {dtlist}') # pd.to_datetime ()-list:DatetimeIndex (['2019-01-01 00list 01list 01purr 01',' 2019-03-000list 01purl 01purl], dtype='datetime64 [ns]' Freq=None) # timestamp converted to period month period pr = ts.to_period ('M') print (f'ts.to_period (): {pr}') # ts.to_period (): 2019-01print (f'pd.to_period ()-type: {type (pr)}') # pd.to_period ()-type:

3. Generate period objects and transform them

# define the period periodper=pd.Period ('2019') print (f'pd.Period (): {per}') # pd.Period (): 2019per_del=pd.Period ('2019')-pd.Period (' 2018') print (fags 2019 and 2018 interval {per_del} years') # can directly +, -Integer (for year) # 2019 and 2018 interval 1 year # period converted to timestamp print (per.to_timestamp (how='end')) # 2019-12-31 00:00:00print (per.to_timestamp (how='start')) # 2019-01-01 00:00:00

4. Generation interval Timedelta

# generate time interval Timedeltaprint (pd.Timedelta (days=5, minutes=50, seconds=20, milliseconds=10, microseconds=10, nanoseconds=10)) # 5 days 00days=5 5020. 01001 get the current time now=pd.datetime.now () # calculate the date dt=now+pd.Timedelta (days=50) print 50 days after the current time (f 'the current time is {now}, 50 days later is {dt}') # the current time is 2019-06-08 17days=5 minutes=50 5931.726065 50 days later, the time is 2019-07-28 17 591 72606 showing only print (dt.strftime ('% Ymuri% mmure% d')) # 2019-07-28

5. Resampling and frequency conversion

# asfreq displays index values on a quarterly basis # 'DatetimeIndex' object has no attribute' asfreq'date=pd.date_range ('1print 2018, periods=20, freq='D') tsdat_series=pd.Series (range (20), index=date) tsp_series=tsdat_series.to_period (' D') print (tsp_series.index.asfreq ('Q')) date=pd.period_range ('1Compare 2018 print, periods=20, freq='D') tsper_series=pd.Series (range (20)) Index=date) print (tsper_series.index.asfreq ('Q')) "PeriodIndex (['2018Q1,' 2018Q1, '2018Q1,' 2018Q1, '2018Q1,' 2018Q1, '2018Q1,' 2018Q1, '2018Q1,' 2018Q1, '2018Q1,' 2018Q1, '2018Q1,' 2018Q1, '2018Q1,] Dtype='period [Q-DEC]', freq='Q-DEC') "" # resample counts and displays print (tsdat_series.resample ('Q'). Sum (). To_period ('Q')) "" 2018Q1 190Freq: Q-DEC Dtype: int64 "" # groupby aggregates the weekly average print (tsdat_series.groupby (lambda x:x.weekday). Mean ()) "" 0 7.01 8.02 9.03 10.04 11.05 12.06 9.5dtype: float64 "" this is what the editor shares with you on how to deal with the time series in the Pandas database. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report