diff --git a/pandas/tests/frame/methods/test_set_index.py b/pandas/tests/frame/methods/test_set_index.py index ca098296daf8d..b66a95bae51c5 100644 --- a/pandas/tests/frame/methods/test_set_index.py +++ b/pandas/tests/frame/methods/test_set_index.py @@ -684,3 +684,14 @@ def __str__(self) -> str: with pytest.raises(TypeError, match=msg): # custom label wrapped in list df.set_index([thing2]) + + def test_set_index_periodindex(self): + # GH#6631 + df = DataFrame(np.random.random(6)) + idx1 = period_range("2011/01/01", periods=6, freq="M") + idx2 = period_range("2013", periods=6, freq="A") + + df = df.set_index(idx1) + tm.assert_index_equal(df.index, idx1) + df = df.set_index(idx2) + tm.assert_index_equal(df.index, idx2) diff --git a/pandas/tests/indexes/datetimes/test_datetime.py b/pandas/tests/indexes/datetimes/test_datetime.py index 789510b452969..1c813d47e21a9 100644 --- a/pandas/tests/indexes/datetimes/test_datetime.py +++ b/pandas/tests/indexes/datetimes/test_datetime.py @@ -1,4 +1,4 @@ -from datetime import date, timedelta +from datetime import date import dateutil import numpy as np @@ -12,51 +12,6 @@ class TestDatetimeIndex: - def test_reindex_preserves_tz_if_target_is_empty_list_or_array(self): - # GH7774 - index = date_range("20130101", periods=3, tz="US/Eastern") - assert str(index.reindex([])[0].tz) == "US/Eastern" - assert str(index.reindex(np.array([]))[0].tz) == "US/Eastern" - - def test_reindex_with_same_tz(self): - # GH 32740 - rng_a = date_range("2010-01-01", "2010-01-02", periods=24, tz="utc") - rng_b = date_range("2010-01-01", "2010-01-02", periods=23, tz="utc") - result1, result2 = rng_a.reindex( - rng_b, method="nearest", tolerance=timedelta(seconds=20) - ) - expected_list1 = [ - "2010-01-01 00:00:00", - "2010-01-01 01:05:27.272727272", - "2010-01-01 02:10:54.545454545", - "2010-01-01 03:16:21.818181818", - "2010-01-01 04:21:49.090909090", - "2010-01-01 05:27:16.363636363", - "2010-01-01 06:32:43.636363636", - "2010-01-01 07:38:10.909090909", - "2010-01-01 08:43:38.181818181", - "2010-01-01 09:49:05.454545454", - "2010-01-01 10:54:32.727272727", - "2010-01-01 12:00:00", - "2010-01-01 13:05:27.272727272", - "2010-01-01 14:10:54.545454545", - "2010-01-01 15:16:21.818181818", - "2010-01-01 16:21:49.090909090", - "2010-01-01 17:27:16.363636363", - "2010-01-01 18:32:43.636363636", - "2010-01-01 19:38:10.909090909", - "2010-01-01 20:43:38.181818181", - "2010-01-01 21:49:05.454545454", - "2010-01-01 22:54:32.727272727", - "2010-01-02 00:00:00", - ] - expected1 = DatetimeIndex( - expected_list1, dtype="datetime64[ns, UTC]", freq=None - ) - expected2 = np.array([0] + [-1] * 21 + [23], dtype=np.dtype("intp")) - tm.assert_index_equal(result1, expected1) - tm.assert_numpy_array_equal(result2, expected2) - def test_time_loc(self): # GH8667 from datetime import time diff --git a/pandas/tests/indexes/datetimes/test_reindex.py b/pandas/tests/indexes/datetimes/test_reindex.py new file mode 100644 index 0000000000000..fa847ad072ada --- /dev/null +++ b/pandas/tests/indexes/datetimes/test_reindex.py @@ -0,0 +1,53 @@ +from datetime import timedelta + +import numpy as np + +from pandas import DatetimeIndex, date_range +import pandas._testing as tm + + +class TestDatetimeIndexReindex: + def test_reindex_preserves_tz_if_target_is_empty_list_or_array(self): + # GH#7774 + index = date_range("2013-01-01", periods=3, tz="US/Eastern") + assert str(index.reindex([])[0].tz) == "US/Eastern" + assert str(index.reindex(np.array([]))[0].tz) == "US/Eastern" + + def test_reindex_with_same_tz_nearest(self): + # GH#32740 + rng_a = date_range("2010-01-01", "2010-01-02", periods=24, tz="utc") + rng_b = date_range("2010-01-01", "2010-01-02", periods=23, tz="utc") + result1, result2 = rng_a.reindex( + rng_b, method="nearest", tolerance=timedelta(seconds=20) + ) + expected_list1 = [ + "2010-01-01 00:00:00", + "2010-01-01 01:05:27.272727272", + "2010-01-01 02:10:54.545454545", + "2010-01-01 03:16:21.818181818", + "2010-01-01 04:21:49.090909090", + "2010-01-01 05:27:16.363636363", + "2010-01-01 06:32:43.636363636", + "2010-01-01 07:38:10.909090909", + "2010-01-01 08:43:38.181818181", + "2010-01-01 09:49:05.454545454", + "2010-01-01 10:54:32.727272727", + "2010-01-01 12:00:00", + "2010-01-01 13:05:27.272727272", + "2010-01-01 14:10:54.545454545", + "2010-01-01 15:16:21.818181818", + "2010-01-01 16:21:49.090909090", + "2010-01-01 17:27:16.363636363", + "2010-01-01 18:32:43.636363636", + "2010-01-01 19:38:10.909090909", + "2010-01-01 20:43:38.181818181", + "2010-01-01 21:49:05.454545454", + "2010-01-01 22:54:32.727272727", + "2010-01-02 00:00:00", + ] + expected1 = DatetimeIndex( + expected_list1, dtype="datetime64[ns, UTC]", freq=None + ) + expected2 = np.array([0] + [-1] * 21 + [23], dtype=np.dtype("intp")) + tm.assert_index_equal(result1, expected1) + tm.assert_numpy_array_equal(result2, expected2) diff --git a/pandas/tests/indexes/period/test_period.py b/pandas/tests/indexes/period/test_period.py index f4773e885829e..273d02ca27d7b 100644 --- a/pandas/tests/indexes/period/test_period.py +++ b/pandas/tests/indexes/period/test_period.py @@ -274,46 +274,6 @@ def _check_all_fields(self, periodindex): for x, val in zip(periods, field_s): assert getattr(x, field) == val - def test_period_set_index_reindex(self): - # GH 6631 - df = DataFrame(np.random.random(6)) - idx1 = period_range("2011/01/01", periods=6, freq="M") - idx2 = period_range("2013", periods=6, freq="A") - - df = df.set_index(idx1) - tm.assert_index_equal(df.index, idx1) - df = df.set_index(idx2) - tm.assert_index_equal(df.index, idx2) - - @pytest.mark.parametrize( - "p_values, o_values, values, expected_values", - [ - ( - [Period("2019Q1", "Q-DEC"), Period("2019Q2", "Q-DEC")], - [Period("2019Q1", "Q-DEC"), Period("2019Q2", "Q-DEC"), "All"], - [1.0, 1.0], - [1.0, 1.0, np.nan], - ), - ( - [Period("2019Q1", "Q-DEC"), Period("2019Q2", "Q-DEC")], - [Period("2019Q1", "Q-DEC"), Period("2019Q2", "Q-DEC")], - [1.0, 1.0], - [1.0, 1.0], - ), - ], - ) - def test_period_reindex_with_object( - self, p_values, o_values, values, expected_values - ): - # GH 28337 - period_index = PeriodIndex(p_values) - object_index = Index(o_values) - - s = Series(values, index=period_index) - result = s.reindex(object_index) - expected = Series(expected_values, index=object_index) - tm.assert_series_equal(result, expected) - def test_is_(self): create_index = lambda: period_range(freq="A", start="1/1/2001", end="12/1/2009") index = create_index() diff --git a/pandas/tests/series/methods/test_reindex.py b/pandas/tests/series/methods/test_reindex.py index 0415434f01fcf..22efc99805983 100644 --- a/pandas/tests/series/methods/test_reindex.py +++ b/pandas/tests/series/methods/test_reindex.py @@ -1,8 +1,16 @@ import numpy as np import pytest -import pandas as pd -from pandas import Categorical, Series, date_range, isna +from pandas import ( + Categorical, + Index, + NaT, + Period, + PeriodIndex, + Series, + date_range, + isna, +) import pandas._testing as tm @@ -293,5 +301,33 @@ def test_reindex_datetimeindexes_tz_naive_and_aware(): def test_reindex_empty_series_tz_dtype(): # GH 20869 result = Series(dtype="datetime64[ns, UTC]").reindex([0, 1]) - expected = Series([pd.NaT] * 2, dtype="datetime64[ns, UTC]") + expected = Series([NaT] * 2, dtype="datetime64[ns, UTC]") tm.assert_equal(result, expected) + + +@pytest.mark.parametrize( + "p_values, o_values, values, expected_values", + [ + ( + [Period("2019Q1", "Q-DEC"), Period("2019Q2", "Q-DEC")], + [Period("2019Q1", "Q-DEC"), Period("2019Q2", "Q-DEC"), "All"], + [1.0, 1.0], + [1.0, 1.0, np.nan], + ), + ( + [Period("2019Q1", "Q-DEC"), Period("2019Q2", "Q-DEC")], + [Period("2019Q1", "Q-DEC"), Period("2019Q2", "Q-DEC")], + [1.0, 1.0], + [1.0, 1.0], + ), + ], +) +def test_reindex_periodindex_with_object(p_values, o_values, values, expected_values): + # GH#28337 + period_index = PeriodIndex(p_values) + object_index = Index(o_values) + + ser = Series(values, index=period_index) + result = ser.reindex(object_index) + expected = Series(expected_values, index=object_index) + tm.assert_series_equal(result, expected)