Skip to content

Commit 34cecd8

Browse files
committed
Merge pull request pandas-dev#7803 from sinhrks/superperiod
BUG: is_superperiod and is_subperiod cannot handle higher freq than S
2 parents 1ee90a5 + 7df3eb6 commit 34cecd8

File tree

4 files changed

+65
-20
lines changed

4 files changed

+65
-20
lines changed

doc/source/v0.15.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ Bug Fixes
193193

194194

195195

196+
- Bug in ``is_superperiod`` and ``is_subperiod`` cannot handle higher frequencies than ``S`` (:issue:`7760`, :issue:`7772`, :issue:`7803`)
196197

197198
- Bug in ``DataFrame.reset_index`` which has ``MultiIndex`` contains ``PeriodIndex`` or ``DatetimeIndex`` with tz raises ``ValueError`` (:issue:`7746`, :issue:`7793`)
198199

pandas/tseries/frequencies.py

+32-20
Original file line numberDiff line numberDiff line change
@@ -929,25 +929,31 @@ def is_subperiod(source, target):
929929
if _is_quarterly(source):
930930
return _quarter_months_conform(_get_rule_month(source),
931931
_get_rule_month(target))
932-
return source in ['D', 'C', 'B', 'M', 'H', 'T', 'S']
932+
return source in ['D', 'C', 'B', 'M', 'H', 'T', 'S', 'L', 'U', 'N']
933933
elif _is_quarterly(target):
934-
return source in ['D', 'C', 'B', 'M', 'H', 'T', 'S']
934+
return source in ['D', 'C', 'B', 'M', 'H', 'T', 'S', 'L', 'U', 'N']
935935
elif target == 'M':
936-
return source in ['D', 'C', 'B', 'H', 'T', 'S']
936+
return source in ['D', 'C', 'B', 'H', 'T', 'S', 'L', 'U', 'N']
937937
elif _is_weekly(target):
938-
return source in [target, 'D', 'C', 'B', 'H', 'T', 'S']
938+
return source in [target, 'D', 'C', 'B', 'H', 'T', 'S', 'L', 'U', 'N']
939939
elif target == 'B':
940-
return source in ['B', 'H', 'T', 'S']
940+
return source in ['B', 'H', 'T', 'S', 'L', 'U', 'N']
941941
elif target == 'C':
942-
return source in ['C', 'H', 'T', 'S']
942+
return source in ['C', 'H', 'T', 'S', 'L', 'U', 'N']
943943
elif target == 'D':
944-
return source in ['D', 'H', 'T', 'S']
944+
return source in ['D', 'H', 'T', 'S', 'L', 'U', 'N']
945945
elif target == 'H':
946-
return source in ['H', 'T', 'S']
946+
return source in ['H', 'T', 'S', 'L', 'U', 'N']
947947
elif target == 'T':
948-
return source in ['T', 'S']
948+
return source in ['T', 'S', 'L', 'U', 'N']
949949
elif target == 'S':
950-
return source in ['S']
950+
return source in ['S', 'L', 'U', 'N']
951+
elif target == 'L':
952+
return source in ['L', 'U', 'N']
953+
elif target == 'U':
954+
return source in ['U', 'N']
955+
elif target == 'N':
956+
return source in ['N']
951957

952958

