Initial Debugging Completed and Execution Successful
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,189 @@
|
||||
import re
|
||||
|
||||
import pytest
|
||||
|
||||
from pandas import (
|
||||
PeriodIndex,
|
||||
Series,
|
||||
period_range,
|
||||
)
|
||||
import pandas._testing as tm
|
||||
|
||||
from pandas.tseries import offsets
|
||||
|
||||
|
||||
class TestPeriodIndex:
|
||||
def test_asfreq(self):
|
||||
pi1 = period_range(freq="Y", start="1/1/2001", end="1/1/2001")
|
||||
pi2 = period_range(freq="Q", start="1/1/2001", end="1/1/2001")
|
||||
pi3 = period_range(freq="M", start="1/1/2001", end="1/1/2001")
|
||||
pi4 = period_range(freq="D", start="1/1/2001", end="1/1/2001")
|
||||
pi5 = period_range(freq="h", start="1/1/2001", end="1/1/2001 00:00")
|
||||
pi6 = period_range(freq="Min", start="1/1/2001", end="1/1/2001 00:00")
|
||||
pi7 = period_range(freq="s", start="1/1/2001", end="1/1/2001 00:00:00")
|
||||
|
||||
assert pi1.asfreq("Q", "s") == pi2
|
||||
assert pi1.asfreq("Q", "s") == pi2
|
||||
assert pi1.asfreq("M", "start") == pi3
|
||||
assert pi1.asfreq("D", "StarT") == pi4
|
||||
assert pi1.asfreq("h", "beGIN") == pi5
|
||||
assert pi1.asfreq("Min", "s") == pi6
|
||||
assert pi1.asfreq("s", "s") == pi7
|
||||
|
||||
assert pi2.asfreq("Y", "s") == pi1
|
||||
assert pi2.asfreq("M", "s") == pi3
|
||||
assert pi2.asfreq("D", "s") == pi4
|
||||
assert pi2.asfreq("h", "s") == pi5
|
||||
assert pi2.asfreq("Min", "s") == pi6
|
||||
assert pi2.asfreq("s", "s") == pi7
|
||||
|
||||
assert pi3.asfreq("Y", "s") == pi1
|
||||
assert pi3.asfreq("Q", "s") == pi2
|
||||
assert pi3.asfreq("D", "s") == pi4
|
||||
assert pi3.asfreq("h", "s") == pi5
|
||||
assert pi3.asfreq("Min", "s") == pi6
|
||||
assert pi3.asfreq("s", "s") == pi7
|
||||
|
||||
assert pi4.asfreq("Y", "s") == pi1
|
||||
assert pi4.asfreq("Q", "s") == pi2
|
||||
assert pi4.asfreq("M", "s") == pi3
|
||||
assert pi4.asfreq("h", "s") == pi5
|
||||
assert pi4.asfreq("Min", "s") == pi6
|
||||
assert pi4.asfreq("s", "s") == pi7
|
||||
|
||||
assert pi5.asfreq("Y", "s") == pi1
|
||||
assert pi5.asfreq("Q", "s") == pi2
|
||||
assert pi5.asfreq("M", "s") == pi3
|
||||
assert pi5.asfreq("D", "s") == pi4
|
||||
assert pi5.asfreq("Min", "s") == pi6
|
||||
assert pi5.asfreq("s", "s") == pi7
|
||||
|
||||
assert pi6.asfreq("Y", "s") == pi1
|
||||
assert pi6.asfreq("Q", "s") == pi2
|
||||
assert pi6.asfreq("M", "s") == pi3
|
||||
assert pi6.asfreq("D", "s") == pi4
|
||||
assert pi6.asfreq("h", "s") == pi5
|
||||
assert pi6.asfreq("s", "s") == pi7
|
||||
|
||||
assert pi7.asfreq("Y", "s") == pi1
|
||||
assert pi7.asfreq("Q", "s") == pi2
|
||||
assert pi7.asfreq("M", "s") == pi3
|
||||
assert pi7.asfreq("D", "s") == pi4
|
||||
assert pi7.asfreq("h", "s") == pi5
|
||||
assert pi7.asfreq("Min", "s") == pi6
|
||||
|
||||
msg = "How must be one of S or E"
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
pi7.asfreq("T", "foo")
|
||||
result1 = pi1.asfreq("3M")
|
||||
result2 = pi1.asfreq("M")
|
||||
expected = period_range(freq="M", start="2001-12", end="2001-12")
|
||||
tm.assert_numpy_array_equal(result1.asi8, expected.asi8)
|
||||
assert result1.freqstr == "3M"
|
||||
tm.assert_numpy_array_equal(result2.asi8, expected.asi8)
|
||||
assert result2.freqstr == "M"
|
||||
|
||||
def test_asfreq_nat(self):
|
||||
idx = PeriodIndex(["2011-01", "2011-02", "NaT", "2011-04"], freq="M")
|
||||
result = idx.asfreq(freq="Q")
|
||||
expected = PeriodIndex(["2011Q1", "2011Q1", "NaT", "2011Q2"], freq="Q")
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize("freq", ["D", "3D"])
|
||||
def test_asfreq_mult_pi(self, freq):
|
||||
pi = PeriodIndex(["2001-01", "2001-02", "NaT", "2001-03"], freq="2M")
|
||||
|
||||
result = pi.asfreq(freq)
|
||||
exp = PeriodIndex(["2001-02-28", "2001-03-31", "NaT", "2001-04-30"], freq=freq)
|
||||
tm.assert_index_equal(result, exp)
|
||||
assert result.freq == exp.freq
|
||||
|
||||
result = pi.asfreq(freq, how="S")
|
||||
exp = PeriodIndex(["2001-01-01", "2001-02-01", "NaT", "2001-03-01"], freq=freq)
|
||||
tm.assert_index_equal(result, exp)
|
||||
assert result.freq == exp.freq
|
||||
|
||||
def test_asfreq_combined_pi(self):
|
||||
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 = 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 = 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_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")
|
||||
tm.assert_index_equal(pi1.asfreq("M"), exp)
|
||||
tm.assert_index_equal(pi1.astype("period[M]"), exp)
|
||||
|
||||
exp = PeriodIndex(["2011-01", "2011-02", "2011-03"], freq="3M")
|
||||
tm.assert_index_equal(pi1.asfreq("3M"), exp)
|
||||
tm.assert_index_equal(pi1.astype("period[3M]"), exp)
|
||||
|
||||
def test_asfreq_with_different_n(self):
|
||||
ser = Series([1, 2], index=PeriodIndex(["2020-01", "2020-03"], freq="2M"))
|
||||
result = ser.asfreq("M")
|
||||
|
||||
excepted = Series([1, 2], index=PeriodIndex(["2020-02", "2020-04"], freq="M"))
|
||||
tm.assert_series_equal(result, excepted)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"freq",
|
||||
[
|
||||
"2BMS",
|
||||
"2YS-MAR",
|
||||
"2bh",
|
||||
],
|
||||
)
|
||||
def test_pi_asfreq_not_supported_frequency(self, freq):
|
||||
# GH#55785
|
||||
msg = f"{freq[1:]} is not supported as period frequency"
|
||||
|
||||
pi = PeriodIndex(["2020-01-01", "2021-01-01"], freq="M")
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
pi.asfreq(freq=freq)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"freq",
|
||||
[
|
||||
"2BME",
|
||||
"2YE-MAR",
|
||||
"2QE",
|
||||
],
|
||||
)
|
||||
def test_pi_asfreq_invalid_frequency(self, freq):
|
||||
# GH#55785
|
||||
msg = f"Invalid frequency: {freq}"
|
||||
|
||||
pi = PeriodIndex(["2020-01-01", "2021-01-01"], freq="M")
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
pi.asfreq(freq=freq)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"freq",
|
||||
[
|
||||
offsets.MonthBegin(2),
|
||||
offsets.BusinessMonthEnd(2),
|
||||
],
|
||||
)
|
||||
def test_pi_asfreq_invalid_baseoffset(self, freq):
|
||||
# GH#56945
|
||||
msg = re.escape(f"{freq} is not supported as period frequency")
|
||||
|
||||
pi = PeriodIndex(["2020-01-01", "2021-01-01"], freq="M")
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
pi.asfreq(freq=freq)
|
||||
@@ -0,0 +1,156 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from pandas import (
|
||||
CategoricalIndex,
|
||||
DatetimeIndex,
|
||||
Index,
|
||||
NaT,
|
||||
Period,
|
||||
PeriodIndex,
|
||||
period_range,
|
||||
)
|
||||
import pandas._testing as tm
|
||||
|
||||
|
||||
class TestPeriodIndexAsType:
|
||||
@pytest.mark.parametrize("dtype", [float, "timedelta64", "timedelta64[ns]"])
|
||||
def test_astype_raises(self, dtype):
|
||||
# GH#13149, GH#13209
|
||||
idx = PeriodIndex(["2016-05-16", "NaT", NaT, np.nan], freq="D")
|
||||
msg = "Cannot cast PeriodIndex to dtype"
|
||||
with pytest.raises(TypeError, match=msg):
|
||||
idx.astype(dtype)
|
||||
|
||||
def test_astype_conversion(self, using_infer_string):
|
||||
# GH#13149, GH#13209
|
||||
idx = PeriodIndex(["2016-05-16", "NaT", NaT, np.nan], freq="D", name="idx")
|
||||
|
||||
result = idx.astype(object)
|
||||
expected = Index(
|
||||
[Period("2016-05-16", freq="D")] + [Period(NaT, freq="D")] * 3,
|
||||
dtype="object",
|
||||
name="idx",
|
||||
)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = idx.astype(np.int64)
|
||||
expected = Index(
|
||||
[16937] + [-9223372036854775808] * 3, dtype=np.int64, name="idx"
|
||||
)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = idx.astype(str)
|
||||
if using_infer_string:
|
||||
expected = Index(
|
||||
[str(x) if x is not NaT else None for x in idx], name="idx", dtype="str"
|
||||
)
|
||||
else:
|
||||
expected = Index([str(x) for x in idx], name="idx", dtype=object)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
idx = period_range("1990", "2009", freq="Y", name="idx")
|
||||
result = idx.astype("i8")
|
||||
tm.assert_index_equal(result, Index(idx.asi8, name="idx"))
|
||||
tm.assert_numpy_array_equal(result.values, idx.asi8)
|
||||
|
||||
def test_astype_uint(self):
|
||||
arr = period_range("2000", periods=2, name="idx")
|
||||
|
||||
with pytest.raises(TypeError, match=r"Do obj.astype\('int64'\)"):
|
||||
arr.astype("uint64")
|
||||
with pytest.raises(TypeError, match=r"Do obj.astype\('int64'\)"):
|
||||
arr.astype("uint32")
|
||||
|
||||
def test_astype_object(self):
|
||||
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 = PeriodIndex(["2011-01", NaT], freq="M")
|
||||
|
||||
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([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 = period_range(start="2013-01-01", periods=4, freq="M", name="idx")
|
||||
expected_list = [
|
||||
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 = 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)
|
||||
assert result.name == expected.name
|
||||
assert idx.tolist() == expected_list
|
||||
|
||||
idx = PeriodIndex(
|
||||
["2013-01-01", "2013-01-02", "NaT", "2013-01-04"], freq="D", name="idx"
|
||||
)
|
||||
expected_list = [
|
||||
Period("2013-01-01", freq="D"),
|
||||
Period("2013-01-02", freq="D"),
|
||||
Period("NaT", freq="D"),
|
||||
Period("2013-01-04", freq="D"),
|
||||
]
|
||||
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 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 NaT
|
||||
|
||||
def test_astype_category(self):
|
||||
obj = period_range("2000", periods=2, name="idx")
|
||||
result = obj.astype("category")
|
||||
expected = CategoricalIndex(
|
||||
[Period("2000-01-01", freq="D"), Period("2000-01-02", freq="D")], name="idx"
|
||||
)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = obj._data.astype("category")
|
||||
expected = expected.values
|
||||
tm.assert_categorical_equal(result, expected)
|
||||
|
||||
def test_astype_array_fallback(self):
|
||||
obj = period_range("2000", periods=2, name="idx")
|
||||
result = obj.astype(bool)
|
||||
expected = Index(np.array([True, True]), name="idx")
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = obj._data.astype(bool)
|
||||
expected = np.array([True, True])
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
def test_period_astype_to_timestamp(self, unit):
|
||||
# GH#55958
|
||||
pi = PeriodIndex(["2011-01", "2011-02", "2011-03"], freq="M")
|
||||
|
||||
exp = DatetimeIndex(
|
||||
["2011-01-01", "2011-02-01", "2011-03-01"], tz="US/Eastern"
|
||||
).as_unit(unit)
|
||||
res = pi.astype(f"datetime64[{unit}, US/Eastern]")
|
||||
tm.assert_index_equal(res, exp)
|
||||
assert res.freq == exp.freq
|
||||
@@ -0,0 +1,41 @@
|
||||
import numpy as np
|
||||
|
||||
from pandas import PeriodIndex
|
||||
import pandas._testing as tm
|
||||
|
||||
|
||||
class TestFactorize:
|
||||
def test_factorize_period(self):
|
||||
idx1 = PeriodIndex(
|
||||
["2014-01", "2014-01", "2014-02", "2014-02", "2014-03", "2014-03"],
|
||||
freq="M",
|
||||
)
|
||||
|
||||
exp_arr = np.array([0, 0, 1, 1, 2, 2], dtype=np.intp)
|
||||
exp_idx = PeriodIndex(["2014-01", "2014-02", "2014-03"], freq="M")
|
||||
|
||||
arr, idx = idx1.factorize()
|
||||
tm.assert_numpy_array_equal(arr, exp_arr)
|
||||
tm.assert_index_equal(idx, exp_idx)
|
||||
|
||||
arr, idx = idx1.factorize(sort=True)
|
||||
tm.assert_numpy_array_equal(arr, exp_arr)
|
||||
tm.assert_index_equal(idx, exp_idx)
|
||||
|
||||
def test_factorize_period_nonmonotonic(self):
|
||||
idx2 = PeriodIndex(
|
||||
["2014-03", "2014-03", "2014-02", "2014-01", "2014-03", "2014-01"],
|
||||
freq="M",
|
||||
)
|
||||
exp_idx = PeriodIndex(["2014-01", "2014-02", "2014-03"], freq="M")
|
||||
|
||||
exp_arr = np.array([2, 2, 1, 0, 2, 0], dtype=np.intp)
|
||||
arr, idx = idx2.factorize(sort=True)
|
||||
tm.assert_numpy_array_equal(arr, exp_arr)
|
||||
tm.assert_index_equal(idx, exp_idx)
|
||||
|
||||
exp_arr = np.array([0, 0, 1, 2, 0, 2], dtype=np.intp)
|
||||
exp_idx = PeriodIndex(["2014-03", "2014-02", "2014-01"], freq="M")
|
||||
arr, idx = idx2.factorize()
|
||||
tm.assert_numpy_array_equal(arr, exp_arr)
|
||||
tm.assert_index_equal(idx, exp_idx)
|
||||
@@ -0,0 +1,41 @@
|
||||
from pandas import (
|
||||
Index,
|
||||
NaT,
|
||||
Period,
|
||||
PeriodIndex,
|
||||
)
|
||||
import pandas._testing as tm
|
||||
|
||||
|
||||
class TestFillNA:
|
||||
def test_fillna_period(self):
|
||||
# GH#11343
|
||||
idx = PeriodIndex(["2011-01-01 09:00", NaT, "2011-01-01 11:00"], freq="h")
|
||||
|
||||
exp = PeriodIndex(
|
||||
["2011-01-01 09:00", "2011-01-01 10:00", "2011-01-01 11:00"], freq="h"
|
||||
)
|
||||
result = idx.fillna(Period("2011-01-01 10:00", freq="h"))
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
exp = Index(
|
||||
[
|
||||
Period("2011-01-01 09:00", freq="h"),
|
||||
"x",
|
||||
Period("2011-01-01 11:00", freq="h"),
|
||||
],
|
||||
dtype=object,
|
||||
)
|
||||
result = idx.fillna("x")
|
||||
tm.assert_index_equal(result, exp)
|
||||
|
||||
exp = Index(
|
||||
[
|
||||
Period("2011-01-01 09:00", freq="h"),
|
||||
Period("2011-01-01", freq="D"),
|
||||
Period("2011-01-01 11:00", freq="h"),
|
||||
],
|
||||
dtype=object,
|
||||
)
|
||||
result = idx.fillna(Period("2011-01-01", freq="D"))
|
||||
tm.assert_index_equal(result, exp)
|
||||
@@ -0,0 +1,18 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from pandas import (
|
||||
NaT,
|
||||
PeriodIndex,
|
||||
period_range,
|
||||
)
|
||||
import pandas._testing as tm
|
||||
|
||||
|
||||
class TestInsert:
|
||||
@pytest.mark.parametrize("na", [np.nan, NaT, None])
|
||||
def test_insert(self, na):
|
||||
# GH#18295 (test missing)
|
||||
expected = PeriodIndex(["2017Q1", NaT, "2017Q2", "2017Q3", "2017Q4"], freq="Q")
|
||||
result = period_range("2017Q1", periods=4, freq="Q").insert(1, na)
|
||||
tm.assert_index_equal(result, expected)
|
||||
@@ -0,0 +1,23 @@
|
||||
import pytest
|
||||
|
||||
from pandas import PeriodIndex
|
||||
|
||||
|
||||
def test_is_full():
|
||||
index = PeriodIndex([2005, 2007, 2009], freq="Y")
|
||||
assert not index.is_full
|
||||
|
||||
index = PeriodIndex([2005, 2006, 2007], freq="Y")
|
||||
assert index.is_full
|
||||
|
||||
index = PeriodIndex([2005, 2005, 2007], freq="Y")
|
||||
assert not index.is_full
|
||||
|
||||
index = PeriodIndex([2005, 2005, 2006], freq="Y")
|
||||
assert index.is_full
|
||||
|
||||
index = PeriodIndex([2006, 2005, 2005], freq="Y")
|
||||
with pytest.raises(ValueError, match="Index is not monotonic"):
|
||||
index.is_full
|
||||
|
||||
assert index[:0].is_full
|
||||
@@ -0,0 +1,26 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from pandas import (
|
||||
PeriodIndex,
|
||||
period_range,
|
||||
)
|
||||
import pandas._testing as tm
|
||||
|
||||
|
||||
class TestRepeat:
|
||||
@pytest.mark.parametrize("use_numpy", [True, False])
|
||||
@pytest.mark.parametrize(
|
||||
"index",
|
||||
[
|
||||
period_range("2000-01-01", periods=3, freq="D"),
|
||||
period_range("2001-01-01", periods=3, freq="2D"),
|
||||
PeriodIndex(["2001-01", "NaT", "2003-01"], freq="M"),
|
||||
],
|
||||
)
|
||||
def test_repeat_freqstr(self, index, use_numpy):
|
||||
# GH#10183
|
||||
expected = PeriodIndex([per for per in index for _ in range(3)])
|
||||
result = np.repeat(index, 3) if use_numpy else index.repeat(3)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.freqstr == index.freqstr
|
||||
@@ -0,0 +1,122 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from pandas import (
|
||||
PeriodIndex,
|
||||
period_range,
|
||||
)
|
||||
import pandas._testing as tm
|
||||
|
||||
|
||||
class TestPeriodIndexShift:
|
||||
# ---------------------------------------------------------------
|
||||
# PeriodIndex.shift is used by __add__ and __sub__
|
||||
|
||||
def test_pi_shift_ndarray(self):
|
||||
idx = PeriodIndex(
|
||||
["2011-01", "2011-02", "NaT", "2011-04"], freq="M", name="idx"
|
||||
)
|
||||
result = idx.shift(np.array([1, 2, 3, 4]))
|
||||
expected = PeriodIndex(
|
||||
["2011-02", "2011-04", "NaT", "2011-08"], freq="M", name="idx"
|
||||
)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = idx.shift(np.array([1, -2, 3, -4]))
|
||||
expected = PeriodIndex(
|
||||
["2011-02", "2010-12", "NaT", "2010-12"], freq="M", name="idx"
|
||||
)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_shift(self):
|
||||
pi1 = period_range(freq="Y", start="1/1/2001", end="12/1/2009")
|
||||
pi2 = period_range(freq="Y", start="1/1/2002", end="12/1/2010")
|
||||
|
||||
tm.assert_index_equal(pi1.shift(0), pi1)
|
||||
|
||||
assert len(pi1) == len(pi2)
|
||||
tm.assert_index_equal(pi1.shift(1), pi2)
|
||||
|
||||
pi1 = period_range(freq="Y", start="1/1/2001", end="12/1/2009")
|
||||
pi2 = period_range(freq="Y", start="1/1/2000", end="12/1/2008")
|
||||
assert len(pi1) == len(pi2)
|
||||
tm.assert_index_equal(pi1.shift(-1), pi2)
|
||||
|
||||
pi1 = period_range(freq="M", start="1/1/2001", end="12/1/2009")
|
||||
pi2 = period_range(freq="M", start="2/1/2001", end="1/1/2010")
|
||||
assert len(pi1) == len(pi2)
|
||||
tm.assert_index_equal(pi1.shift(1), pi2)
|
||||
|
||||
pi1 = period_range(freq="M", start="1/1/2001", end="12/1/2009")
|
||||
pi2 = period_range(freq="M", start="12/1/2000", end="11/1/2009")
|
||||
assert len(pi1) == len(pi2)
|
||||
tm.assert_index_equal(pi1.shift(-1), pi2)
|
||||
|
||||
pi1 = period_range(freq="D", start="1/1/2001", end="12/1/2009")
|
||||
pi2 = period_range(freq="D", start="1/2/2001", end="12/2/2009")
|
||||
assert len(pi1) == len(pi2)
|
||||
tm.assert_index_equal(pi1.shift(1), pi2)
|
||||
|
||||
pi1 = period_range(freq="D", start="1/1/2001", end="12/1/2009")
|
||||
pi2 = period_range(freq="D", start="12/31/2000", end="11/30/2009")
|
||||
assert len(pi1) == len(pi2)
|
||||
tm.assert_index_equal(pi1.shift(-1), pi2)
|
||||
|
||||
def test_shift_corner_cases(self):
|
||||
# GH#9903
|
||||
idx = PeriodIndex([], name="xxx", freq="h")
|
||||
|
||||
msg = "`freq` argument is not supported for PeriodIndex.shift"
|
||||
with pytest.raises(TypeError, match=msg):
|
||||
# period shift doesn't accept freq
|
||||
idx.shift(1, freq="h")
|
||||
|
||||
tm.assert_index_equal(idx.shift(0), idx)
|
||||
tm.assert_index_equal(idx.shift(3), idx)
|
||||
|
||||
idx = PeriodIndex(
|
||||
["2011-01-01 10:00", "2011-01-01 11:00", "2011-01-01 12:00"],
|
||||
name="xxx",
|
||||
freq="h",
|
||||
)
|
||||
tm.assert_index_equal(idx.shift(0), idx)
|
||||
exp = PeriodIndex(
|
||||
["2011-01-01 13:00", "2011-01-01 14:00", "2011-01-01 15:00"],
|
||||
name="xxx",
|
||||
freq="h",
|
||||
)
|
||||
tm.assert_index_equal(idx.shift(3), exp)
|
||||
exp = PeriodIndex(
|
||||
["2011-01-01 07:00", "2011-01-01 08:00", "2011-01-01 09:00"],
|
||||
name="xxx",
|
||||
freq="h",
|
||||
)
|
||||
tm.assert_index_equal(idx.shift(-3), exp)
|
||||
|
||||
def test_shift_nat(self):
|
||||
idx = PeriodIndex(
|
||||
["2011-01", "2011-02", "NaT", "2011-04"], freq="M", name="idx"
|
||||
)
|
||||
result = idx.shift(1)
|
||||
expected = PeriodIndex(
|
||||
["2011-02", "2011-03", "NaT", "2011-05"], freq="M", name="idx"
|
||||
)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
|
||||
def test_shift_gh8083(self):
|
||||
# test shift for PeriodIndex
|
||||
# GH#8083
|
||||
drange = period_range("20130101", periods=5, freq="D")
|
||||
result = drange.shift(1)
|
||||
expected = PeriodIndex(
|
||||
["2013-01-02", "2013-01-03", "2013-01-04", "2013-01-05", "2013-01-06"],
|
||||
freq="D",
|
||||
)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_shift_periods(self):
|
||||
# GH #22458 : argument 'n' was deprecated in favor of 'periods'
|
||||
idx = period_range(freq="Y", start="1/1/2001", end="12/1/2009")
|
||||
tm.assert_index_equal(idx.shift(periods=0), idx)
|
||||
tm.assert_index_equal(idx.shift(0), idx)
|
||||
@@ -0,0 +1,142 @@
|
||||
from datetime import datetime
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from pandas import (
|
||||
DatetimeIndex,
|
||||
NaT,
|
||||
PeriodIndex,
|
||||
Timedelta,
|
||||
Timestamp,
|
||||
date_range,
|
||||
period_range,
|
||||
)
|
||||
import pandas._testing as tm
|
||||
|
||||
|
||||
class TestToTimestamp:
|
||||
def test_to_timestamp_non_contiguous(self):
|
||||
# GH#44100
|
||||
dti = date_range("2021-10-18", periods=9, freq="D")
|
||||
pi = dti.to_period()
|
||||
|
||||
result = pi[::2].to_timestamp()
|
||||
expected = dti[::2]
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = pi._data[::2].to_timestamp()
|
||||
expected = dti._data[::2]
|
||||
# TODO: can we get the freq to round-trip?
|
||||
tm.assert_datetime_array_equal(result, expected, check_freq=False)
|
||||
|
||||
result = pi[::-1].to_timestamp()
|
||||
expected = dti[::-1]
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = pi._data[::-1].to_timestamp()
|
||||
expected = dti._data[::-1]
|
||||
tm.assert_datetime_array_equal(result, expected, check_freq=False)
|
||||
|
||||
result = pi[::2][::-1].to_timestamp()
|
||||
expected = dti[::2][::-1]
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = pi._data[::2][::-1].to_timestamp()
|
||||
expected = dti._data[::2][::-1]
|
||||
tm.assert_datetime_array_equal(result, expected, check_freq=False)
|
||||
|
||||
def test_to_timestamp_freq(self):
|
||||
idx = period_range("2017", periods=12, freq="Y-DEC")
|
||||
result = idx.to_timestamp()
|
||||
expected = date_range("2017", periods=12, freq="YS-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")
|
||||
|
||||
result = index.to_timestamp("D")
|
||||
expected = DatetimeIndex(
|
||||
[NaT, datetime(2011, 1, 1), datetime(2011, 2, 1)],
|
||||
dtype="M8[ns]",
|
||||
name="idx",
|
||||
)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == "idx"
|
||||
|
||||
result2 = result.to_period(freq="M")
|
||||
tm.assert_index_equal(result2, index)
|
||||
assert result2.name == "idx"
|
||||
|
||||
result3 = result.to_period(freq="3M")
|
||||
exp = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="3M", name="idx")
|
||||
tm.assert_index_equal(result3, exp)
|
||||
assert result3.freqstr == "3M"
|
||||
|
||||
msg = "Frequency must be positive, because it represents span: -2Y"
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
result.to_period(freq="-2Y")
|
||||
|
||||
def test_to_timestamp_preserve_name(self):
|
||||
index = period_range(freq="Y", start="1/1/2001", end="12/1/2009", name="foo")
|
||||
assert index.name == "foo"
|
||||
|
||||
conv = index.to_timestamp("D")
|
||||
assert conv.name == "foo"
|
||||
|
||||
def test_to_timestamp_quarterly_bug(self):
|
||||
years = np.arange(1960, 2000).repeat(4)
|
||||
quarters = np.tile(list(range(1, 5)), 40)
|
||||
|
||||
pindex = PeriodIndex.from_fields(year=years, quarter=quarters)
|
||||
|
||||
stamps = pindex.to_timestamp("D", "end")
|
||||
expected = DatetimeIndex([x.to_timestamp("D", "end") for x in pindex])
|
||||
tm.assert_index_equal(stamps, expected)
|
||||
assert stamps.freq == expected.freq
|
||||
|
||||
def test_to_timestamp_pi_mult(self):
|
||||
idx = PeriodIndex(["2011-01", "NaT", "2011-02"], freq="2M", name="idx")
|
||||
|
||||
result = idx.to_timestamp()
|
||||
expected = DatetimeIndex(
|
||||
["2011-01-01", "NaT", "2011-02-01"], dtype="M8[ns]", name="idx"
|
||||
)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = idx.to_timestamp(how="E")
|
||||
expected = DatetimeIndex(
|
||||
["2011-02-28", "NaT", "2011-03-31"], dtype="M8[ns]", name="idx"
|
||||
)
|
||||
expected = expected + Timedelta(1, "D") - Timedelta(1, "ns")
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_to_timestamp_pi_combined(self):
|
||||
idx = period_range(start="2011", periods=2, freq="1D1h", name="idx")
|
||||
|
||||
result = idx.to_timestamp()
|
||||
expected = DatetimeIndex(
|
||||
["2011-01-01 00:00", "2011-01-02 01:00"], dtype="M8[ns]", name="idx"
|
||||
)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = idx.to_timestamp(how="E")
|
||||
expected = DatetimeIndex(
|
||||
["2011-01-02 00:59:59", "2011-01-03 01:59:59"], name="idx", dtype="M8[ns]"
|
||||
)
|
||||
expected = expected + Timedelta(1, "s") - Timedelta(1, "ns")
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = idx.to_timestamp(how="E", freq="h")
|
||||
expected = DatetimeIndex(
|
||||
["2011-01-02 00:00", "2011-01-03 01:00"], dtype="M8[ns]", name="idx"
|
||||
)
|
||||
expected = expected + Timedelta(1, "h") - Timedelta(1, "ns")
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_to_timestamp_1703(self):
|
||||
index = period_range("1/1/2012", periods=4, freq="D")
|
||||
|
||||
result = index.to_timestamp()
|
||||
assert result[0] == Timestamp("1/1/2012")
|
||||
Reference in New Issue
Block a user