From 80bc2d572ebe1e0d61b17eebeef701cdfdef4d99 Mon Sep 17 00:00:00 2001 From: gfyoung Date: Sun, 16 Jul 2017 10:31:58 -0700 Subject: [PATCH] TST, MAINT: Add missing components to PR --- doc/source/timeseries.rst | 4 +- doc/source/whatsnew/v0.21.0.txt | 2 + .../indexes/datetimes/test_date_range.py | 24 ++++++++++ pandas/tseries/frequencies.py | 45 +++++++++---------- 4 files changed, 49 insertions(+), 26 deletions(-) diff --git a/doc/source/timeseries.rst b/doc/source/timeseries.rst index 1dd80aec4fd6c7..8f02a86adbd482 100644 --- a/doc/source/timeseries.rst +++ b/doc/source/timeseries.rst @@ -1092,9 +1092,9 @@ frequencies. We will refer to these aliases as *offset aliases* "BQ", "business quarter endfrequency" "QS", "quarter start frequency" "BQS", "business quarter start frequency" - "A", "year end frequency" + "A, Y", "year end frequency" "BA", "business year end frequency" - "AS", "year start frequency" + "AS, YS", "year start frequency" "BAS", "business year start frequency" "BH", "business hour frequency" "H", "hourly frequency" diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 7c52cf6f450b2a..13094b9cdd9006 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -40,6 +40,8 @@ Other Enhancements - :func:`DataFrame.clip()` and :func:`Series.clip()` have gained an ``inplace`` argument. (:issue:`15388`) - :func:`crosstab` has gained a ``margins_name`` parameter to define the name of the row / column that will contain the totals when ``margins=True``. (:issue:`15972`) - :func:`DataFrame.select_dtypes` now accepts scalar values for include/exclude as well as list-like. (:issue:`16855`) +- :func:`date_range` now accepts 'YS' in addition to 'AS' as an alias for start of year (:issue:`9313`) +- :func:`date_range` now accepts 'Y' in addition to 'A' as an alias for end of year (:issue:`9313`) .. _whatsnew_0210.api_breaking: diff --git a/pandas/tests/indexes/datetimes/test_date_range.py b/pandas/tests/indexes/datetimes/test_date_range.py index 62686b356dc302..76d24426f957bf 100644 --- a/pandas/tests/indexes/datetimes/test_date_range.py +++ b/pandas/tests/indexes/datetimes/test_date_range.py @@ -33,6 +33,30 @@ def test_date_range_gen_error(self): rng = date_range('1/1/2000 00:00', '1/1/2000 00:18', freq='5min') assert len(rng) == 4 + @pytest.mark.parametrize("freq", ["AS", "YS"]) + def test_begin_year_alias(self, freq): + # see gh-9313 + rng = date_range("1/1/2013", "7/1/2017", freq=freq) + exp = pd.DatetimeIndex(["2013-01-01", "2014-01-01", + "2015-01-01", "2016-01-01"], freq=freq) + tm.assert_index_equal(rng, exp) + + @pytest.mark.parametrize("freq", ["A", "Y"]) + def test_end_year_alias(self, freq): + # see gh-9313 + rng = date_range("1/1/2013", "7/1/2017", freq=freq) + exp = pd.DatetimeIndex(["2013-12-31", "2014-12-31", + "2015-12-31", "2016-12-31"], freq=freq) + tm.assert_index_equal(rng, exp) + + @pytest.mark.parametrize("freq", ["BA", "BY"]) + def test_business_end_year_alias(self, freq): + # see gh-9313 + rng = date_range("1/1/2013", "7/1/2017", freq=freq) + exp = pd.DatetimeIndex(["2013-12-31", "2014-12-31", + "2015-12-31", "2016-12-30"], freq=freq) + tm.assert_index_equal(rng, exp) + def test_date_range_negative_freq(self): # GH 11018 rng = date_range('2011-12-31', freq='-2A', periods=3) diff --git a/pandas/tseries/frequencies.py b/pandas/tseries/frequencies.py index 5c3c90520d1c37..887ebc7adac2bc 100644 --- a/pandas/tseries/frequencies.py +++ b/pandas/tseries/frequencies.py @@ -399,10 +399,14 @@ def _get_freq_str(base, mult=1): 'Q': 'Q', 'A': 'A', 'W': 'W', - 'M': 'M' + 'M': 'M', + 'Y': 'A', + 'BY': 'A', + 'YS': 'A', + 'BYS': 'A', } -need_suffix = ['QS', 'BQ', 'BQS', 'AS', 'BA', 'BAS'] +need_suffix = ['QS', 'BQ', 'BQS', 'YS', 'AS', 'BY', 'BA', 'BYS', 'BAS'] for __prefix in need_suffix: for _m in tslib._MONTHS: _offset_to_period_map['%s-%s' % (__prefix, _m)] = \ @@ -422,35 +426,18 @@ def get_period_alias(offset_str): return _offset_to_period_map.get(offset_str, None) -_pure_alias = { - # 'A' is equivalent to 'Y'. - 'Y': 'A', - 'YS': 'AS', - 'BY': 'BA', - 'BYS': 'BAS', - 'Y-DEC': 'A-DEC', - 'Y-JAN': 'A-JAN', - 'Y-FEB': 'A-FEB', - 'Y-MAR': 'A-MAR', - 'Y-APR': 'A-APR', - 'Y-MAY': 'A-MAY', - 'Y-JUN': 'A-JUN', - 'Y-JUL': 'A-JUL', - 'Y-AUG': 'A-AUG', - 'Y-SEP': 'A-SEP', - 'Y-OCT': 'A-OCT', - 'Y-NOV': 'A-NOV', -} - - _lite_rule_alias = { 'W': 'W-SUN', 'Q': 'Q-DEC', 'A': 'A-DEC', # YearEnd(month=12), + 'Y': 'A-DEC', 'AS': 'AS-JAN', # YearBegin(month=1), + 'YS': 'AS-JAN', 'BA': 'BA-DEC', # BYearEnd(month=12), + 'BY': 'BA-DEC', 'BAS': 'BAS-JAN', # BYearBegin(month=1), + 'BYS': 'BAS-JAN', 'Min': 'T', 'min': 'T', @@ -730,16 +717,26 @@ def get_standard_freq(freq): _reverse_period_code_map[_v] = _k # Additional aliases +year_aliases = {} + +for k, v in compat.iteritems(_period_code_map): + if k.startswith("A-"): + alias = "Y" + k[1:] + year_aliases[alias] = v + +_period_code_map.update(**year_aliases) +del year_aliases + _period_code_map.update({ "Q": 2000, # Quarterly - December year end (default quarterly) "A": 1000, # Annual + "Y": 1000, "W": 4000, # Weekly "C": 5000, # Custom Business Day }) def _period_str_to_code(freqstr): - freqstr = _pure_alias.get(freqstr, freqstr) freqstr = _lite_rule_alias.get(freqstr, freqstr) if freqstr not in _dont_uppercase: