|
| 1 | + |
| 2 | +Python Pandas - Date Functionality |
| 3 | + |
| 4 | + |
| 5 | +Extending the Time series, Date functionalities play major role in financial data analysis. While working with Date data, we will frequently come across the following − |
| 6 | +Generating sequence of dates |
| 7 | +Convert the date series to different frequencies |
| 8 | +Create a Range of Dates |
| 9 | +Using the date.range() function by specifying the periods and the frequency, we can create the date series. By default, the frequency of range is Days. |
| 10 | +import pandas as pd |
| 11 | +print pd.date_range('1/1/2011', periods=5) |
| 12 | +Its output is as follows − |
| 13 | +DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03', '2011-01-04', '2011-01-05'], |
| 14 | +dtype='datetime64[ns]', freq='D') |
| 15 | + |
| 16 | +#### Change the Date Frequency |
| 17 | +import pandas as pd |
| 18 | +print pd.date_range('1/1/2011', periods=5,freq='M') |
| 19 | +Its output is as follows − |
| 20 | +DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31', '2011-04-30', '2011-05-31'], |
| 21 | +dtype='datetime64[ns]', freq='M') |
| 22 | +bdate_range |
| 23 | +bdate_range() stands for business date ranges. Unlike date_range(), it excludes Saturday and Sunday. |
| 24 | +import pandas as pd |
| 25 | +print pd.date_range('1/1/2011', periods=5) |
| 26 | +Its output is as follows − |
| 27 | +DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03', '2011-01-04', '2011-01-05'], |
| 28 | +dtype='datetime64[ns]', freq='D') |
| 29 | +Observe, after 3rd March, the date jumps to 6th march excluding 4th and 5th. Just check your calendar for the days. |
| 30 | +Convenience functions like date_range and bdate_range utilize a variety of frequency aliases. The default frequency for date_range is a calendar day while the default for bdate_range is a business day. |
| 31 | +import pandas as pd |
| 32 | +start = pd.datetime(2011, 1, 1) |
| 33 | +end = pd.datetime(2011, 1, 5) |
| 34 | +print pd.date_range(start, end) |
| 35 | +Its output is as follows − |
| 36 | +DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03', '2011-01-04', '2011-01-05'], |
| 37 | +dtype='datetime64[ns]', freq='D') |
| 38 | +Offset Aliases |
| 39 | +A number of string aliases are given to useful common time series frequencies. We will refer to these aliases as offset aliases. |
| 40 | +Alias |
| 41 | +Description |
| 42 | +Alias |
| 43 | +Description |
| 44 | +B |
| 45 | +business day frequency |
| 46 | +BQS |
| 47 | +business quarter start frequency |
| 48 | +D |
| 49 | +calendar day frequency |
| 50 | +A |
| 51 | +annual(Year) end frequency |
| 52 | +W |
| 53 | +weekly frequency |
| 54 | +BA |
| 55 | +business year end frequency |
| 56 | +M |
| 57 | +month end frequency |
| 58 | +BAS |
| 59 | +business year start frequency |
| 60 | +SM |
| 61 | +semi-month end frequency |
| 62 | +BH |
| 63 | +business hour frequency |
| 64 | +BM |
| 65 | +business month end frequency |
| 66 | +H |
| 67 | +hourly frequency |
| 68 | +MS |
| 69 | +month start frequency |
| 70 | +T, min |
| 71 | +minutely frequency |
| 72 | +SMS |
| 73 | +SMS semi month start frequency |
| 74 | +S |
| 75 | +secondly frequency |
| 76 | +BMS |
| 77 | +business month start frequency |
| 78 | +L, ms |
| 79 | +milliseconds |
| 80 | +Q |
| 81 | +quarter end frequency |
| 82 | +U, us |
| 83 | +microseconds |
| 84 | +BQ |
| 85 | +business quarter end frequency |
| 86 | +N |
| 87 | +nanoseconds |
| 88 | +QS |
| 89 | +quarter start frequency |
| 90 | + |
0 commit comments