From 4daaf13bafb0f03091e7962fd75404b5bfce40bf Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 6 Feb 2020 11:32:32 -0800 Subject: [PATCH 1/6] move misplaced PeriodIndex tests --- pandas/tests/indexes/period/test_tools.py | 55 +++++++------------ .../series/methods/test_combine_first.py | 19 +++++++ .../tests/series/methods/test_to_timestamp.py | 54 ++++++++++++++++++ 3 files changed, 93 insertions(+), 35 deletions(-) create mode 100644 pandas/tests/series/methods/test_combine_first.py create mode 100644 pandas/tests/series/methods/test_to_timestamp.py diff --git a/pandas/tests/indexes/period/test_tools.py b/pandas/tests/indexes/period/test_tools.py index 23350fdff4b78..906ac54fb0c86 100644 --- a/pandas/tests/indexes/period/test_tools.py +++ b/pandas/tests/indexes/period/test_tools.py @@ -5,10 +5,11 @@ from pandas._libs.tslibs import IncompatibleFrequency from pandas._libs.tslibs.ccalendar import MONTHS +from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG -import pandas as pd from pandas import ( DatetimeIndex, + NaT, Period, PeriodIndex, Series, @@ -110,9 +111,9 @@ def _get_with_delta(delta, freq="A-DEC"): assert result.name == "foo" def test_to_timestamp_freq(self): - idx = pd.period_range("2017", periods=12, freq="A-DEC") + idx = period_range("2017", periods=12, freq="A-DEC") result = idx.to_timestamp() - expected = pd.date_range("2017", periods=12, freq="AS-JAN") + expected = date_range("2017", periods=12, freq="AS-JAN") tm.assert_index_equal(result, expected) def test_to_timestamp_repr_is_code(self): @@ -126,7 +127,7 @@ def test_to_timestamp_repr_is_code(self): assert eval(repr(z)) == z def test_to_timestamp_to_period_astype(self): - idx = DatetimeIndex([pd.NaT, "2011-01-01", "2011-02-01"], name="idx") + idx = DatetimeIndex([NaT, "2011-01-01", "2011-02-01"], name="idx") res = idx.astype("period[M]") exp = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="M", name="idx") @@ -137,7 +138,7 @@ def test_to_timestamp_to_period_astype(self): tm.assert_index_equal(res, exp) def test_dti_to_period(self): - dti = pd.date_range(start="1/1/2005", end="12/1/2005", freq="M") + 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") @@ -190,8 +191,7 @@ def test_to_period_monthish(self): prng = rng.to_period() assert prng.freq == "M" - msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG - with pytest.raises(ValueError, match=msg): + 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): @@ -203,46 +203,31 @@ def test_period_dt64_round_trip(self): pi = dti.to_period(freq="H") tm.assert_index_equal(pi.to_timestamp(), dti) - def test_combine_first(self): - # GH#3367 - didx = pd.date_range(start="1950-01-31", end="1950-07-31", freq="M") - pidx = pd.period_range( - start=pd.Period("1950-1"), end=pd.Period("1950-7"), freq="M" - ) - # check to be consistent with DatetimeIndex - for idx in [didx, pidx]: - a = pd.Series([1, np.nan, np.nan, 4, 5, np.nan, 7], index=idx) - b = pd.Series([9, 9, 9, 9, 9, 9, 9], index=idx) - - result = a.combine_first(b) - expected = pd.Series([1, 9, 9, 4, 5, 9, 7], index=idx, dtype=np.float64) - tm.assert_series_equal(result, expected) - @pytest.mark.parametrize("freq", ["D", "2D"]) def test_searchsorted(self, freq): - pidx = pd.PeriodIndex( + pidx = PeriodIndex( ["2014-01-01", "2014-01-02", "2014-01-03", "2014-01-04", "2014-01-05"], freq=freq, ) - p1 = pd.Period("2014-01-01", freq=freq) + p1 = Period("2014-01-01", freq=freq) assert pidx.searchsorted(p1) == 0 - p2 = pd.Period("2014-01-04", freq=freq) + p2 = Period("2014-01-04", freq=freq) assert pidx.searchsorted(p2) == 3 - assert pidx.searchsorted(pd.NaT) == 0 + assert pidx.searchsorted(NaT) == 0 msg = "Input has different freq=H from PeriodArray" with pytest.raises(IncompatibleFrequency, match=msg): - pidx.searchsorted(pd.Period("2014-01-01", freq="H")) + pidx.searchsorted(Period("2014-01-01", freq="H")) msg = "Input has different freq=5D from PeriodArray" with pytest.raises(IncompatibleFrequency, match=msg): - pidx.searchsorted(pd.Period("2014-01-01", freq="5D")) + pidx.searchsorted(Period("2014-01-01", freq="5D")) def test_searchsorted_invalid(self): - pidx = pd.PeriodIndex( + pidx = PeriodIndex( ["2014-01-01", "2014-01-02", "2014-01-03", "2014-01-04", "2014-01-05"], freq="D", ) @@ -290,7 +275,7 @@ def test_to_timestamp_pi_nat(self): result = index.to_timestamp("D") expected = DatetimeIndex( - [pd.NaT, datetime(2011, 1, 1), datetime(2011, 2, 1)], name="idx" + [NaT, datetime(2011, 1, 1), datetime(2011, 2, 1)], name="idx" ) tm.assert_index_equal(result, expected) assert result.name == "idx" @@ -357,22 +342,22 @@ def test_to_timestamp_pi_combined(self): tm.assert_index_equal(result, expected) def test_period_astype_to_timestamp(self): - pi = pd.PeriodIndex(["2011-01", "2011-02", "2011-03"], freq="M") + pi = PeriodIndex(["2011-01", "2011-02", "2011-03"], freq="M") - exp = pd.DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"]) + exp = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"]) tm.assert_index_equal(pi.astype("datetime64[ns]"), exp) - exp = pd.DatetimeIndex(["2011-01-31", "2011-02-28", "2011-03-31"]) + exp = DatetimeIndex(["2011-01-31", "2011-02-28", "2011-03-31"]) exp = exp + Timedelta(1, "D") - Timedelta(1, "ns") tm.assert_index_equal(pi.astype("datetime64[ns]", how="end"), exp) - exp = pd.DatetimeIndex( + exp = DatetimeIndex( ["2011-01-01", "2011-02-01", "2011-03-01"], tz="US/Eastern" ) res = pi.astype("datetime64[ns, US/Eastern]") tm.assert_index_equal(pi.astype("datetime64[ns, US/Eastern]"), exp) - exp = pd.DatetimeIndex( + exp = DatetimeIndex( ["2011-01-31", "2011-02-28", "2011-03-31"], tz="US/Eastern" ) exp = exp + Timedelta(1, "D") - Timedelta(1, "ns") diff --git a/pandas/tests/series/methods/test_combine_first.py b/pandas/tests/series/methods/test_combine_first.py new file mode 100644 index 0000000000000..aed6425e50117 --- /dev/null +++ b/pandas/tests/series/methods/test_combine_first.py @@ -0,0 +1,19 @@ +import numpy as np + +from pandas import Period, Series, date_range, period_range +import pandas._testing as tm + + +class TestCombineFirst: + def test_combine_first_period_datetime(self): + # GH#3367 + didx = date_range(start="1950-01-31", end="1950-07-31", freq="M") + pidx = period_range(start=Period("1950-1"), end=Period("1950-7"), freq="M") + # check to be consistent with DatetimeIndex + for idx in [didx, pidx]: + a = Series([1, np.nan, np.nan, 4, 5, np.nan, 7], index=idx) + b = Series([9, 9, 9, 9, 9, 9, 9], index=idx) + + result = a.combine_first(b) + expected = Series([1, 9, 9, 4, 5, 9, 7], index=idx, dtype=np.float64) + tm.assert_series_equal(result, expected) diff --git a/pandas/tests/series/methods/test_to_timestamp.py b/pandas/tests/series/methods/test_to_timestamp.py new file mode 100644 index 0000000000000..44caf1f082a4f --- /dev/null +++ b/pandas/tests/series/methods/test_to_timestamp.py @@ -0,0 +1,54 @@ +from datetime import timedelta + +from pandas import Series, Timedelta, date_range, period_range, to_datetime +import pandas._testing as tm + + +class TestToTimestamp: + def test_to_timestamp(self): + index = period_range(freq="A", start="1/1/2001", end="12/1/2009") + series = Series(1, index=index, name="foo") + + exp_index = date_range("1/1/2001", end="12/31/2009", freq="A-DEC") + result = series.to_timestamp(how="end") + exp_index = exp_index + Timedelta(1, "D") - Timedelta(1, "ns") + tm.assert_index_equal(result.index, exp_index) + assert result.name == "foo" + + exp_index = date_range("1/1/2001", end="1/1/2009", freq="AS-JAN") + result = series.to_timestamp(how="start") + tm.assert_index_equal(result.index, exp_index) + + def _get_with_delta(delta, freq="A-DEC"): + return date_range( + to_datetime("1/1/2001") + delta, + to_datetime("12/31/2009") + delta, + freq=freq, + ) + + delta = timedelta(hours=23) + result = series.to_timestamp("H", "end") + exp_index = _get_with_delta(delta) + exp_index = exp_index + Timedelta(1, "h") - Timedelta(1, "ns") + tm.assert_index_equal(result.index, exp_index) + + delta = timedelta(hours=23, minutes=59) + result = series.to_timestamp("T", "end") + exp_index = _get_with_delta(delta) + exp_index = exp_index + Timedelta(1, "m") - Timedelta(1, "ns") + tm.assert_index_equal(result.index, exp_index) + + result = series.to_timestamp("S", "end") + delta = timedelta(hours=23, minutes=59, seconds=59) + exp_index = _get_with_delta(delta) + exp_index = exp_index + Timedelta(1, "s") - Timedelta(1, "ns") + tm.assert_index_equal(result.index, exp_index) + + index = period_range(freq="H", start="1/1/2001", end="1/2/2001") + series = Series(1, index=index, name="foo") + + exp_index = date_range("1/1/2001 00:59:59", end="1/2/2001 00:59:59", freq="H") + result = series.to_timestamp(how="end") + exp_index = exp_index + Timedelta(1, "s") - Timedelta(1, "ns") + tm.assert_index_equal(result.index, exp_index) + assert result.name == "foo" From 4967392107bce352d36160857eb882f5416511d8 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 6 Feb 2020 11:41:37 -0800 Subject: [PATCH 2/6] misplaced tests --- .../tests/indexes/datetimes/test_to_period.py | 75 +++++++++ pandas/tests/indexes/period/test_tools.py | 159 ++---------------- pandas/tests/scalar/period/test_period.py | 18 ++ 3 files changed, 104 insertions(+), 148 deletions(-) create mode 100644 pandas/tests/indexes/datetimes/test_to_period.py diff --git a/pandas/tests/indexes/datetimes/test_to_period.py b/pandas/tests/indexes/datetimes/test_to_period.py new file mode 100644 index 0000000000000..49039e1219339 --- /dev/null +++ b/pandas/tests/indexes/datetimes/test_to_period.py @@ -0,0 +1,75 @@ +import pytest + +from pandas._libs.tslibs.ccalendar import MONTHS +from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG + +from pandas import Period, 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) diff --git a/pandas/tests/indexes/period/test_tools.py b/pandas/tests/indexes/period/test_tools.py index 906ac54fb0c86..1d7e9480cc1f9 100644 --- a/pandas/tests/indexes/period/test_tools.py +++ b/pandas/tests/indexes/period/test_tools.py @@ -1,23 +1,19 @@ -from datetime import datetime, timedelta +from datetime import datetime import numpy as np import pytest from pandas._libs.tslibs import IncompatibleFrequency -from pandas._libs.tslibs.ccalendar import MONTHS -from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG from pandas import ( DatetimeIndex, NaT, Period, PeriodIndex, - Series, Timedelta, Timestamp, date_range, period_range, - to_datetime, ) import pandas._testing as tm @@ -43,79 +39,8 @@ def test_monthly(self): def test_freq(self, freq): self._check_freq(freq, "1970-01-01") - def test_negone_ordinals(self): - freqs = ["A", "M", "Q", "D", "H", "T", "S"] - - period = Period(ordinal=-1, freq="D") - for freq in freqs: - repr(period.asfreq(freq)) - - for freq in freqs: - period = Period(ordinal=-1, freq=freq) - repr(period) - assert period.year == 1969 - - period = Period(ordinal=-1, freq="B") - repr(period) - period = Period(ordinal=-1, freq="W") - repr(period) - class TestPeriodIndex: - def test_to_timestamp(self): - index = period_range(freq="A", start="1/1/2001", end="12/1/2009") - series = Series(1, index=index, name="foo") - - exp_index = date_range("1/1/2001", end="12/31/2009", freq="A-DEC") - result = series.to_timestamp(how="end") - exp_index = exp_index + Timedelta(1, "D") - Timedelta(1, "ns") - tm.assert_index_equal(result.index, exp_index) - assert result.name == "foo" - - exp_index = date_range("1/1/2001", end="1/1/2009", freq="AS-JAN") - result = series.to_timestamp(how="start") - tm.assert_index_equal(result.index, exp_index) - - def _get_with_delta(delta, freq="A-DEC"): - return date_range( - to_datetime("1/1/2001") + delta, - to_datetime("12/31/2009") + delta, - freq=freq, - ) - - delta = timedelta(hours=23) - result = series.to_timestamp("H", "end") - exp_index = _get_with_delta(delta) - exp_index = exp_index + Timedelta(1, "h") - Timedelta(1, "ns") - tm.assert_index_equal(result.index, exp_index) - - delta = timedelta(hours=23, minutes=59) - result = series.to_timestamp("T", "end") - exp_index = _get_with_delta(delta) - exp_index = exp_index + Timedelta(1, "m") - Timedelta(1, "ns") - tm.assert_index_equal(result.index, exp_index) - - result = series.to_timestamp("S", "end") - delta = timedelta(hours=23, minutes=59, seconds=59) - exp_index = _get_with_delta(delta) - exp_index = exp_index + Timedelta(1, "s") - Timedelta(1, "ns") - tm.assert_index_equal(result.index, exp_index) - - index = period_range(freq="H", start="1/1/2001", end="1/2/2001") - series = Series(1, index=index, name="foo") - - exp_index = date_range("1/1/2001 00:59:59", end="1/2/2001 00:59:59", freq="H") - result = series.to_timestamp(how="end") - exp_index = exp_index + Timedelta(1, "s") - Timedelta(1, "ns") - tm.assert_index_equal(result.index, exp_index) - assert result.name == "foo" - - def test_to_timestamp_freq(self): - idx = period_range("2017", periods=12, freq="A-DEC") - result = idx.to_timestamp() - expected = date_range("2017", periods=12, freq="AS-JAN") - tm.assert_index_equal(result, expected) - def test_to_timestamp_repr_is_code(self): zs = [ Timestamp("99-04-17 00:00:00", tz="UTC"), @@ -126,7 +51,7 @@ def test_to_timestamp_repr_is_code(self): for z in zs: assert eval(repr(z)) == z - def test_to_timestamp_to_period_astype(self): + def test_dti_astype_period(self): idx = DatetimeIndex([NaT, "2011-01-01", "2011-02-01"], name="idx") res = idx.astype("period[M]") @@ -137,72 +62,8 @@ def test_to_timestamp_to_period_astype(self): exp = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="3M", name="idx") tm.assert_index_equal(res, exp) - 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) +class TestSearchsorted: @pytest.mark.parametrize("freq", ["D", "2D"]) def test_searchsorted(self, freq): pidx = PeriodIndex( @@ -269,6 +130,12 @@ def test_tolist(self): recon = PeriodIndex(rs) tm.assert_index_equal(index, recon) + def test_to_timestamp_freq(self): + idx = period_range("2017", periods=12, freq="A-DEC") + result = idx.to_timestamp() + expected = date_range("2017", periods=12, freq="AS-JAN") + tm.assert_index_equal(result, expected) + def test_to_timestamp_pi_nat(self): # GH#7228 index = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="M", name="idx") @@ -351,15 +218,11 @@ def test_period_astype_to_timestamp(self): exp = exp + Timedelta(1, "D") - Timedelta(1, "ns") tm.assert_index_equal(pi.astype("datetime64[ns]", how="end"), exp) - exp = DatetimeIndex( - ["2011-01-01", "2011-02-01", "2011-03-01"], tz="US/Eastern" - ) + exp = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"], tz="US/Eastern") res = pi.astype("datetime64[ns, US/Eastern]") tm.assert_index_equal(pi.astype("datetime64[ns, US/Eastern]"), exp) - exp = DatetimeIndex( - ["2011-01-31", "2011-02-28", "2011-03-31"], tz="US/Eastern" - ) + exp = DatetimeIndex(["2011-01-31", "2011-02-28", "2011-03-31"], tz="US/Eastern") exp = exp + Timedelta(1, "D") - Timedelta(1, "ns") res = pi.astype("datetime64[ns, US/Eastern]", how="end") tm.assert_index_equal(res, exp) diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index 995d47c1473be..b396a88e6eb6a 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -1565,3 +1565,21 @@ def test_small_year_parsing(): per1 = Period("0001-01-07", "D") assert per1.year == 1 assert per1.day == 7 + + +def test_negone_ordinals(): + freqs = ["A", "M", "Q", "D", "H", "T", "S"] + + period = Period(ordinal=-1, freq="D") + for freq in freqs: + repr(period.asfreq(freq)) + + for freq in freqs: + period = Period(ordinal=-1, freq=freq) + repr(period) + assert period.year == 1969 + + period = Period(ordinal=-1, freq="B") + repr(period) + period = Period(ordinal=-1, freq="W") + repr(period) From fa8e09021647ffb27b75a4df681235c0b762e324 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 6 Feb 2020 11:44:49 -0800 Subject: [PATCH 3/6] move to_period tests --- pandas/tests/indexes/datetimes/test_astype.py | 78 +--------------- .../tests/indexes/datetimes/test_to_period.py | 88 ++++++++++++++++++- 2 files changed, 88 insertions(+), 78 deletions(-) diff --git a/pandas/tests/indexes/datetimes/test_astype.py b/pandas/tests/indexes/datetimes/test_astype.py index 6139726dc34e4..a66ec3c307e68 100644 --- a/pandas/tests/indexes/datetimes/test_astype.py +++ b/pandas/tests/indexes/datetimes/test_astype.py @@ -1,22 +1,12 @@ from datetime import datetime import dateutil -from dateutil.tz import tzlocal import numpy as np import pytest import pytz import pandas as pd -from pandas import ( - DatetimeIndex, - Index, - Int64Index, - NaT, - Period, - Series, - Timestamp, - date_range, -) +from pandas import DatetimeIndex, Index, Int64Index, NaT, Series, Timestamp, date_range import pandas._testing as tm @@ -287,72 +277,6 @@ def setup_method(self, method): ] 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 - - 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 = 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) - @pytest.mark.parametrize("tz", [None, "US/Central"]) def test_astype_category(self, tz): obj = pd.date_range("2000", periods=2, tz=tz) diff --git a/pandas/tests/indexes/datetimes/test_to_period.py b/pandas/tests/indexes/datetimes/test_to_period.py index 49039e1219339..5567f98c52211 100644 --- a/pandas/tests/indexes/datetimes/test_to_period.py +++ b/pandas/tests/indexes/datetimes/test_to_period.py @@ -1,9 +1,19 @@ +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 Period, date_range, period_range +from pandas import ( + DatetimeIndex, + Period, + PeriodIndex, + Timestamp, + date_range, + period_range, +) import pandas._testing as tm @@ -73,3 +83,79 @@ def test_period_dt64_round_trip(self): 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) From 86dd5edeb45a10d6ae52bf4bd75b58facf3df05e Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 6 Feb 2020 11:46:58 -0800 Subject: [PATCH 4/6] misplaced tests --- pandas/tests/indexes/datetimes/test_astype.py | 29 ++++++++++++++----- pandas/tests/indexes/period/test_tools.py | 23 --------------- .../tests/scalar/timestamp/test_rendering.py | 10 +++++++ 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/pandas/tests/indexes/datetimes/test_astype.py b/pandas/tests/indexes/datetimes/test_astype.py index a66ec3c307e68..916f722247a14 100644 --- a/pandas/tests/indexes/datetimes/test_astype.py +++ b/pandas/tests/indexes/datetimes/test_astype.py @@ -6,7 +6,16 @@ import pytz import pandas as pd -from pandas import DatetimeIndex, Index, Int64Index, NaT, Series, Timestamp, date_range +from pandas import ( + DatetimeIndex, + Index, + Int64Index, + NaT, + PeriodIndex, + Series, + Timestamp, + date_range, +) import pandas._testing as tm @@ -268,15 +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") + + res = idx.astype("period[M]") + exp = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="M", name="idx") + tm.assert_index_equal(res, exp) + + res = idx.astype("period[3M]") + exp = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="3M", name="idx") + tm.assert_index_equal(res, exp) -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) +class TestAstype: @pytest.mark.parametrize("tz", [None, "US/Central"]) def test_astype_category(self, tz): obj = pd.date_range("2000", periods=2, tz=tz) diff --git a/pandas/tests/indexes/period/test_tools.py b/pandas/tests/indexes/period/test_tools.py index 1d7e9480cc1f9..b8793a72c5628 100644 --- a/pandas/tests/indexes/period/test_tools.py +++ b/pandas/tests/indexes/period/test_tools.py @@ -40,29 +40,6 @@ def test_freq(self, freq): self._check_freq(freq, "1970-01-01") -class TestPeriodIndex: - def test_to_timestamp_repr_is_code(self): - zs = [ - Timestamp("99-04-17 00:00:00", tz="UTC"), - Timestamp("2001-04-17 00:00:00", tz="UTC"), - Timestamp("2001-04-17 00:00:00", tz="America/Los_Angeles"), - Timestamp("2001-04-17 00:00:00", tz=None), - ] - for z in zs: - assert eval(repr(z)) == z - - def test_dti_astype_period(self): - idx = DatetimeIndex([NaT, "2011-01-01", "2011-02-01"], name="idx") - - res = idx.astype("period[M]") - exp = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="M", name="idx") - tm.assert_index_equal(res, exp) - - res = idx.astype("period[3M]") - exp = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="3M", name="idx") - tm.assert_index_equal(res, exp) - - class TestSearchsorted: @pytest.mark.parametrize("freq", ["D", "2D"]) def test_searchsorted(self, freq): diff --git a/pandas/tests/scalar/timestamp/test_rendering.py b/pandas/tests/scalar/timestamp/test_rendering.py index 6b64b230a0bb9..cab6946bb8d02 100644 --- a/pandas/tests/scalar/timestamp/test_rendering.py +++ b/pandas/tests/scalar/timestamp/test_rendering.py @@ -85,3 +85,13 @@ def test_pprint(self): {'w': {'a': Timestamp('2011-01-01 00:00:00')}}], 'foo': 1}""" assert result == expected + + def test_to_timestamp_repr_is_code(self): + zs = [ + Timestamp("99-04-17 00:00:00", tz="UTC"), + Timestamp("2001-04-17 00:00:00", tz="UTC"), + Timestamp("2001-04-17 00:00:00", tz="America/Los_Angeles"), + Timestamp("2001-04-17 00:00:00", tz=None), + ] + for z in zs: + assert eval(repr(z)) == z From d12d2c3d4e594b63378c5f5ca4e6fac484a44f1d Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 6 Feb 2020 11:49:00 -0800 Subject: [PATCH 5/6] misplaced tests --- pandas/tests/indexes/period/test_astype.py | 30 +++++++++++++++++++++- pandas/tests/indexes/period/test_tools.py | 21 ++------------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/pandas/tests/indexes/period/test_astype.py b/pandas/tests/indexes/period/test_astype.py index ec386dd9dd11c..9ec0f79ff5a4a 100644 --- a/pandas/tests/indexes/period/test_astype.py +++ b/pandas/tests/indexes/period/test_astype.py @@ -2,7 +2,16 @@ import pytest import pandas as pd -from pandas import Index, Int64Index, NaT, Period, PeriodIndex, period_range +from pandas import ( + DatetimeIndex, + Index, + Int64Index, + NaT, + Period, + PeriodIndex, + Timedelta, + period_range, +) import pandas._testing as tm @@ -126,3 +135,22 @@ def test_astype_array_fallback(self): result = obj._data.astype(bool) expected = np.array([True, True]) tm.assert_numpy_array_equal(result, expected) + + def test_period_astype_to_timestamp(self): + pi = PeriodIndex(["2011-01", "2011-02", "2011-03"], freq="M") + + exp = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"]) + tm.assert_index_equal(pi.astype("datetime64[ns]"), exp) + + exp = DatetimeIndex(["2011-01-31", "2011-02-28", "2011-03-31"]) + exp = exp + Timedelta(1, "D") - Timedelta(1, "ns") + tm.assert_index_equal(pi.astype("datetime64[ns]", how="end"), exp) + + exp = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"], tz="US/Eastern") + res = pi.astype("datetime64[ns, US/Eastern]") + tm.assert_index_equal(pi.astype("datetime64[ns, US/Eastern]"), exp) + + exp = DatetimeIndex(["2011-01-31", "2011-02-28", "2011-03-31"], tz="US/Eastern") + exp = exp + Timedelta(1, "D") - Timedelta(1, "ns") + res = pi.astype("datetime64[ns, US/Eastern]", how="end") + tm.assert_index_equal(res, exp) diff --git a/pandas/tests/indexes/period/test_tools.py b/pandas/tests/indexes/period/test_tools.py index b8793a72c5628..dae220006ebe0 100644 --- a/pandas/tests/indexes/period/test_tools.py +++ b/pandas/tests/indexes/period/test_tools.py @@ -107,6 +107,8 @@ def test_tolist(self): recon = PeriodIndex(rs) tm.assert_index_equal(index, recon) + +class TestToTimestamp: def test_to_timestamp_freq(self): idx = period_range("2017", periods=12, freq="A-DEC") result = idx.to_timestamp() @@ -185,25 +187,6 @@ def test_to_timestamp_pi_combined(self): expected = expected + Timedelta(1, "h") - Timedelta(1, "ns") tm.assert_index_equal(result, expected) - def test_period_astype_to_timestamp(self): - pi = PeriodIndex(["2011-01", "2011-02", "2011-03"], freq="M") - - exp = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"]) - tm.assert_index_equal(pi.astype("datetime64[ns]"), exp) - - exp = DatetimeIndex(["2011-01-31", "2011-02-28", "2011-03-31"]) - exp = exp + Timedelta(1, "D") - Timedelta(1, "ns") - tm.assert_index_equal(pi.astype("datetime64[ns]", how="end"), exp) - - exp = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"], tz="US/Eastern") - res = pi.astype("datetime64[ns, US/Eastern]") - tm.assert_index_equal(pi.astype("datetime64[ns, US/Eastern]"), exp) - - exp = DatetimeIndex(["2011-01-31", "2011-02-28", "2011-03-31"], tz="US/Eastern") - exp = exp + Timedelta(1, "D") - Timedelta(1, "ns") - res = pi.astype("datetime64[ns, US/Eastern]", how="end") - tm.assert_index_equal(res, exp) - def test_to_timestamp_1703(self): index = period_range("1/1/2012", periods=4, freq="D") From 98b5b6f0da672fc3430b779b93a5278c3c716cc6 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 6 Feb 2020 12:05:44 -0800 Subject: [PATCH 6/6] misplaced PeriodIndex tests --- pandas/tests/indexes/period/test_asfreq.py | 30 +-- pandas/tests/indexes/period/test_astype.py | 51 ++--- .../tests/indexes/period/test_constructors.py | 94 ++++---- pandas/tests/indexes/period/test_indexing.py | 208 +++++++++--------- pandas/tests/indexes/period/test_ops.py | 4 - pandas/tests/indexes/period/test_period.py | 91 ++++---- .../indexes/period/test_scalar_compat.py | 2 + pandas/tests/indexes/period/test_setops.py | 91 ++++---- pandas/tests/indexes/period/test_shift.py | 11 +- pandas/tests/series/methods/test_asfreq.py | 23 ++ 10 files changed, 299 insertions(+), 306 deletions(-) create mode 100644 pandas/tests/series/methods/test_asfreq.py diff --git a/pandas/tests/indexes/period/test_asfreq.py b/pandas/tests/indexes/period/test_asfreq.py index 88e800d66f3ad..8c04ac1177676 100644 --- a/pandas/tests/indexes/period/test_asfreq.py +++ b/pandas/tests/indexes/period/test_asfreq.py @@ -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 @@ -98,7 +96,7 @@ 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) @@ -106,38 +104,18 @@ def test_asfreq_combined_pi(self): 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") diff --git a/pandas/tests/indexes/period/test_astype.py b/pandas/tests/indexes/period/test_astype.py index 9ec0f79ff5a4a..2f10e45193d5d 100644 --- a/pandas/tests/indexes/period/test_astype.py +++ b/pandas/tests/indexes/period/test_astype.py @@ -1,8 +1,8 @@ import numpy as np import pytest -import pandas as pd from pandas import ( + CategoricalIndex, DatetimeIndex, Index, Int64Index, @@ -10,6 +10,7 @@ Period, PeriodIndex, Timedelta, + UInt64Index, period_range, ) import pandas._testing as tm @@ -50,39 +51,39 @@ def test_astype_conversion(self): def test_astype_uint(self): arr = period_range("2000", periods=2) - expected = pd.UInt64Index(np.array([10957, 10958], dtype="uint64")) + expected = UInt64Index(np.array([10957, 10958], dtype="uint64")) tm.assert_index_equal(arr.astype("uint64"), expected) tm.assert_index_equal(arr.astype("uint32"), expected) def test_astype_object(self): - idx = pd.PeriodIndex([], freq="M") + idx = PeriodIndex([], freq="M") exp = np.array([], dtype=object) tm.assert_numpy_array_equal(idx.astype(object).values, exp) tm.assert_numpy_array_equal(idx._mpl_repr(), exp) - idx = pd.PeriodIndex(["2011-01", pd.NaT], freq="M") + idx = PeriodIndex(["2011-01", NaT], freq="M") - exp = np.array([pd.Period("2011-01", freq="M"), pd.NaT], dtype=object) + exp = np.array([Period("2011-01", freq="M"), NaT], dtype=object) tm.assert_numpy_array_equal(idx.astype(object).values, exp) tm.assert_numpy_array_equal(idx._mpl_repr(), exp) - exp = np.array([pd.Period("2011-01-01", freq="D"), pd.NaT], dtype=object) - idx = pd.PeriodIndex(["2011-01-01", pd.NaT], freq="D") + exp = np.array([Period("2011-01-01", freq="D"), NaT], dtype=object) + idx = PeriodIndex(["2011-01-01", NaT], freq="D") tm.assert_numpy_array_equal(idx.astype(object).values, exp) tm.assert_numpy_array_equal(idx._mpl_repr(), exp) # TODO: de-duplicate this version (from test_ops) with the one above # (from test_period) def test_astype_object2(self): - idx = pd.period_range(start="2013-01-01", periods=4, freq="M", name="idx") + idx = period_range(start="2013-01-01", periods=4, freq="M", name="idx") expected_list = [ - pd.Period("2013-01-31", freq="M"), - pd.Period("2013-02-28", freq="M"), - pd.Period("2013-03-31", freq="M"), - pd.Period("2013-04-30", freq="M"), + Period("2013-01-31", freq="M"), + Period("2013-02-28", freq="M"), + Period("2013-03-31", freq="M"), + Period("2013-04-30", freq="M"), ] - expected = pd.Index(expected_list, dtype=object, name="idx") + expected = Index(expected_list, dtype=object, name="idx") result = idx.astype(object) assert isinstance(result, Index) assert result.dtype == object @@ -94,31 +95,31 @@ def test_astype_object2(self): ["2013-01-01", "2013-01-02", "NaT", "2013-01-04"], freq="D", name="idx" ) expected_list = [ - pd.Period("2013-01-01", freq="D"), - pd.Period("2013-01-02", freq="D"), - pd.Period("NaT", freq="D"), - pd.Period("2013-01-04", freq="D"), + Period("2013-01-01", freq="D"), + Period("2013-01-02", freq="D"), + Period("NaT", freq="D"), + Period("2013-01-04", freq="D"), ] - expected = pd.Index(expected_list, dtype=object, name="idx") + expected = Index(expected_list, dtype=object, name="idx") result = idx.astype(object) assert isinstance(result, Index) assert result.dtype == object tm.assert_index_equal(result, expected) for i in [0, 1, 3]: assert result[i] == expected[i] - assert result[2] is pd.NaT + assert result[2] is NaT assert result.name == expected.name result_list = idx.tolist() for i in [0, 1, 3]: assert result_list[i] == expected_list[i] - assert result_list[2] is pd.NaT + assert result_list[2] is NaT def test_astype_category(self): - obj = pd.period_range("2000", periods=2) + obj = period_range("2000", periods=2) result = obj.astype("category") - expected = pd.CategoricalIndex( - [pd.Period("2000-01-01", freq="D"), pd.Period("2000-01-02", freq="D")] + expected = CategoricalIndex( + [Period("2000-01-01", freq="D"), Period("2000-01-02", freq="D")] ) tm.assert_index_equal(result, expected) @@ -127,9 +128,9 @@ def test_astype_category(self): tm.assert_categorical_equal(result, expected) def test_astype_array_fallback(self): - obj = pd.period_range("2000", periods=2) + obj = period_range("2000", periods=2) result = obj.astype(bool) - expected = pd.Index(np.array([True, True])) + expected = Index(np.array([True, True])) tm.assert_index_equal(result, expected) result = obj._data.astype(bool) diff --git a/pandas/tests/indexes/period/test_constructors.py b/pandas/tests/indexes/period/test_constructors.py index dcd3c8e946e9a..fcbadce3d63b1 100644 --- a/pandas/tests/indexes/period/test_constructors.py +++ b/pandas/tests/indexes/period/test_constructors.py @@ -6,7 +6,16 @@ from pandas.core.dtypes.dtypes import PeriodDtype import pandas as pd -from pandas import Index, Period, PeriodIndex, Series, date_range, offsets, period_range +from pandas import ( + Index, + NaT, + Period, + PeriodIndex, + Series, + date_range, + offsets, + period_range, +) import pandas._testing as tm from pandas.core.arrays import PeriodArray @@ -14,27 +23,25 @@ class TestPeriodIndex: def test_construction_base_constructor(self): # GH 13664 - arr = [pd.Period("2011-01", freq="M"), pd.NaT, pd.Period("2011-03", freq="M")] - tm.assert_index_equal(pd.Index(arr), pd.PeriodIndex(arr)) - tm.assert_index_equal(pd.Index(np.array(arr)), pd.PeriodIndex(np.array(arr))) + arr = [Period("2011-01", freq="M"), NaT, Period("2011-03", freq="M")] + tm.assert_index_equal(Index(arr), PeriodIndex(arr)) + tm.assert_index_equal(Index(np.array(arr)), PeriodIndex(np.array(arr))) - arr = [np.nan, pd.NaT, pd.Period("2011-03", freq="M")] - tm.assert_index_equal(pd.Index(arr), pd.PeriodIndex(arr)) - tm.assert_index_equal(pd.Index(np.array(arr)), pd.PeriodIndex(np.array(arr))) + arr = [np.nan, NaT, Period("2011-03", freq="M")] + tm.assert_index_equal(Index(arr), PeriodIndex(arr)) + tm.assert_index_equal(Index(np.array(arr)), PeriodIndex(np.array(arr))) - arr = [pd.Period("2011-01", freq="M"), pd.NaT, pd.Period("2011-03", freq="D")] - tm.assert_index_equal(pd.Index(arr), pd.Index(arr, dtype=object)) + arr = [Period("2011-01", freq="M"), NaT, Period("2011-03", freq="D")] + tm.assert_index_equal(Index(arr), Index(arr, dtype=object)) - tm.assert_index_equal( - pd.Index(np.array(arr)), pd.Index(np.array(arr), dtype=object) - ) + tm.assert_index_equal(Index(np.array(arr)), Index(np.array(arr), dtype=object)) def test_base_constructor_with_period_dtype(self): dtype = PeriodDtype("D") values = ["2011-01-01", "2012-03-04", "2014-05-01"] - result = pd.Index(values, dtype=dtype) + result = Index(values, dtype=dtype) - expected = pd.PeriodIndex(values, dtype=dtype) + expected = PeriodIndex(values, dtype=dtype) tm.assert_index_equal(result, expected) @pytest.mark.parametrize( @@ -43,9 +50,9 @@ def test_base_constructor_with_period_dtype(self): def test_index_object_dtype(self, values_constructor): # Index(periods, dtype=object) is an Index (not an PeriodIndex) periods = [ - pd.Period("2011-01", freq="M"), - pd.NaT, - pd.Period("2011-03", freq="M"), + Period("2011-01", freq="M"), + NaT, + Period("2011-03", freq="M"), ] values = values_constructor(periods) result = Index(values, dtype=object) @@ -118,8 +125,8 @@ def test_constructor_arrays_negative_year(self): pindex = PeriodIndex(year=years, quarter=quarters) - tm.assert_index_equal(pindex.year, pd.Index(years)) - tm.assert_index_equal(pindex.quarter, pd.Index(quarters)) + tm.assert_index_equal(pindex.year, Index(years)) + tm.assert_index_equal(pindex.quarter, Index(quarters)) def test_constructor_invalid_quarters(self): msg = "Quarter must be 1 <= q <= 4" @@ -184,7 +191,7 @@ def test_constructor_datetime64arr(self): @pytest.mark.parametrize("box", [None, "series", "index"]) def test_constructor_datetime64arr_ok(self, box): # https://github.com/pandas-dev/pandas/issues/23438 - data = pd.date_range("2017", periods=4, freq="M") + data = date_range("2017", periods=4, freq="M") if box is None: data = data._values elif box == "series": @@ -226,52 +233,47 @@ def test_constructor_dtype(self): PeriodIndex(["2011-01"], freq="M", dtype="period[D]") def test_constructor_empty(self): - idx = pd.PeriodIndex([], freq="M") + idx = PeriodIndex([], freq="M") assert isinstance(idx, PeriodIndex) assert len(idx) == 0 assert idx.freq == "M" with pytest.raises(ValueError, match="freq not specified"): - pd.PeriodIndex([]) + PeriodIndex([]) def test_constructor_pi_nat(self): idx = PeriodIndex( - [Period("2011-01", freq="M"), pd.NaT, Period("2011-01", freq="M")] + [Period("2011-01", freq="M"), NaT, Period("2011-01", freq="M")] ) exp = PeriodIndex(["2011-01", "NaT", "2011-01"], freq="M") tm.assert_index_equal(idx, exp) idx = PeriodIndex( - np.array([Period("2011-01", freq="M"), pd.NaT, Period("2011-01", freq="M")]) + np.array([Period("2011-01", freq="M"), NaT, Period("2011-01", freq="M")]) ) tm.assert_index_equal(idx, exp) idx = PeriodIndex( - [pd.NaT, pd.NaT, Period("2011-01", freq="M"), Period("2011-01", freq="M")] + [NaT, NaT, Period("2011-01", freq="M"), Period("2011-01", freq="M")] ) exp = PeriodIndex(["NaT", "NaT", "2011-01", "2011-01"], freq="M") tm.assert_index_equal(idx, exp) idx = PeriodIndex( np.array( - [ - pd.NaT, - pd.NaT, - Period("2011-01", freq="M"), - Period("2011-01", freq="M"), - ] + [NaT, NaT, Period("2011-01", freq="M"), Period("2011-01", freq="M")] ) ) tm.assert_index_equal(idx, exp) - idx = PeriodIndex([pd.NaT, pd.NaT, "2011-01", "2011-01"], freq="M") + idx = PeriodIndex([NaT, NaT, "2011-01", "2011-01"], freq="M") tm.assert_index_equal(idx, exp) with pytest.raises(ValueError, match="freq not specified"): - PeriodIndex([pd.NaT, pd.NaT]) + PeriodIndex([NaT, NaT]) with pytest.raises(ValueError, match="freq not specified"): - PeriodIndex(np.array([pd.NaT, pd.NaT])) + PeriodIndex(np.array([NaT, NaT])) with pytest.raises(ValueError, match="freq not specified"): PeriodIndex(["NaT", "NaT"]) @@ -283,40 +285,36 @@ def test_constructor_incompat_freq(self): msg = "Input has different freq=D from PeriodIndex\\(freq=M\\)" with pytest.raises(IncompatibleFrequency, match=msg): - PeriodIndex( - [Period("2011-01", freq="M"), pd.NaT, Period("2011-01", freq="D")] - ) + PeriodIndex([Period("2011-01", freq="M"), NaT, Period("2011-01", freq="D")]) with pytest.raises(IncompatibleFrequency, match=msg): PeriodIndex( np.array( - [Period("2011-01", freq="M"), pd.NaT, Period("2011-01", freq="D")] + [Period("2011-01", freq="M"), NaT, Period("2011-01", freq="D")] ) ) - # first element is pd.NaT + # first element is NaT with pytest.raises(IncompatibleFrequency, match=msg): - PeriodIndex( - [pd.NaT, Period("2011-01", freq="M"), Period("2011-01", freq="D")] - ) + PeriodIndex([NaT, Period("2011-01", freq="M"), Period("2011-01", freq="D")]) with pytest.raises(IncompatibleFrequency, match=msg): PeriodIndex( np.array( - [pd.NaT, Period("2011-01", freq="M"), Period("2011-01", freq="D")] + [NaT, Period("2011-01", freq="M"), Period("2011-01", freq="D")] ) ) def test_constructor_mixed(self): - idx = PeriodIndex(["2011-01", pd.NaT, Period("2011-01", freq="M")]) + idx = PeriodIndex(["2011-01", NaT, Period("2011-01", freq="M")]) exp = PeriodIndex(["2011-01", "NaT", "2011-01"], freq="M") tm.assert_index_equal(idx, exp) - idx = PeriodIndex(["NaT", pd.NaT, Period("2011-01", freq="M")]) + idx = PeriodIndex(["NaT", NaT, Period("2011-01", freq="M")]) exp = PeriodIndex(["NaT", "NaT", "2011-01"], freq="M") tm.assert_index_equal(idx, exp) - idx = PeriodIndex([Period("2011-01-01", freq="D"), pd.NaT, "2012-01-01"]) + idx = PeriodIndex([Period("2011-01-01", freq="D"), NaT, "2012-01-01"]) exp = PeriodIndex(["2011-01-01", "NaT", "2012-01-01"], freq="D") tm.assert_index_equal(idx, exp) @@ -349,11 +347,11 @@ def test_constructor_simple_new_empty(self): @pytest.mark.parametrize("floats", [[1.1, 2.1], np.array([1.1, 2.1])]) def test_constructor_floats(self, floats): with pytest.raises(AssertionError, match="