Skip to content

Commit bcf7e73

Browse files
committed
test_between_time parametrization simplified, raises test created
1 parent ddbb24a commit bcf7e73

File tree

3 files changed

+42
-40
lines changed

3 files changed

+42
-40
lines changed

doc/source/whatsnew/v0.24.0.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Other Enhancements
8484
- :meth:`Series.nlargest`, :meth:`Series.nsmallest`, :meth:`DataFrame.nlargest`, and :meth:`DataFrame.nsmallest` now accept the value ``"all"`` for the ``keep` argument. This keeps all ties for the nth largest/smallest value (:issue:`16818`)
8585
- :class:`IntervalIndex` has gained the :meth:`~IntervalIndex.set_closed` method to change the existing ``closed`` value (:issue:`21670`)
8686
- :func:`~DataFrame.to_csv` and :func:`~DataFrame.to_json` now support ``compression='infer'`` to infer compression based on filename (:issue:`15008`)
87-
- :func:`between_time` and :func:`at_time` now support an axis parameter (:issue: `8839`)
87+
- :func:`between_time` and :func:`at_time` now support an ``axis`` parameter (:issue: `8839`)
8888
-
8989

9090
.. _whatsnew_0240.api_breaking:

pandas/core/generic.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6809,13 +6809,14 @@ def at_time(self, time, asof=False, axis=None):
68096809
axis = self._stat_axis_number
68106810
axis = self._get_axis_number(axis)
68116811

6812+
index = self._get_axis(axis)
68126813
try:
6813-
index = self._get_axis(axis)
68146814
indexer = index.indexer_at_time(time, asof=asof)
6815-
return self._take(indexer, axis=axis)
68166815
except AttributeError:
68176816
raise TypeError('Index must be DatetimeIndex')
68186817

6818+
return self._take(indexer, axis=axis)
6819+
68196820
def between_time(self, start_time, end_time, include_start=True,
68206821
include_end=True, axis=None):
68216822
"""
@@ -6879,15 +6880,16 @@ def between_time(self, start_time, end_time, include_start=True,
68796880
axis = self._stat_axis_number
68806881
axis = self._get_axis_number(axis)
68816882

6883+
index = self._get_axis(axis)
68826884
try:
6883-
index = self._get_axis(axis)
68846885
indexer = index.indexer_between_time(
68856886
start_time, end_time, include_start=include_start,
68866887
include_end=include_end)
6887-
return self._take(indexer, axis=axis)
68886888
except AttributeError:
68896889
raise TypeError('Index must be DatetimeIndex')
68906890

6891+
return self._take(indexer, axis=axis)
6892+
68916893
def resample(self, rule, how=None, axis=0, fill_method=None, closed=None,
68926894
label=None, convention='start', kind=None, loffset=None,
68936895
limit=None, base=0, on=None, level=None):

pandas/tests/frame/test_timeseries.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -640,26 +640,22 @@ def test_at_time_raises(self):
640640
with pytest.raises(TypeError): # index is not a DatetimeIndex
641641
df.at_time('00:00')
642642

643-
@pytest.mark.parametrize('time_axis', [
644-
(False, False), (True, False), (False, True), (True, True)])
645-
def test_at_time_axis(self, time_axis):
643+
@pytest.mark.parametrize('axis', ['index', 'columns'])
644+
def test_at_time_axis(self, axis):
646645
# issue 8839
647646
rng = date_range('1/1/2000', '1/5/2000', freq='5min')
648647
ts = DataFrame(np.random.randn(len(rng), len(rng)))
648+
ts.index, ts.columns = rng, rng
649649

650650
indices = rng[(rng.hour == 9) & (rng.minute == 30) & (rng.second == 0)]
651651

652-
if time_axis[0]:
653-
ts.index = rng
654-
expected = ts.loc[indices]
655-
result = ts.at_time('9:30', axis=0)
656-
assert_frame_equal(result, expected)
657-
658-
if time_axis[1]:
659-
ts.columns = rng
652+
if axis == 'index':
653+
expected = ts.loc[indices, :]
654+
elif axis == 'columns':
660655
expected = ts.loc[:, indices]
661-
result = ts.at_time('9:30', axis=1)
662-
assert_frame_equal(result, expected)
656+
657+
result = ts.at_time('9:30', axis=axis)
658+
assert_frame_equal(result, expected)
663659

664660
def test_between_time(self):
665661
rng = date_range('1/1/2000', '1/5/2000', freq='5min')
@@ -727,38 +723,42 @@ def test_between_time_raises(self):
727723
with pytest.raises(TypeError): # index is not a DatetimeIndex
728724
df.between_time(start_time='00:00', end_time='12:00')
729725

730-
@pytest.mark.parametrize('time_axis', [
731-
(False, False), (True, False), (False, True), (True, True)])
732-
def test_between_time_axis(self, time_axis):
726+
@pytest.mark.parametrize('axis', [
727+
(), 'index', 'columns', ('index', 'columns')])
728+
def test_between_time_axis(self, axis):
733729
# issue 8839
734730
rng = date_range('1/1/2000', periods=100, freq='10min')
735-
blank = np.arange(0, len(rng))
731+
ts = DataFrame(np.random.randn(len(rng), len(rng)))
736732
stime, etime = ('08:00:00', '09:00:00')
737-
rand_data = np.random.randn(len(rng), len(rng))
738733
exp_len = 7
739734

740-
if time_axis[0]:
741-
index = rng
742-
else:
743-
index = blank
744-
if time_axis[1]:
745-
col = rng
746-
else:
747-
col = blank
748-
749-
ts = DataFrame(rand_data, index=index, columns=col)
750-
751-
if time_axis[0]:
735+
if 'index' in axis:
736+
ts.index = rng
752737
assert len(ts.between_time(stime, etime)) == exp_len
753738
assert len(ts.between_time(stime, etime, axis=0)) == exp_len
754-
else:
755-
pytest.raises(TypeError, ts.between_time, stime, etime)
756-
pytest.raises(TypeError, ts.between_time, stime, etime, axis=0)
757739

758-
if time_axis[1]:
740+
if 'columns' in axis:
741+
ts.columns = rng
759742
selected = ts.between_time(stime, etime, axis=1).columns
760743
assert len(selected) == exp_len
761-
else:
744+
745+
@pytest.mark.parametrize('axis', [
746+
(), 'index', 'columns', ('index', 'columns')])
747+
def test_between_time_axis_raises(self, axis):
748+
# issue 8839
749+
rng = date_range('1/1/2000', periods=100, freq='10min')
750+
mask = np.arange(0, len(rng))
751+
rand_data = np.random.randn(len(rng), len(rng))
752+
ts = DataFrame(rand_data, index=rng, columns=rng)
753+
stime, etime = ('08:00:00', '09:00:00')
754+
755+
if 'index' not in axis:
756+
ts.index = mask
757+
pytest.raises(TypeError, ts.between_time, stime, etime)
758+
pytest.raises(TypeError, ts.between_time, stime, etime, axis=0)
759+
760+
if 'columns' not in axis:
761+
ts.columns = mask
762762
pytest.raises(TypeError, ts.between_time, stime, etime, axis=1)
763763

764764
def test_operation_on_NaT(self):

0 commit comments

Comments
 (0)