953959
def is_superperiod(source, target):
@@ -982,25 +988,31 @@ def is_superperiod(source, target):
982988
smonth = _get_rule_month(source)
983989
tmonth = _get_rule_month(target)
984990
return _quarter_months_conform(smonth, tmonth)
985-
return target in ['D', 'C', 'B', 'M', 'H', 'T', 'S']
991+
return target in ['D', 'C', 'B', 'M', 'H', 'T', 'S', 'L', 'U', 'N']
986992
elif _is_quarterly(source):
987-
return target in ['D', 'C', 'B', 'M', 'H', 'T', 'S']
993+
return target in ['D', 'C', 'B', 'M', 'H', 'T', 'S', 'L', 'U', 'N']
988994
elif source == 'M':
989-
return target in ['D', 'C', 'B', 'H', 'T', 'S']
995+
return target in ['D', 'C', 'B', 'H', 'T', 'S', 'L', 'U', 'N']
990996
elif _is_weekly(source):
991-
return target in [source, 'D', 'C', 'B', 'H', 'T', 'S']
997+
return target in [source, 'D', 'C', 'B', 'H', 'T', 'S', 'L', 'U', 'N']
992998
elif source == 'B':
993-
return target in ['D', 'C', 'B', 'H', 'T', 'S']
999+
return target in ['D', 'C', 'B', 'H', 'T', 'S', 'L', 'U', 'N']
9941000
elif source == 'C':
995-
return target in ['D', 'C', 'B', 'H', 'T', 'S']
1001+
return target in ['D', 'C', 'B', 'H', 'T', 'S', 'L', 'U', 'N']
9961002
elif source == 'D':
997-
return target in ['D', 'C', 'B', 'H', 'T', 'S']
1003+
return target in ['D', 'C', 'B', 'H', 'T', 'S', 'L', 'U', 'N']
9981004
elif source == 'H':
999-
return target in ['H', 'T', 'S']
1005+
return target in ['H', 'T', 'S', 'L', 'U', 'N']
10001006
elif source == 'T':
1001-
return target in ['T', 'S']
1007+
return target in ['T', 'S', 'L', 'U', 'N']
10021008
elif source == 'S':
1003-
return target in ['S']
1009+
return target in ['S', 'L', 'U', 'N']
1010+
elif source == 'L':
1011+
return target in ['L', 'U', 'N']
1012+
elif source == 'U':
1013+
return target in ['U', 'N']
1014+
elif source == 'N':
1015+
return target in ['N']
10041016

10051017

10061018
def _get_rule_month(source, default='DEC'):

pandas/tseries/tests/test_frequencies.py

+10
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,16 @@ def test_is_superperiod_subperiod():
342342
assert(fmod.is_superperiod(offsets.Hour(), offsets.Minute()))
343343
assert(fmod.is_subperiod(offsets.Minute(), offsets.Hour()))
344344

345+
assert(fmod.is_superperiod(offsets.Second(), offsets.Milli()))
346+
assert(fmod.is_subperiod(offsets.Milli(), offsets.Second()))
347+
348+
assert(fmod.is_superperiod(offsets.Milli(), offsets.Micro()))
349+
assert(fmod.is_subperiod(offsets.Micro(), offsets.Milli()))
350+
351+
assert(fmod.is_superperiod(offsets.Micro(), offsets.Nano()))
352+
assert(fmod.is_subperiod(offsets.Nano(), offsets.Micro()))
353+
354+
345355
if __name__ == '__main__':
346356
import nose
347357
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],

pandas/tseries/tests/test_plotting.py

+22
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,28 @@ def test_from_weekly_resampling(self):
707707
for l in ax.get_lines():
708708
self.assertTrue(PeriodIndex(data=l.get_xdata()).freq.startswith('W'))
709709

710+
@slow
711+
def test_mixed_freq_second_millisecond(self):
712+
# GH 7772, GH 7760
713+
idxh = date_range('2014-07-01 09:00', freq='S', periods=50)
714+
idxl = date_range('2014-07-01 09:00', freq='100L', periods=500)
715+
high = Series(np.random.randn(len(idxh)), idxh)
716+
low = Series(np.random.randn(len(idxl)), idxl)
717+
# high to low
718+
high.plot()
719+
ax = low.plot()
720+
self.assertEqual(len(ax.get_lines()), 2)
721+
for l in ax.get_lines():
722+
self.assertEqual(PeriodIndex(data=l.get_xdata()).freq, 'L')
723+
tm.close()
724+
725+
# low to high
726+
low.plot()
727+
ax = high.plot()
728+
self.assertEqual(len(ax.get_lines()), 2)
729+
for l in ax.get_lines():
730+
self.assertEqual(PeriodIndex(data=l.get_xdata()).freq, 'L')
731+
710732
@slow
711733
def test_irreg_dtypes(self):
712734
# date

0 commit comments

Comments
 (0)