8000 ENH: between_time, at_time accept axis parameter by yrhooke · Pull Request #21799 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

ENH: between_time, at_time accept axis parameter #21799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 19, 2018
Merged
Prev Previous commit
Next Next commit
added version/issue info for 8839
  • Loading branch information
yrhooke committed Aug 9, 2018
commit adf543d652cdcb72f3310eb3c1d8dc51a5264dab
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ Other Enhancements
- :func:`to_timedelta` now supports iso-formated timedelta strings (:issue:`21877`)
- :class:`Series` and :class:`DataFrame` now support :class:`Iterable` in constructor (:issue:`2193`)
- :class:`DatetimeIndex` gained :attr:`DatetimeIndex.timetz` attribute. Returns local time with timezone information. (:issue:`21358`)
- :func:`between_time` and :func:`at_time` now support an axis parameter (:issue: `8839`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use double backticks around axis

-

.. _whatsnew_0240.api_breaking:

Expand Down
5 changes: 5 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7027,6 +7027,9 @@ def at_time(self, time, asof=False, axis=None):
time : datetime.time or string
axis : int or string axis name, optional
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a versionadded 0.24.0 here (and other new kwargs)


.. versionadded:: 0.24.0


Returns
-------
values_at_time : same type as caller
Expand Down Expand Up @@ -7087,6 +7090,8 @@ def between_time(self, start_time, end_time, include_start=True,
include_end : boolean, default True
axis : int or string axis name, optional
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same


.. versionadded:: 0.24.0

Returns
-------
values_between_time : same type as caller
Expand Down
78 changes: 43 additions & 35 deletions pandas/tests/frame/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,21 +640,26 @@ def test_at_time_raises(self):
with pytest.raises(TypeError): # index is not a DatetimeIndex
df.at_time('00:00')

def test_at_time_axis(self):
@pytest.mark.parametrize('time_axis', [
(False, False), (True, False), (False, True), (True, True)])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm this is an odd way to parameterize, can you just use axis as the argument?

def test_at_time_axis(self, time_axis):
# issue 8839
rng = date_range('1/1/2000', '1/5/2000', freq='5min')
ts = DataFrame(np.random.randn(len(rng), len(rng)))

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

ts.index = rng
expected = ts.loc[indices]
result = ts.at_time('9:30', axis=0)
assert_frame_equal(result, expected)
if time_axis[0]:
ts.index = rng
expected = ts.loc[indices]
result = ts.at_time('9:30', axis=0)
assert_frame_equal(result, expected)

ts.columns = rng
expected = ts.loc[:, indices]
result = ts.at_time('9:30', axis=1)
assert_frame_equal(result, expected)
if time_axis[1]:
ts.columns = rng
expected = ts.loc[:, indices]
result = ts.at_time('9:30', axis=1)
assert_frame_equal(result, expected)

def test_between_time(self):
rng = date_range('1/1/2000', '1/5/2000', freq='5min')
Expand Down Expand Up @@ -722,36 +727,39 @@ def test_between_time_raises(self):
with pytest.raises(TypeError): # index is not a DatetimeIndex
df.between_time(start_time='00:00', end_time='12:00')

def test_between_time_axis(self):
@pytest.mark.parametrize('time_axis', [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same this is very hard to read

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can make the error case a separate test, which makes both tests much simpler

(False, False), (True, False), (False, True), (True, True)])
def test_between_time_axis(self, time_axis):
# issue 8839
rng = date_range('1/1/2000', periods=100, freq='10min')
blank = np.arange(0, len(rng))
stime, etime = ('08:00:00', '09:00:00')
dimn = (len(rng), len(rng))
rand_data = np.random.randn(len(rng), len(rng))
exp_len = 7
for time_index, time_col in product([True, False], [True, False]):
if time_index:
index = rng
else:
index = blank
if time_col:
col = rng
else:
col = blank

ts = DataFrame(np.random.randn(*dimn), index=index, columns=col)

if time_index:
assert len(ts.between_time(stime, etime)) == exp_len
assert len(ts.between_time(stime, etime, axis=0)) == exp_len
else:
pytest.raises(TypeError, ts.between_time, stime, etime)
pytest.raises(TypeError, ts.between_time, stime, etime, axis=0)

if time_col:
selected = ts.between_time(stime, etime, axis=1).columns
assert len(selected) == exp_len
else:
pytest.raises(TypeError, ts.between_time, stime, etime, axis=1)

if time_axis[0]:
index = rng
else:
index = blank
if time_axis[1]:
col = rng
else:
col = blank

ts = DataFrame(rand_data, index=index, columns=col)

if time_axis[0]:
assert len(ts.between_time(stime, etime)) == exp_len
assert len(ts.between_time(stime, etime, axis=0)) == exp_len
else:
pytest.raises(TypeError, ts.between_time, stime, etime)
pytest.raises(TypeError, ts.between_time, stime, etime, axis=0)

if time_axis[1]:
selected = ts.between_time(stime, etime, axis=1).columns
assert len(selected) == exp_len
else:
pytest.raises(TypeError, ts.between_time, stime, etime, axis=1)

def test_operation_on_NaT(self):
# Both NaT and Timestamp are in DataFrame.
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/series/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@ def test_between_time_formats(self):
assert len(ts.between_time(*time_string)) == expected_length

def test_between_time_axis(self):
# issue 8839
rng = date_range('1/1/2000', periods=100, freq='10min')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add the issue number as a comment (to all new tests)

ts = Series(np.random.randn(len(rng)), index=rng)
stime, etime = ('08:00:00', '09:00:00')
Expand Down
0