python pandas Sequence Processing Related Functions Detailed Explanation
- 2021-07-06 11:29:53
- OfStack
Create Time Series
Function pd.date_range ()
According to the specified range, a time series DatetimeIndex is generated, and the type of each element is Timestamp. This function is widely used.
ts = pd.date_range('2017-09-01', periods=10, freq='d', normalize=False)
ts
The output is:
DatetimeIndex(['2017-09-01', '2017-09-02', '2017-09-03', '2017-09-04',
'2017-09-05', '2017-09-06', '2017-09-07', '2017-09-08',
'2017-09-09', '2017-09-10'],
dtype='datetime64[ns]', freq='D'
Main input parameter analysis:
Among them, the value of freq can be the following symbol to indicate the interval, and can combine symbols and numbers, such as' 3d ', indicating that one time point is recorded every three days. Both case and case are fine.
B business day frequency
C custom business day frequency (experimental)
D calendar day frequency
W weekly frequency
M month end frequency
SM semi-month end frequency (15th and end of month)
BM business month end frequency
CBM custom business month end frequency
MS month start frequency
SMS semi-month start frequency (1st and 15th)
BMS business month start frequency
CBMS custom business month start frequency
Q quarter end frequency
BQ business quarter endfrequency
QS quarter start frequency
BQS business quarter start frequency
A year end frequency
BA business year end frequency
AS year start frequency
BAS business year start frequency
BH business hour frequency
H hourly frequency
T, min minutely frequency
S secondly frequency
L, ms milliseconds
U, us microseconds
N nanoseconds
String to timestamp
The pd. to_datetime () function converts a string representing a time to the bit TimeStamp.
pd.to_datetime('2017-09-01')
The output is:
Timestamp('2017-09-01 00:00:00')
Common parameters:
format: Used to format strings, as shown above by default.
Addition and subtraction of timestamp
Sometimes you need to increase or decrease the time, and you can use the type: DateOffset.
pd.to_datetime('2017-09-01') + pd.DateOffset(days=10)
The output is:
Timestamp('2017-09-11 00:00:00')
Common parameters for DateOffset:
The above can be set at the same time and used in combination.
pd.to_datetime('2017-09-01') + pd.DateOffset(seconds=10, days = 10)
The output is:
Timestamp('2017-09-11 00:00:10')