Skip to content

REF/TST: misplaced tests in tests.indexes.period #31758

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 6 commits into from
Feb 9, 2020
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
83 changes: 10 additions & 73 deletions pandas/tests/indexes/datetimes/test_astype.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from datetime import datetime

import dateutil
from dateutil.tz import tzlocal
import numpy as np
import pytest
import pytz
Expand All @@ -12,7 +11,7 @@
Index,
Int64Index,
NaT,
Period,
PeriodIndex,
Series,
Timestamp,
date_range,
Expand Down Expand Up @@ -278,81 +277,19 @@ def test_integer_index_astype_datetime(self, tz, dtype):
expected = pd.DatetimeIndex(["2018-01-01"], tz=tz)
tm.assert_index_equal(result, expected)

def test_dti_astype_period(self):
idx = DatetimeIndex([NaT, "2011-01-01", "2011-02-01"], name="idx")

class TestToPeriod:
def setup_method(self, method):
data = [
Timestamp("2007-01-01 10:11:12.123456Z"),
Timestamp("2007-01-01 10:11:13.789123Z"),
]
self.index = DatetimeIndex(data)

def test_to_period_millisecond(self):
index = self.index

with tm.assert_produces_warning(UserWarning):
# warning that timezone info will be lost
period = index.to_period(freq="L")
assert 2 == len(period)
assert period[0] == Period("2007-01-01 10:11:12.123Z", "L")
assert period[1] == Period("2007-01-01 10:11:13.789Z", "L")

def test_to_period_microsecond(self):
index = self.index
res = idx.astype("period[M]")
exp = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="M", name="idx")
tm.assert_index_equal(res, exp)

with tm.assert_produces_warning(UserWarning):
# warning that timezone info will be lost
period = index.to_period(freq="U")
assert 2 == len(period)
assert period[0] == Period("2007-01-01 10:11:12.123456Z", "U")
assert period[1] == Period("2007-01-01 10:11:13.789123Z", "U")

@pytest.mark.parametrize(
"tz",
["US/Eastern", pytz.utc, tzlocal(), "dateutil/US/Eastern", dateutil.tz.tzutc()],
)
def test_to_period_tz(self, tz):
ts = date_range("1/1/2000", "2/1/2000", tz=tz)

with tm.assert_produces_warning(UserWarning):
# GH#21333 warning that timezone info will be lost
result = ts.to_period()[0]
expected = ts[0].to_period()

assert result == expected

expected = date_range("1/1/2000", "2/1/2000").to_period()

with tm.assert_produces_warning(UserWarning):
# GH#21333 warning that timezone info will be lost
result = ts.to_period()

tm.assert_index_equal(result, expected)
res = idx.astype("period[3M]")
exp = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="3M", name="idx")
tm.assert_index_equal(res, exp)

@pytest.mark.parametrize("tz", ["Etc/GMT-1", "Etc/GMT+1"])
def test_to_period_tz_utc_offset_consistency(self, tz):
# GH 22905
ts = pd.date_range("1/1/2000", "2/1/2000", tz="Etc/GMT-1")
with tm.assert_produces_warning(UserWarning):
result = ts.to_period()[0]
expected = ts[0].to_period()
assert result == expected

def test_to_period_nofreq(self):
idx = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-04"])
with pytest.raises(ValueError):
idx.to_period()

idx = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-03"], freq="infer")
assert idx.freqstr == "D"
expected = pd.PeriodIndex(["2000-01-01", "2000-01-02", "2000-01-03"], freq="D")
tm.assert_index_equal(idx.to_period(), expected)

# GH 7606
idx = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-03"])
assert idx.freqstr is None
tm.assert_index_equal(idx.to_period(), expected)

class TestAstype:
@pytest.mark.parametrize("tz", [None, "US/Central"])
def test_astype_category(self, tz):
obj = pd.date_range("2000", periods=2, tz=tz)
Expand Down
161 changes: 161 additions & 0 deletions pandas/tests/indexes/datetimes/test_to_period.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import dateutil.tz
from dateutil.tz import tzlocal
import pytest
import pytz

from pandas._libs.tslibs.ccalendar import MONTHS
from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG

