Skip to content

Commit 4da9b15

Browse files
sinhrksjreback
authored andcommitted
BUG: Can't get period code with frequency alias 'minute' or 'Minute'
closes #11854 closes #12540
1 parent 4973c59 commit 4da9b15

File tree

5 files changed

+67
-6
lines changed

5 files changed

+67
-6
lines changed

doc/source/whatsnew/v0.18.1.txt

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ Performance Improvements
8888
Bug Fixes
8989
~~~~~~~~~
9090

91+
- Bug in ``Period`` and ``PeriodIndex`` creation raises ``KeyError`` if ``freq="Minute"`` is specified. Note that "Minute" freq is deprecated in v0.17.0, and recommended to use ``freq="T"`` instead (:issue:`11854`)
92+
9193
- Bug in ``value_counts`` when ``normalize=True`` and ``dropna=True`` where nulls still contributed to the normalized count (:issue:`12558`)
9294

9395
- Bug in ``CategoricalIndex.get_loc`` returns different result from regular ``Index`` (:issue:`12531`)

pandas/src/period.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ cdef class Period(object):
657657

658658
if isinstance(freq, compat.string_types):
659659
from pandas.tseries.frequencies import _period_alias_dict
660+
freq = freq.upper()
660661
freq = _period_alias_dict.get(freq, freq)
661662
elif isinstance(freq, (int, tuple)):
662663
from pandas.tseries.frequencies import get_freq_code as _gfc

pandas/tseries/frequencies.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ def get_period_alias(offset_str):
385385
'Min': 'T',
386386
'min': 'T',
387387
'ms': 'L',
388-
'us': 'U'
388+
'us': 'U',
389+
'ns': 'N'
389390
}
390391

391392
# TODO: Can this be killed?
@@ -683,7 +684,7 @@ def _period_alias_dictionary():
683684
alias_dict = {}
684685

685686
M_aliases = ["M", "MTH", "MONTH", "MONTHLY"]
686-
B_aliases = ["B", "BUS", "BUSINESS", "BUSINESSLY", 'WEEKDAY']
687+
B_aliases = ["B", "BUS", "BUSINESS", "BUSINESSLY", "WEEKDAY"]
687688
D_aliases = ["D", "DAY", "DLY", "DAILY"]
688689
H_aliases = ["H", "HR", "HOUR", "HRLY", "HOURLY"]
689690
T_aliases = ["T", "MIN", "MINUTE", "MINUTELY"]
@@ -705,7 +706,7 @@ def _period_alias_dictionary():
705706
alias_dict[k] = 'H'
706707

707708
for k in T_aliases:
708-
alias_dict[k] = 'Min'
709+
alias_dict[k] = 'T'
709710

710711
for k in S_aliases:
711712
alias_dict[k] = 'S'

pandas/tseries/tests/test_frequencies.py

+41
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,47 @@ def test_get_rule_month():
170170
assert (result == 'MAY')
171171

172172

