current position:Home>Python - Pandas - time series

Python - Pandas - time series

2022-02-02 12:02:40 wlkkhhxx

Time series generation and resampling :

import pandas as pd
import numpy as np
time_line = pd.date_range(start="20210101",end="20210201",freq="10D")
# pd.date_range(start=None, end=None, periods=None, freq='D')
'''
DatetimeIndex(['2021-01-01', '2021-01-11', '2021-01-21', '2021-01-31'], dtype='datetime64[ns]', freq='10D')
'''
'''
 Alias        Offset type                explain 
D         Day                    Every calendar day 
B         BusinessDay            Every working day 
H         Hour                   Every hour 
T/min     Minute                 Per cent 
S         Second                 Per second 
L/ms      milli                  Every millisecond 
U         Micro                  Every subtle  
M         MonthEnd               Last day of each month 
BM        BusinessMonthEnd       The last working day of every month 
MS        MonthBegin             The first calendar day of every month 
BMS       BusinessMonthBegin     The first working day of every month 
'''

tl = pd.DataFrame(np.arange(10),index=pd.date_range('20210101',periods=10))
'''
            0
2021-01-01  0
2021-01-02  1
2021-01-03  2
2021-01-04  3
2021-01-05  4
2021-01-06  5
2021-01-07  6
2021-01-08  7
2021-01-09  8
2021-01-10  9
'''

tl2 = tl.resample('B').mean()
#  Resampling by working day 
'''
              0
2021-01-01  1.0
2021-01-04  3.0
2021-01-05  4.0
2021-01-06  5.0
2021-01-07  6.0
2021-01-08  8.0
'''

Time series reorganization :

import pandas as pd
from matplotlib import  pyplot as plt
file_path = "./PM2.5/BeijingPM20100101_20151231.csv"

# PeriodIndex, Separate the time string , Such as df Medium  year/month/day..., Reorganize into time series 
#  And the recombinant sequence is designated as index

df = pd.read_csv(file_path)
'''
 No  year  month  day  ...    Iws  precipitation  Iprec          datetime
0          1  2010      1    1  ...   1.79            0.0    0.0  2010-01-01 00:00
1          2  2010      1    1  ...   4.92            0.0    0.0  2010-01-01 01:00
2          3  2010      1    1  ...   6.71            0.0    0.0  2010-01-01 02:00
3          4  2010      1    1  ...   9.84            0.0    0.0  2010-01-01 03:00
4          5  2010      1    1  ...  12.97            0.0    0.0  2010-01-01 04:00
      ...   ...    ...  ...  ...    ...            ...    ...               ...
52579  52580  2015     12   31  ...   7.14            0.0    0.0  2015-12-31 19:00
'''

period = pd.PeriodIndex(year=df["year"],month=df["month"],day=df["day"],hour=df["hour"],freq="H")
'''
PeriodIndex(['2010-01-01 00:00', '2010-01-01 01:00', '2010-01-01 02:00',
             '2010-01-01 03:00', '2010-01-01 04:00', '2010-01-01 05:00',
             '2010-01-01 06:00', '2010-01-01 07:00', '2010-01-01 08:00',
             '2010-01-01 09:00',
             ...
             '2015-12-31 14:00', '2015-12-31 15:00', '2015-12-31 16:00',
             '2015-12-31 17:00', '2015-12-31 18:00', '2015-12-31 19:00',
             '2015-12-31 20:00', '2015-12-31 21:00', '2015-12-31 22:00',
             '2015-12-31 23:00'],
            dtype='period[H]', length=52584)
'''

df["datetime"] = period
print(df.head(10))
'''
   No  year  month  day  ...    Iws  precipitation  Iprec          datetime
0   1  2010      1    1  ...   1.79            0.0    0.0  2010-01-01 00:00
1   2  2010      1    1  ...   4.92            0.0    0.0  2010-01-01 01:00
2   3  2010      1    1  ...   6.71            0.0    0.0  2010-01-01 02:00
3   4  2010      1    1  ...   9.84            0.0    0.0  2010-01-01 03:00
4   5  2010      1    1  ...  12.97            0.0    0.0  2010-01-01 04:00
5   6  2010      1    1  ...  16.10            0.0    0.0  2010-01-01 05:00
6   7  2010      1    1  ...  19.23            0.0    0.0  2010-01-01 06:00
7   8  2010      1    1  ...  21.02            0.0    0.0  2010-01-01 07:00
8   9  2010      1    1  ...  24.15            0.0    0.0  2010-01-01 08:00
9  10  2010      1    1  ...  27.28            0.0    0.0  2010-01-01 09:00
[10 rows x 19 columns]
'''

copyright notice
author[wlkkhhxx],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202021202380845.html

Random recommended