8000 TST/REF: share method tests between DataFrame and Series by jbrockmendel · Pull Request #37596 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

TST/REF: share method tests between DataFrame and Series #37596

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 8 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
TST: share between_time tests
  • Loading branch information
jbrockmendel committed Nov 2, 2020
commit 1b0dc6969bbe57c6a1e65376b9b25b96fcb7fffd
34 changes: 32 additions & 2 deletions pandas/tests/frame/methods/test_between_time.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
from datetime import time
from datetime import datetime, time

import numpy as np
import pytest

from pandas import DataFrame, date_range
from pandas._libs.tslibs import timezones

from pandas import DataFrame, Series, date_range
import pandas._testing as tm


class TestBe 8000 tweenTime:
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
def test_localized_between_time(self, tzstr, frame_or_series):
tz = timezones.maybe_get_tz(tzstr)

rng = date_range("4/16/2012", "5/1/2012", freq="H")
ts = Series(np.random.randn(len(rng)), index=rng)
if frame_or_series is DataFrame:
ts = ts.to_frame()

ts_local = ts.tz_localize(tzstr)

t1, t2 = time(10, 0), time(11, 0)
result = ts_local.between_time(t1, t2)
expected = ts.between_time(t1, t2).tz_localize(tzstr)
tm.assert_equal(result, expected)
assert timezones.tz_compare(result.index.tz, tz)

def test_between_time_types(self, frame_or_series):
# GH11818
rng = date_range("1/1/2000", "1/5/2000", freq="5min")
obj = DataFrame({"A": 0}, index=rng)
if frame_or_series is Series:
obj = obj["A"]

msg = r"Cannot convert arg \[datetime\.datetime\(2010, 1, 2, 1, 0\)\] to a time"
with pytest.raises(ValueError, match=msg):
obj.between_time(datetime(2010, 1, 2, 1), datetime(2010, 1, 2, 5))

def test_between_time(self, close_open_fixture, frame_or_series):
rng = date_range("1/1/2000", "1/5/2000", freq="5min")
ts = DataFrame(np.random.randn(len(rng), 2), index=rng)
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/indexes/datetimes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,3 +719,12 @@ def test_slice_datetime_locs(self, box, kind, tz_aware_fixture):
result = index.slice_locs(key, box(2010, 1, 2))
expected = (0, 1)
assert result == expected


class TestIndexerBetweenTime:
def test_indexer_between_time(self):
# GH#11818
rng = date_range("1/1/2000", "1/5/2000", freq="5min")
msg = r"Cannot convert arg \[datetime\.datetime\(2010, 1, 2, 1, 0\)\] to a time"
with pytest.raises(ValueError, match=msg):
rng.indexer_between_time(datetime(2010, 1, 2, 1), datetime(2010, 1, 2, 5))
34 changes: 0 additions & 34 deletions pandas/tests/series/methods/test_between_time.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,12 @@
from datetime import datetime, time

import numpy as np
import pytest

from pandas._libs.tslibs import timezones
import pandas.util._test_decorators as td

from pandas import DataFrame, Series, date_range
import pandas._testing as tm


class TestBetweenTime:
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
def test_localized_between_time(self, tzstr):
tz = timezones.maybe_get_tz(tzstr)

rng = date_range("4/16/2012", "5/1/2012", freq="H")
ts = Series(np.random.randn(len(rng)), index=rng)

ts_local = ts.tz_localize(tzstr)

t1, t2 = time(10, 0), time(11, 0)
result = ts_local.between_time(t1, t2)
expected = ts.between_time(t1, t2).tz_localize(tzstr)
tm.assert_series_equal(result, expected)
assert timezones.tz_compare(result.index.tz, tz)

def test_between_time_types(self):
# GH11818
rng = date_range("1/1/2000", "1/5/2000", freq="5min")
msg = r"Cannot convert arg \[datetime\.datetime\(2010, 1, 2, 1, 0\)\] to a time"
with pytest.raises(ValueError, match=msg):
rng.indexer_between_time(datetime(2010, 1, 2, 1), datetime(2010, 1, 2, 5))

frame = DataFrame({"A": 0}, index=rng)
with pytest.raises(ValueError, match=msg):
frame.between_time(datetime(2010, 1, 2, 1), datetime(2010, 1, 2, 5))

series = Series(0, index=rng)
with pytest.raises(ValueError, match=msg):
series.between_time(datetime(2010, 1, 2, 1), datetime(2010, 1, 2, 5))

@td.skip_if_has_locale
def test_between_time_formats(self):
# GH11818
Expand Down
0