程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python pandas time series data

編輯:Python

python pandas time series data

List of articles

  • 1. to_datetime() “ String sequence ” To time series
  • 2. Use datetime Object sequence Get time series data
  • 3. pd.date_range() Create time series data
    • 3.1 Create method ① Appoint periods
    • 3.2 Create method ② Specify first
    • 3.3 Create method ③ Specified frequency freq
  • 4. The time of the Indexes
    • 4.1 Indexes Get individual data
    • 4.2 Index year
    • 4.3 Index year
  • 6. section
    • 6.1 Support `[start:end:step]` section
    • 6.2 section ( Contains end data )
    • 6.3 Modify data by slicing
    • 6.4 truncate() Truncation function
  • 7. Empty date in time series
    • 7.1 The form of a null date
    • 7.2 Judge null
  • 7.3 Remove null date data
  • 8. Time sequence
    • 8.1 positive sequence
    • 8.2 In reverse order
  • 9. date Of duplicate value
    • 9.1 Determine if there are duplicate values is_unique attribute
    • 9.2 polymerization
  • 10. Lag ahead And Hysteresis shift()
    • 10.1 Hysteresis
    • 10.2 Lag ahead


ʚʕ̯•͡˔•̯᷅ʔɞʚʕ̯•͡˔•̯᷅ʔɞʚʕ̯•͡˔•̯᷅ʔɞʚʕ̯•͡˔•̯᷅ʔɞʚʕ̯•͡˔•̯᷅ʔɞʚʕ̯•͡˔•̯᷅ʔɞʚʕ̯•͡˔•̯᷅ʔɞʚʕ̯•͡˔•̯᷅ʔɞʚʕ̯•͡˔•̯᷅ʔɞʚʕ̯•͡˔•̯᷅ʔɞ


꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ


import pandas as pd

1. to_datetime() “ String sequence ” To time series

Use to_datetime() Method can The data of the string sequence is converted to Index of time series , namely Convert data into time series data .

dates1 = pd.to_datetime(['1/6/2022', '3/6/2022', '2/6/2022'])
print(dates1)


__

2. Use datetime Object sequence Get time series data

With datetime Objects as a sequence Series or DataFrame The index of , Time series data , No additional methods are needed .

dates3 = [datetime(2022, 6, 1), datetime(2022, 6, 3), datetime(2022, 6, 4), datetime(2022, 6, 2), datetime(2022, 6, 5)]
s1 = pd.Series([2, 3, 5, 7, 9], index=dates3)
print(s1)
print("=============================================================")
print(s1.index)


3. pd.date_range() Create time series data

3.1 Create method ① Appoint periods

date_range1 = pd.date_range('1/1/2022', periods=1000)
s2 = pd.Series(np.random.randn(1000), index=date_range1)
print(s2)


3.2 Create method ② Specify first

date_range2 = pd.date_range('2022-2-1', '2022-3-1')
s2 = pd.Series(np.random.randn(len(date_range2)), index=date_range2)
print(s2)


3.3 Create method ③ Specified frequency freq

frequently-used freq Yes :

Parameters describe Y year M month W Zhou D God B Working day H Hours T minute s second

Examples are as follows :

date_range3 = pd.date_range('2022-06-01','2023-06-30',freq='3s')
s2 = pd.Series(np.random.randn(len(date_range3)), index=date_range3)
print(s2)


4. The time of the Indexes

4.1 Indexes Get individual data

dates = [datetime(2022, 6, 1), datetime(2022, 6, 3), datetime(2022, 6, 4), datetime(2022, 6, 2), datetime(2022, 6, 5)]
s1 = pd.Series([2, 3, 5, 7, 9], index=dates)
print(s1['2022 06 02'])
print(s1['2022-06-02'])
print(s1['2022/06/02'])
print(s1['2022.06.02'])
print(s1['2022, 06, 02'])
print("===========================")
# This method does not get a piece of data , It's a value .
print(s1[datetime(2022, 6, 2)])

4.2 Index year

Example year index

s2 = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2022', periods=1000))
print(s2['2022'])


4.3 Index year

s2 = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2022', periods=1000))
print(s2['2022-03'])


6. section

6.1 Support [start:end:step] section

print(s1[::2])


6.2 section ( Contains end data )

s2 = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2022', periods=1000))
print(s2['2022-03-19':'2022-04-02'])


6.3 Modify data by slicing

You can modify some data by slicing , The following example shows that the results are slightly .

s2 = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2022', periods=1000))
s2['2022-03-19':'2022-04-02'] = 1

6.4 truncate() Truncation function

Cut data after a certain date

s2 = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2022', periods=1000))
print(s2.truncate(after='10/6/2023'))


Cut the data before a certain date

s2 = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2022', periods=1000))
print(s2.truncate(before='10/6/2023'))


7. Empty date in time series

7.1 The form of a null date

dates2 = pd.to_datetime(['1/6/2022', '3/6/2022', '2/6/2022', None])
print(dates2)

If there is a null value in the original data None, Here it is transformed into NaT In the form of .


7.2 Judge null

Judge whether it is a null value ,isnull() Method It is available in time series index .

dates2 = pd.to_datetime(['1/6/2022', '3/6/2022', '2/6/2022', None])
print(pd.isnull(dates2))


7.3 Remove null date data

dates2 = pd.to_datetime(['1/6/2022', '3/6/2022', '2/6/2022', None])
print(dates2.dropna())


8. Time sequence

Sort the time

8.1 positive sequence

dates3 = [datetime(2022, 6, 1), datetime(2022, 6, 3), datetime(2022, 6, 4), datetime(2022, 6, 2), datetime(2022, 6, 5)]
s1 = pd.Series([2, 3, 5, 7, 9], index=dates3)
print(s1.sort_index())


8.2 In reverse order

print(s1.sort_index(ascending=False))


9. date Of duplicate value

9.1 Determine if there are duplicate values is_unique attribute

dates = pd.DatetimeIndex(['6/1/2022', '6/2/2022', '6/2/2022', '6/3/2022', '6/4/2022'])
s3 = pd.Series(['aaa', 'ccc', 'bbb', 'ddd', 'eee'], index=dates)
print(s3.index.is_unique)

Print out s3 Of is_unique attribute , The value is Fasle, Indicates that there are duplicate values .


9.2 polymerization

When there are a large number of duplicate indexes , Maybe suitable for polymerization .

Take the aggregate as an example .

print(s3.groupby(level=0).count())


10. Lag ahead And Hysteresis shift()

10.1 Hysteresis

s4 = pd.Series(np.random.randn(4),index=pd.date_range('1/1/2022',periods=4,freq='M'))
print(s4.shift(2))


10.2 Lag ahead

s4 = pd.Series(np.random.randn(4),index=pd.date_range('1/1/2022',periods=4,freq='M'))
print(s4.shift(-2))


꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ꧔ꦿ
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved