Chronological order
stay pandas in , There is a very common function date_range, Especially when dealing with time series data , The purpose of this function is to generate a
DatetimeIndex, It is the index of time series data .
pandas.date_range(start=None, end=None, periods=None, freq=None, tz=None,
normalize=False, name=None, closed=None, **kwargs) →
pandas.core.indexes.datetimes.DatetimeIndex
Returns a fixed frequency DatetimeIndex.
parameter :
start:str or datetime-like, Optional , The default value is None, Represents the starting point of the date .
end:str or datetime-like, Optional , The default value is None, Indicates the end of the date .
periods:
int, Optional , The default value is None, Indicates how many Date index values you want to generate from and this function ( Number of cycles to generate ); If it is None In my words , that start and end Must not be
None.
freq:str or DateOffset, default “D”, Represents the unit of natural day , This parameter is used to specify the time unit , such as “5H” Indicates every 5 Every hour .
Name Description
B Business daily frequency
C Custom business daily frequency
D Calendar day frequency
W Weekly frequency
M Month end frequency
SM Monthly settlement frequency (15 second , end of the month )
BM
Business month end frequency
CBM
Custom business month end frequency
MS Monthly start frequency
SMS Start frequency (1 number ,15 number )
BMS
Business month start frequency
CBMS
Custom business month start frequency
Q Quarter end frequency
BQ
Business quarter end frequency
QS
Quarter start frequency
BQS
Quarter start frequency
A, Y Year end frequency
BA, BY
Business year end frequency
AS, YS
Annual starting frequency
BAS, BYS
Annual business start frequency
BH
Frequency of business hours
H Frequency per hour
T, min
Frequency per minute
S Frequency per second
L, ms millisecond
U, us Microsecond
N nanosecond
tz:str or tzinfo, Optional , Return to localized DatetimeIndex Time zone name for , for example ' Asia/Hong_Kong
'. By default , Generated DatetimeIndex It's time zone independent .
normalize:bool, default False. If True In my words , Before generating the time index value, the start and end It's all midnight of the day 0 spot .
name:str, default None. Specify a name for the returned time index .
closed:{None, ‘left’, ‘right’}, Optional . The default value is None, express
start and end Is the end of the interval included in the interval , There can be three values ,“left” Represents a left closed right open interval ,“right” Represents a left open right closed interval ,None It means that both sides are closed intervals .
**kwargs: compatibility , It didn't affect the results .
import pandas as pd, numpy as np # Create time series ( default freq="D") time_index =
pd.date_range(start="2020-03-01", end="2020-03-15") print(time_index) # Running results :
DatetimeIndex(['2020-03-01', '2020-03-02', '2020-03-03', '2020-03-04',
'2020-03-05', '2020-03-06', '2020-03-07', '2020-03-08', '2020-03-09',
'2020-03-10', '2020-03-11', '2020-03-12', '2020-03-13', '2020-03-14',
'2020-03-15'], dtype='datetime64[ns]', freq='D') # establish 10 Time series time_index02 =
pd.date_range(start="2020-03-01", periods=10) print(time_index02) # Running results :
DatetimeIndex(['2020-03-01', '2020-03-02', '2020-03-03', '2020-03-04',
'2020-03-05', '2020-03-06', '2020-03-07', '2020-03-08', '2020-03-09',
'2020-03-10'], dtype='datetime64[ns]', freq='D') # Create a time series , step 3 day time_index03
= pd.date_range(start="2020-03-01", periods=10, freq="3D") print(time_index03)
# Running results : DatetimeIndex(['2020-03-01', '2020-03-04', '2020-03-07', '2020-03-10',
'2020-03-13', '2020-03-16', '2020-03-19', '2020-03-22', '2020-03-25',
'2020-03-28'], dtype='datetime64[ns]', freq='3D') # Create a time series , In minutes
time_index04 = pd.date_range(start="2020-03-01", periods=10, freq="S")
print(time_index04) # Running results : DatetimeIndex(['2020-03-01 00:00:00', '2020-03-01
00:00:01', '2020-03-01 00:00:02', '2020-03-01 00:00:03', '2020-03-01 00:00:04',
'2020-03-01 00:00:05', '2020-03-01 00:00:06', '2020-03-01 00:00:07',
'2020-03-01 00:00:08', '2020-03-01 00:00:09'], dtype='datetime64[ns]',
freq='S') data = np.random.randint(3000, 3010, 10) print(data) # Running results : [3001
3005 3001 3009 3002 3008 3002 3000 3001 3003] time_index05 = pd.Series(data,
time_index04) print(time_index05) # Running results : 2020-03-01 00:00:00 3001 2020-03-01
00:00:01 3005 2020-03-01 00:00:02 3001 2020-03-01 00:00:03 3009 2020-03-01
00:00:04 3002 2020-03-01 00:00:05 3008 2020-03-01 00:00:06 3002 2020-03-01
00:00:07 3000 2020-03-01 00:00:08 3001 2020-03-01 00:00:09 3003 Freq: S, dtype:
int32
Technology
Daily Recommendation