-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Changes from 1 commit
b139cb1
51b7e61
adf543d
bc83f9d
ad41871
5ea87f4
4ab35ee
35b4b41
2172bc9
3a9550e
9ae360f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') | ||
|
@@ -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', [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same this is very hard to read There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') | ||
|
There was a problem hiding this comment.
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