from pandas import (
DatetimeIndex,
Period,
PeriodIndex,
Timestamp,
date_range,
period_range,
)
import pandas._testing as tm


class TestToPeriod:
def test_dti_to_period(self):
dti = date_range(start="1/1/2005", end="12/1/2005", freq="M")
pi1 = dti.to_period()
pi2 = dti.to_period(freq="D")
pi3 = dti.to_period(freq="3D")

assert pi1[0] == Period("Jan 2005", freq="M")
assert pi2[0] == Period("1/31/2005", freq="D")
assert pi3[0] == Period("1/31/2005", freq="3D")

assert pi1[-1] == Period("Nov 2005", freq="M")
assert pi2[-1] == Period("11/30/2005", freq="D")
assert pi3[-1], Period("11/30/2005", freq="3D")

tm.assert_index_equal(pi1, period_range("1/1/2005", "11/1/2005", freq="M"))
tm.assert_index_equal(
pi2, period_range("1/1/2005", "11/1/2005", freq="M").asfreq("D")
)
tm.assert_index_equal(
pi3, period_range("1/1/2005", "11/1/2005", freq="M").asfreq("3D")
)

@pytest.mark.parametrize("month", MONTHS)
def test_to_period_quarterly(self, month):
# make sure we can make the round trip
freq = "Q-{month}".format(month=month)
rng = period_range("1989Q3", "1991Q3", freq=freq)
stamps = rng.to_timestamp()
result = stamps.to_period(freq)
tm.assert_index_equal(rng, result)

@pytest.mark.parametrize("off", ["BQ", "QS", "BQS"])
def test_to_period_quarterlyish(self, off):
rng = date_range("01-Jan-2012", periods=8, freq=off)
prng = rng.to_period()
assert prng.freq == "Q-DEC"

@pytest.mark.parametrize("off", ["BA", "AS", "BAS"])
def test_to_period_annualish(self, off):
rng = date_range("01-Jan-2012", periods=8, freq=off)
prng = rng.to_period()
assert prng.freq == "A-DEC"

def test_to_period_monthish(self):
offsets = ["MS", "BM"]
for off in offsets:
rng = date_range("01-Jan-2012", periods=8, freq=off)
prng = rng.to_period()
assert prng.freq == "M"

rng = date_range("01-Jan-2012", periods=8, freq="M")
prng = rng.to_period()
assert prng.freq == "M"

with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG):
date_range("01-Jan-2012", periods=8, freq="EOM")

def test_period_dt64_round_trip(self):
dti = date_range("1/1/2000", "1/7/2002", freq="B")
pi = dti.to_period()
tm.assert_index_equal(pi.to_timestamp(), dti)

dti = date_range("1/1/2000", "1/7/2002", freq="B")
pi = dti.to_period(freq="H")
tm.assert_index_equal(pi.to_timestamp(), dti)

def test_to_period_millisecond(self):
index = DatetimeIndex(
[
Timestamp("2007-01-01 10:11:12.123456Z"),
Timestamp("2007-01-01 10:11:13.789123Z"),
]
)

with tm.assert_produces_warning(UserWarning):
# warning that timezone info will be lost
period = index.to_period(freq="L")
assert 2 == len(period)
assert period[0] == Period("2007-01-01 10:11:12.123Z", "L")
assert period[1] == Period("2007-01-01 10:11:13.789Z", "L")

def test_to_period_microsecond(self):
index = DatetimeIndex(
[
Timestamp("2007-01-01 10:11:12.123456Z"),
Timestamp("2007-01-01 10:11:13.789123Z"),
]
)

with tm.assert_produces_warning(UserWarning):
# warning that timezone info will be lost
period = index.to_period(freq="U")
assert 2 == len(period)
assert period[0] == Period("2007-01-01 10:11:12.123456Z", "U")
assert period[1] == Period("2007-01-01 10:11:13.789123Z", "U")

@pytest.mark.parametrize(
"tz",
["US/Eastern", pytz.utc, tzlocal(), "dateutil/US/Eastern", dateutil.tz.tzutc()],
)
def test_to_period_tz(self, tz):
ts = date_range("1/1/2000", "2/1/2000", tz=tz)

with tm.assert_produces_warning(UserWarning):
# GH#21333 warning that timezone info will be lost
result = ts.to_period()[0]
expected = ts[0].to_period()

