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:

start: Start time, which can be a string or a value of type datetime. Default None. end: End time, which can be a string or a value of type datetime. If the length is specified, that is, periods, it can not be set. Default None. periods: Length of time series, integer type. If there is end, it can not be set. Default None. freq: Frequency of timing generation, that is, how many times a timing point is generated. String type or DateOffset type. The default is' D ', that is, day granularity, as shown in the above code output. tz: Time zone, string type. Default None. normalize: bool type, never used, don't know what to do. name: Sets the name of the timing, string type, default None. closed: Whether to include both sides of the value. The default is None, that is, both sides are reserved.

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:

months, set month. days, set days. years, set year. hours, set the hour. minutes, set minutes. seconds, set the second.

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')

Related articles: