virtuelle Umgebungen teil20 und teil20a
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,138 @@
|
||||
import pytest
|
||||
|
||||
from pandas import (
|
||||
PeriodIndex,
|
||||
Series,
|
||||
period_range,
|
||||
)
|
||||
import pandas._testing as tm
|
||||
|
||||
|
||||
class TestPeriodIndex:
|
||||
def test_asfreq(self):
|
||||
pi1 = period_range(freq="A", 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("A", "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("A", "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("A", "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("A", "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("A", "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("A", "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)
|
@@ -0,0 +1,148 @@
|
||||
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):
|
||||
# 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)
|
||||
expected = Index([str(x) for x in idx], name="idx")
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
idx = period_range("1990", "2009", freq="A", 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):
|
||||
pi = PeriodIndex(["2011-01", "2011-02", "2011-03"], freq="M")
|
||||
|
||||
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(res, exp)
|
||||
assert res.freq == exp.freq
|
@@ -0,0 +1,54 @@
|
||||
import numpy as np
|
||||
|
||||
from pandas import (
|
||||
PeriodIndex,
|
||||
factorize,
|
||||
)
|
||||
import pandas._testing as tm
|
||||
|
||||
|
||||
class TestFactorize:
|
||||
def test_factorize(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)
|
||||
|
||||
idx2 = PeriodIndex(
|
||||
["2014-03", "2014-03", "2014-02", "2014-01", "2014-03", "2014-01"], 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)
|
||||
|
||||
def test_factorize_complex(self): # TODO: WTF is this test doing here?s
|
||||
# GH 17927
|
||||
array = [1, 2, 2 + 1j]
|
||||
msg = "factorize with argument that is not not a Series"
|
||||
with tm.assert_produces_warning(FutureWarning, match=msg):
|
||||
labels, uniques = factorize(array)
|
||||
|
||||
expected_labels = np.array([0, 1, 2], dtype=np.intp)
|
||||
tm.assert_numpy_array_equal(labels, expected_labels)
|
||||
|
||||
# Should return a complex dtype in the future
|
||||
expected_uniques = np.array([(1 + 0j), (2 + 0j), (2 + 1j)], dtype=object)
|
||||
tm.assert_numpy_array_equal(uniques, expected_uniques)
|
@@ -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="A")
|
||||
assert not index.is_full
|
||||
|
||||
index = PeriodIndex([2005, 2006, 2007], freq="A")
|
||||
assert index.is_full
|
||||
|
||||
index = PeriodIndex([2005, 2005, 2007], freq="A")
|
||||
assert not index.is_full
|
||||
|
||||
index = PeriodIndex([2005, 2005, 2006], freq="A")
|
||||
assert index.is_full
|
||||
|
||||
index = PeriodIndex([2006, 2005, 2005], freq="A")
|
||||
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="A", start="1/1/2001", end="12/1/2009")
|
||||
pi2 = period_range(freq="A", 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="A", start="1/1/2001", end="12/1/2009")
|
||||
pi2 = period_range(freq="A", 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="A", 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,132 @@
|
||||
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="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")
|
||||
|
||||
result = index.to_timestamp("D")
|
||||
expected = DatetimeIndex(
|
||||
[NaT, datetime(2011, 1, 1), datetime(2011, 2, 1)], 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: -2A"
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
result.to_period(freq="-2A")
|
||||
|
||||
def test_to_timestamp_preserve_name(self):
|
||||
index = period_range(freq="A", 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(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"], name="idx")
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = idx.to_timestamp(how="E")
|
||||
expected = DatetimeIndex(["2011-02-28", "NaT", "2011-03-31"], 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"], 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"
|
||||
)
|
||||
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"], 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