assert result == expected

expected = date_range("1/1/2000", "2/1/2000").to_period()

with tm.assert_produces_warning(UserWarning):
# GH#21333 warning that timezone info will be lost
result = ts.to_period()

tm.assert_index_equal(result, expected)

@pytest.mark.parametrize("tz", ["Etc/GMT-1", "Etc/GMT+1"])
def test_to_period_tz_utc_offset_consistency(self, tz):
# GH#22905
ts = date_range("1/1/2000", "2/1/2000", tz="Etc/GMT-1")
with tm.assert_produces_warning(UserWarning):
result = ts.to_period()[0]
expected = ts[0].to_period()
assert result == expected

def test_to_period_nofreq(self):
idx = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-04"])
with pytest.raises(ValueError):
idx.to_period()

idx = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-03"], freq="infer")
assert idx.freqstr == "D"
expected = PeriodIndex(["2000-01-01", "2000-01-02", "2000-01-03"], freq="D")
tm.assert_index_equal(idx.to_period(), expected)

# GH#7606
idx = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-03"])
assert idx.freqstr is None
tm.assert_index_equal(idx.to_period(), expected)
30 changes: 4 additions & 26 deletions pandas/tests/indexes/period/test_asfreq.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import numpy as np
import pytest

import pandas as pd
from pandas import DataFrame, PeriodIndex, Series, period_range
from pandas import PeriodIndex, period_range
import pandas._testing as tm


Expand Down Expand Up @@ -98,46 +96,26 @@ def test_asfreq_mult_pi(self, freq):
assert result.freq == exp.freq

def test_asfreq_combined_pi(self):
pi = pd.PeriodIndex(["2001-01-01 00:00", "2001-01-02 02:00", "NaT"], freq="H")
pi = PeriodIndex(["2001-01-01 00:00", "2001-01-02 02:00", "NaT"], freq="H")
exp = PeriodIndex(["2001-01-01 00:00", "2001-01-02 02:00", "NaT"], freq="25H")
for freq, how in zip(["1D1H", "1H1D"], ["S", "E"]):
result = pi.asfreq(freq, how=how)
tm.assert_index_equal(result, exp)
assert result.freq == exp.freq

for freq in ["1D1H", "1H1D"]:
pi = pd.PeriodIndex(
["2001-01-01 00:00", "2001-01-02 02:00", "NaT"], freq=freq
)
pi = PeriodIndex(["2001-01-01 00:00", "2001-01-02 02:00", "NaT"], freq=freq)
result = pi.asfreq("H")
exp = PeriodIndex(["2001-01-02 00:00", "2001-01-03 02:00", "NaT"], freq="H")
tm.assert_index_equal(result, exp)
assert result.freq == exp.freq

pi = pd.PeriodIndex(
["2001-01-01 00:00", "2001-01-02 02:00", "NaT"], freq=freq
)
pi = PeriodIndex(["2001-01-01 00:00", "2001-01-02 02:00", "NaT"], freq=freq)
result = pi.asfreq("H", how="S")
exp = PeriodIndex(["2001-01-01 00:00", "2001-01-02 02:00", "NaT"], freq="H")
tm.assert_index_equal(result, exp)
assert result.freq == exp.freq

def test_asfreq_ts(self):
index = period_range(freq="A", start="1/1/2001", end="12/31/2010")
ts = Series(np.random.randn(len(index)), index=index)
df = DataFrame(np.random.randn(len(index), 3), index=index)

result = ts.asfreq("D", how="end")
df_result = df.asfreq("D", how="end")
exp_index = index.asfreq("D", how="end")
assert len(result) == len(ts)
tm.assert_index_equal(result.index, exp_index)
tm.assert_index_equal(df_result.index, exp_index)

result = ts.asfreq("D", how="start")
assert len(result) == len(ts)
tm.assert_index_equal(result.index, index.asfreq("D", how="start"))

def test_astype_asfreq(self):
pi1 = PeriodIndex(["2011-01-01", "2011-02-01", "2011-03-01"], freq="D")
exp = PeriodIndex(["2011-01", "2011-02", "2011-03"], freq="M")
Expand Down
Loading