Skip to content
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

REF: Misplaced DatetimeIndex tests #55614

Merged
merged 12 commits into from
Oct 22, 2023
19 changes: 19 additions & 0 deletions pandas/tests/indexes/datetimes/methods/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@


class TestDatetimeIndex:
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
def test_dti_astype_asobject_around_dst_transition(self, tzstr):
# GH#1345

# dates around a dst transition
rng = date_range("2/13/2010", "5/6/2010", tz=tzstr)

objs = rng.astype(object)
for i, x in enumerate(objs):
exval = rng[i]
assert x == exval
assert x.tzinfo == exval.tzinfo

objs = rng.astype(object)
for i, x in enumerate(objs):
exval = rng[i]
assert x == exval
assert x.tzinfo == exval.tzinfo

def test_astype(self):
# GH 13149, GH 13209
idx = DatetimeIndex(["2016-05-16", "NaT", NaT, np.nan], name="idx")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,29 @@ def test_delete(self):
# either depending on numpy version
idx.delete(5)

for tz in [None, "Asia/Tokyo", "US/Pacific"]:
idx = date_range(
start="2000-01-01 09:00", periods=10, freq="h", name="idx", tz=tz
)

expected = date_range(
start="2000-01-01 10:00", periods=9, freq="h", name="idx", tz=tz
)
result = idx.delete(0)
tm.assert_index_equal(result, expected)
assert result.name == expected.name
assert result.freqstr == "h"
assert result.tz == expected.tz
@pytest.mark.parametrize("tz", [None, "Asia/Tokyo", "US/Pacific"])
def test_delete2(self, tz):
idx = date_range(
start="2000-01-01 09:00", periods=10, freq="h", name="idx", tz=tz
)

expected = date_range(
start="2000-01-01 09:00", periods=9, freq="h", name="idx", tz=tz
)
result = idx.delete(-1)
tm.assert_index_equal(result, expected)
assert result.name == expected.name
assert result.freqstr == "h"
assert result.tz == expected.tz
expected = date_range(
start="2000-01-01 10:00", periods=9, freq="h", name="idx", tz=tz
)
result = idx.delete(0)
tm.assert_index_equal(result, expected)
assert result.name == expected.name
assert result.freqstr == "h"
assert result.tz == expected.tz

expected = date_range(
start="2000-01-01 09:00", periods=9, freq="h", name="idx", tz=tz
)
result = idx.delete(-1)
tm.assert_index_equal(result, expected)
assert result.name == expected.name
assert result.freqstr == "h"
assert result.tz == expected.tz

def test_delete_slice(self):
idx = date_range(start="2000-01-01", periods=10, freq="D", name="idx")
Expand Down Expand Up @@ -101,38 +102,40 @@ def test_delete_slice(self):
assert result.name == expected.name
assert result.freq == expected.freq

for tz in [None, "Asia/Tokyo", "US/Pacific"]:
ts = Series(
1,
index=date_range(
"2000-01-01 09:00", periods=10, freq="h", name="idx", tz=tz
),
)
# preserve freq
result = ts.drop(ts.index[:5]).index
expected = date_range(
"2000-01-01 14:00", periods=5, freq="h", name="idx", tz=tz
)
tm.assert_index_equal(result, expected)
assert result.name == expected.name
assert result.freq == expected.freq
assert result.tz == expected.tz

# reset freq to None
result = ts.drop(ts.index[[1, 3, 5, 7, 9]]).index
expected = DatetimeIndex(
[
"2000-01-01 09:00",
"2000-01-01 11:00",
"2000-01-01 13:00",
"2000-01-01 15:00",
"2000-01-01 17:00",
],
freq=None,
name="idx",
tz=tz,
)
tm.assert_index_equal(result, expected)
assert result.name == expected.name
assert result.freq == expected.freq
assert result.tz == expected.tz
# TODO: belongs in Series.drop tests?
@pytest.mark.parametrize("tz", [None, "Asia/Tokyo", "US/Pacific"])
def test_delete_slice2(self, tz):
ts = Series(
1,
index=date_range(
"2000-01-01 09:00", periods=10, freq="h", name="idx", tz=tz
),
)
# preserve freq
result = ts.drop(ts.index[:5]).index
expected = date_range(
"2000-01-01 14:00", periods=5, freq="h", name="idx", tz=tz
)
tm.assert_index_equal(result, expected)
assert result.name == expected.name
assert result.freq == expected.freq
assert result.tz == expected.tz