173+
def test_period_str_to_code():
174+
assert (frequencies._period_str_to_code('A') == 1000)
175+
assert (frequencies._period_str_to_code('A-DEC') == 1000)
176+
assert (frequencies._period_str_to_code('A-JAN') == 1001)
177+
assert (frequencies._period_str_to_code('Q') == 2000)
178+
assert (frequencies._period_str_to_code('Q-DEC') == 2000)
179+
assert (frequencies._period_str_to_code('Q-FEB') == 2002)
180+
181+
def _assert_depr(freq, expected, aliases):
182+
assert isinstance(aliases, list)
183+
assert (frequencies._period_str_to_code(freq) == expected)
184+
185+
for alias in aliases:
186+
with tm.assert_produces_warning(FutureWarning,
187+
check_stacklevel=False):
188+
assert (frequencies._period_str_to_code(alias) == expected)
189+
190+
_assert_depr("M", 3000, ["MTH", "MONTH", "MONTHLY"])
191+
192+
assert (frequencies._period_str_to_code('W') == 4000)
193+
assert (frequencies._period_str_to_code('W-SUN') == 4000)
194+
assert (frequencies._period_str_to_code('W-FRI') == 4005)
195+
196+
_assert_depr("B", 5000, ["BUS", "BUSINESS", "BUSINESSLY", "WEEKDAY"])
197+
_assert_depr("D", 6000, ["DAY", "DLY", "DAILY"])
198+
_assert_depr("H", 7000, ["HR", "HOUR", "HRLY", "HOURLY"])
199+
200+
_assert_depr("T", 8000, ["minute", "MINUTE", "MINUTELY"])
201+
assert (frequencies._period_str_to_code('Min') == 8000)
202+
203+
_assert_depr("S", 9000, ["sec", "SEC", "SECOND", "SECONDLY"])
204+
_assert_depr("L", 10000, ["MILLISECOND", "MILLISECONDLY"])
205+
assert (frequencies._period_str_to_code('ms') == 10000)
206+
207+
_assert_depr("U", 11000, ["MICROSECOND", "MICROSECONDLY"])
208+
assert (frequencies._period_str_to_code('US') == 11000)
209+
210+
_assert_depr("N", 12000, ["NANOSECOND", "NANOSECONDLY"])
211+
assert (frequencies._period_str_to_code('NS') == 12000)
212+
213+
173214
class TestFrequencyCode(tm.TestCase):
174215
def test_freq_code(self):
175216
self.assertEqual(frequencies.get_freq('A'), 1000)

pandas/tseries/tests/test_period.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import pandas as pd
2323
import numpy as np
2424
from numpy.random import randn
25-
from pandas.compat import range, lrange, lmap, zip, text_type, PY3
25+
from pandas.compat import range, lrange, lmap, zip, text_type, PY3, iteritems
2626
from pandas.compat.numpy_compat import np_datetime64_compat
2727

2828
from pandas import (Series, DataFrame,
@@ -36,8 +36,6 @@
3636
class TestPeriodProperties(tm.TestCase):
3737
"Test properties such as year, month, weekday, etc...."
3838

39-
#
40-
4139
def test_quarterly_negative_ordinals(self):
4240
p = Period(ordinal=-1, freq='Q-DEC')
4341
self.assertEqual(p.year, 1969)
@@ -440,6 +438,24 @@ def test_freq_str(self):
440438
self.assertEqual(i1.freq, offsets.Minute())
441439
self.assertEqual(i1.freqstr, 'T')
442440

441+
def test_period_deprecated_freq(self):
442+
cases = {"M": ["MTH", "MONTH", "MONTHLY", "Mth", "month", "monthly"],
443+
"B": ["BUS", "BUSINESS", "BUSINESSLY", "WEEKDAY", "bus"],
444+
"D": ["DAY", "DLY", "DAILY", "Day", "Dly", "Daily"],
445+
"H": ["HR", "HOUR", "HRLY", "HOURLY", "hr", "Hour", "HRly"],
446+
"T": ["minute", "MINUTE", "MINUTELY", "minutely"],
447+
"S": ["sec", "SEC", "SECOND", "SECONDLY", "second"],
448+
"L": ["MILLISECOND", "MILLISECONDLY", "millisecond"],
449+
"U": ["MICROSECOND", "MICROSECONDLY", "microsecond"],
450+
"N": ["NANOSECOND", "NANOSECONDLY", "nanosecond"]}
451+
for exp, freqs in iteritems(cases):
452+
for freq in freqs:
453+
454+
with tm.assert_produces_warning(FutureWarning,
455+
check_stacklevel=False):
456+
res = pd.Period('2016-03-01 09:00', freq=freq)
457+
self.assertEqual(res, Period('2016-03-01 09:00', freq=exp))
458+
443459
def test_repr(self):
444460
p = Period('Jan-2000')
445461
self.assertIn('2000-01', repr(p))

0 commit comments

Comments
 (0)