Skip to content

REF: misplaced to_datetime, date_range tests #32920

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
Show file tree
Hide file tree
Changes from all commits
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
55 changes: 55 additions & 0 deletions pandas/tests/indexes/datetimes/test_date_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pytz
from pytz import timezone

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

Expand Down Expand Up @@ -662,6 +663,60 @@ def test_negative_non_tick_frequency_descending_dates(self, tz_aware_fixture):
tm.assert_index_equal(result, expected)


class TestDateRangeTZ:
"""Tests for date_range with timezones"""

def test_hongkong_tz_convert(self):
# GH#1673 smoke test
dr = date_range("2012-01-01", "2012-01-10", freq="D", tz="Hongkong")

# it works!
dr.hour

@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
def test_date_range_span_dst_transition(self, tzstr):
# GH#1778

# Standard -> Daylight Savings Time
dr = date_range("03/06/2012 00:00", periods=200, freq="W-FRI", tz="US/Eastern")

assert (dr.hour == 0).all()

dr = date_range("2012-11-02", periods=10, tz=tzstr)
result = dr.hour
expected = pd.Index([0] * 10)
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
def test_date_range_timezone_str_argument(self, tzstr):
tz = timezones.maybe_get_tz(tzstr)
result = date_range("1/1/2000", periods=10, tz=tzstr)
expected = date_range("1/1/2000", periods=10, tz=tz)

tm.assert_index_equal(result, expected)

def test_date_range_with_fixedoffset_noname(self):
from pandas.tests.indexes.datetimes.test_timezones import fixed_off_no_name

off = fixed_off_no_name
start = datetime(2012, 3, 11, 5, 0, 0, tzinfo=off)
end = datetime(2012, 6, 11, 5, 0, 0, tzinfo=off)
rng = date_range(start=start, end=end)
assert off == rng.tz

idx = pd.Index([start, end])
assert off == idx.tz

@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
def test_date_range_with_tz(self, tzstr):
stamp = Timestamp("3/11/2012 05:00", tz=tzstr)
assert stamp.hour == 5

rng = date_range("3/11/2012 04:00", periods=10, freq="H", tz=tzstr)

assert stamp == rng[1]


class TestGenRangeGeneration:
def test_generate(self):
rng1 = list(generate_range(START, END, offset=BDay()))
Expand Down
71 changes: 0 additions & 71 deletions pandas/tests/indexes/datetimes/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -1161,74 +1161,3 @@ def test_iteration_preserves_nanoseconds(self, tz):
)
for i, ts in enumerate(index):
assert ts == index[i]


class TestDateRange:
"""Tests for date_range with timezones"""

def test_hongkong_tz_convert(self):
# GH#1673 smoke test
dr = date_range("2012-01-01", "2012-01-10", freq="D", tz="Hongkong")

# it works!
dr.hour

@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
def test_date_range_span_dst_transition(self, tzstr):
# GH#1778

# Standard -> Daylight Savings Time
dr = date_range("03/06/2012 00:00", periods=200, freq="W-FRI", tz="US/Eastern")

assert (dr.hour == 0).all()

dr = date_range("2012-11-02", periods=10, tz=tzstr)
result = dr.hour
expected = Index([0] * 10)
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
def test_date_range_timezone_str_argument(self, tzstr):
tz = timezones.maybe_get_tz(tzstr)
result = date_range("1/1/2000", periods=10, tz=tzstr)
expected = date_range("1/1/2000", periods=10, tz=tz)

tm.assert_index_equal(result, expected)

def test_date_range_with_fixedoffset_noname(self):
off = fixed_off_no_name
start = datetime(2012, 3, 11, 5, 0, 0, tzinfo=off)
end = datetime(2012, 6, 11, 5, 0, 0, tzinfo=off)
rng = date_range(start=start, end=end)
assert off == rng.tz

idx = Index([start, end])
assert off == idx.tz

@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
def test_date_range_with_tz(self, tzstr):
stamp = Timestamp("3/11/2012 05:00", tz=tzstr)
assert stamp.hour == 5

rng = date_range("3/11/2012 04:00", periods=10, freq="H", tz=tzstr)

assert stamp == rng[1]


class TestToDatetime:
"""Tests for the to_datetime constructor with timezones"""

def test_to_datetime_utc(self):
arr = np.array([dateutil.parser.parse("2012-06-13T01:39:00Z")], dtype=object)

result = to_datetime(arr, utc=True)
assert result.tz is pytz.utc

def test_to_datetime_fixed_offset(self):
dates = [
datetime(2000, 1, 1, tzinfo=fixed_off),
datetime(2000, 1, 2, tzinfo=fixed_off),
datetime(2000, 1, 3, tzinfo=fixed_off),
]
result = to_datetime(dates)
assert result.tz == fixed_off
17 changes: 17 additions & 0 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,23 @@ def test_to_datetime_with_format_out_of_bounds(self, dt_str):
with pytest.raises(OutOfBoundsDatetime):
pd.to_datetime(dt_str, format="%Y%m%d")

def test_to_datetime_utc(self):
arr = np.array([parse("2012-06-13T01:39:00Z")], dtype=object)

result = to_datetime(arr, utc=True)
assert result.tz is pytz.utc

def test_to_datetime_fixed_offset(self):
from pandas.tests.indexes.datetimes.test_timezones import fixed_off

dates = [
datetime(2000, 1, 1, tzinfo=fixed_off),
datetime(2000, 1, 2, tzinfo=fixed_off),
datetime(2000, 1, 3, tzinfo=fixed_off),
]
result = to_datetime(dates)
assert result.tz == fixed_off


class TestToDatetimeUnit:
@pytest.mark.parametrize("cache", [True, False])
Expand Down