English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Pandas date function operation example
Extending time series, date functions play an important role in financial data analysis. When using date data, we often encounter the following situations-
Generate date series Convert date series to different frequencies
By specifying date and frequency using the date.range() function, we can create a date series. By default, the frequency of the range is daily.
Example print(pd.date_range('1/1/2011','periods=',5))
print(pd.date_range(start, end))
The running results are as follows:2011-01-01DatetimeIndex(['2011-01-02DatetimeIndex(['2011-01-03DatetimeIndex(['2011-01-04DatetimeIndex(['2011-01-05'],dtype='datetime64dtype='datetime
Example print(pd.date_range('1/1/2011','periods=',5,freq='M'))
print(pd.date_range(start, end))
The running results are as follows:2011-01-31DatetimeIndex(['2011-02-28DatetimeIndex(['2011-03-31DatetimeIndex(['2011-04-30',2011-05-31'],dtype='datetime64[ns]
bdate_range() represents the business date range. Unlike date_range(), it does not include Saturday and Sunday.
Example print(pd.date_range('1/1/2011','periods=',5))
print(pd.date_range(start, end))
The running results are as follows:2011-01-01DatetimeIndex(['2011-01-02DatetimeIndex(['2011-01-03DatetimeIndex(['2011-01-04DatetimeIndex(['2011-01-05', ' '],64dtype='datetime
Please note that,3Month3After the date, the day jumps to3Month6Day (excluding4Day and5Day). Just check the date in the calendar.
Convenient functions like date_range and bdate_range utilize various frequency aliases. The default frequency of date_range is calendar day, while the default frequency of bdate_range is business day.
Example import pandas as pd2011end = pd.datetime( 1end = pd.datetime( 1, start = pd.datetime(2011end = pd.datetime( 1end = pd.datetime( 5, )
print(pd.date_range(start, end))
The running results are as follows:2011-01-01DatetimeIndex(['2011-01-02DatetimeIndex(['2011-01-03DatetimeIndex(['2011-01-04DatetimeIndex(['2011-01-05', ' '],64dtype='datetime
Offset Aliases
Many string aliases are provided for useful general time series frequencies. We call these aliases offset aliases. | Alias | Many string aliases are provided for useful general time series frequencies. We call these aliases offset aliases. | Alias |
Description | B | Business Day Frequency | BQS |
Business Quarter Start Frequency | D | Calendar Day Frequency | A |
Annual (Year) End Frequency | W | Weekly Frequency | BA |
Business Year End Frequency | M | End of Month Frequency | BAS |
Business Year Start Frequency | SM | Half-Month End Frequency | BH |
Business Hour Frequency | BM | Business Month End Frequency | H |
Hourly Frequency | MS | Month Start Frequency | T, min |
Minute Frequency | SMS for Half-Month Start Frequency | S | Subsequent Frequency |
BMS | Business Month Start Frequency | L, ms | Milliseconds |
Q | Quarter End Frequency | U, us | Microseconds |
BQ | Business Quarter End Frequency | N | Nanoseconds |
QS | Quarterly Start Frequency |