# reset freq to None
result = ts.drop(ts.index[[1, 3, 5, 7, 9]]).index
expected = DatetimeIndex(
[
"2000-01-01 09:00",
"2000-01-01 11:00",
"2000-01-01 13:00",
"2000-01-01 15:00",
"2000-01-01 17:00",
],
freq=None,
name="idx",
tz=tz,
)
tm.assert_index_equal(result, expected)
assert result.name == expected.name
assert result.freq == expected.freq
assert result.tz == expected.tz
7 changes: 7 additions & 0 deletions pandas/tests/indexes/datetimes/methods/test_isocalendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ def test_isocalendar_returns_correct_values_close_to_new_year_with_tz():
dtype="UInt32",
)
tm.assert_frame_equal(result, expected_data_frame)


def test_dti_timestamp_isocalendar_fields():
idx = tm.makeDateIndex(100)
expected = tuple(idx.isocalendar().iloc[-1].to_list())
result = idx[-1].isocalendar()
assert result == expected
95 changes: 95 additions & 0 deletions pandas/tests/indexes/datetimes/methods/test_normalize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
from dateutil.tz import tzlocal
import numpy as np
import pytest

import pandas.util._test_decorators as td

from pandas import (
DatetimeIndex,
NaT,
Timestamp,
date_range,
)
import pandas._testing as tm


class TestNormalize:
def test_normalize(self):
rng = date_range("1/1/2000 9:30", periods=10, freq="D")

result = rng.normalize()
expected = date_range("1/1/2000", periods=10, freq="D")
tm.assert_index_equal(result, expected)

arr_ns = np.array([1380585623454345752, 1380585612343234312]).astype(
"datetime64[ns]"
)
rng_ns = DatetimeIndex(arr_ns)
rng_ns_normalized = rng_ns.normalize()

arr_ns = np.array([1380585600000000000, 1380585600000000000]).astype(
"datetime64[ns]"
)
expected = DatetimeIndex(arr_ns)
tm.assert_index_equal(rng_ns_normalized, expected)

assert result.is_normalized
assert not rng.is_normalized

def test_normalize_nat(self):
dti = DatetimeIndex([NaT, Timestamp("2018-01-01 01:00:00")])
result = dti.normalize()
expected = DatetimeIndex([NaT, Timestamp("2018-01-01")])
tm.assert_index_equal(result, expected)

def test_normalize_tz(self):
rng = date_range("1/1/2000 9:30", periods=10, freq="D", tz="US/Eastern")

result = rng.normalize() # does not preserve freq
expected = date_range("1/1/2000", periods=10, freq="D", tz="US/Eastern")
tm.assert_index_equal(result, expected._with_freq(None))

assert result.is_normalized
assert not rng.is_normalized

rng = date_range("1/1/2000 9:30", periods=10, freq="D", tz="UTC")

result = rng.normalize()
expected = date_range("1/1/2000", periods=10, freq="D", tz="UTC")
tm.assert_index_equal(result, expected)

assert result.is_normalized
assert not rng.is_normalized

rng = date_range("1/1/2000 9:30", periods=10, freq="D", tz=tzlocal())
result = rng.normalize() # does not preserve freq
expected = date_range("1/1/2000", periods=10, freq="D", tz=tzlocal())
tm.assert_index_equal(result, expected._with_freq(None))

assert result.is_normalized
assert not rng.is_normalized

@td.skip_if_windows
@pytest.mark.parametrize(
"timezone",
[
"US/Pacific",
"US/Eastern",
"UTC",
"Asia/Kolkata",
"Asia/Shanghai",
"Australia/Canberra",
],
)
def test_normalize_tz_local(self, timezone):
# GH#13459
with tm.set_timezone(timezone):
rng = date_range("1/1/2000 9:30", periods=10, freq="D", tz=tzlocal())

result = rng.normalize()
expected = date_range("1/1/2000", periods=10, freq="D", tz=tzlocal())
expected = expected._with_freq(None)
tm.assert_index_equal(result, expected)

assert result.is_normalized
assert not rng.is_normalized
31 changes: 31 additions & 0 deletions pandas/tests/indexes/datetimes/methods/test_resolution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from dateutil.tz import tzlocal
import pytest

from pandas.compat import IS64

from pandas import date_range


@pytest.mark.parametrize(
"freq,expected",
[
("Y", "day"),
("Q", "day"),
("ME", "day"),
("D", "day"),
("h", "hour"),
("min", "minute"),
("s", "second"),
("ms", "millisecond"),
("us", "microsecond"),
],
)
def test_dti_resolution(request, tz_naive_fixture, freq, expected):
tz = tz_naive_fixture
if freq == "Y" and not IS64 and isinstance(tz, tzlocal):
request.applymarker(
pytest.mark.xfail(reason="OverflowError inside tzlocal past 2038")
)

idx = date_range(start="2013-04-01", periods=30, freq=freq, tz=tz)
assert idx.resolution == expected
Loading
Loading