From 938f4dff2dc5ba8b23aecc4b230043d389c897ca Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 6 Oct 2021 20:12:26 -0700 Subject: [PATCH 1/2] REF: share test_isin --- pandas/tests/indexes/datetimelike.py | 12 ++++++++++++ pandas/tests/indexes/datetimes/test_datetime.py | 12 ------------ pandas/tests/indexes/timedeltas/test_timedelta.py | 13 ------------- 3 files changed, 12 insertions(+), 25 deletions(-) diff --git a/pandas/tests/indexes/datetimelike.py b/pandas/tests/indexes/datetimelike.py index 70156092eeabe..ecdbf01fd41c1 100644 --- a/pandas/tests/indexes/datetimelike.py +++ b/pandas/tests/indexes/datetimelike.py @@ -9,6 +9,18 @@ class DatetimeLike(Base): + def test_isin(self, simple_index): + index = simple_index[:4] + result = index.isin(index) + assert result.all() + + result = index.isin(list(index)) + assert result.all() + + result = index.isin([index[2], 5]) + expected = np.array([False, False, True, False]) + tm.assert_numpy_array_equal(result, expected) + def test_argsort_matches_array(self, simple_index): idx = simple_index idx = idx.insert(1, pd.NaT) diff --git a/pandas/tests/indexes/datetimes/test_datetime.py b/pandas/tests/indexes/datetimes/test_datetime.py index 17b80fbc0afc2..b220ce486f80b 100644 --- a/pandas/tests/indexes/datetimes/test_datetime.py +++ b/pandas/tests/indexes/datetimes/test_datetime.py @@ -154,18 +154,6 @@ def test_groupby_function_tuple_1677(self): result = monthly_group.mean() assert isinstance(result.index[0], tuple) - def test_isin(self): - index = tm.makeDateIndex(4) - result = index.isin(index) - assert result.all() - - result = index.isin(list(index)) - assert result.all() - - tm.assert_almost_equal( - index.isin([index[2], 5]), np.array([False, False, True, False]) - ) - def assert_index_parameters(self, index): assert index.freq == "40960N" assert index.inferred_freq == "40960N" diff --git a/pandas/tests/indexes/timedeltas/test_timedelta.py b/pandas/tests/indexes/timedeltas/test_timedelta.py index 33f0565c0b23b..111e1f8996eb8 100644 --- a/pandas/tests/indexes/timedeltas/test_timedelta.py +++ b/pandas/tests/indexes/timedeltas/test_timedelta.py @@ -49,19 +49,6 @@ def test_pickle_after_set_freq(self): res = tm.round_trip_pickle(tdi) tm.assert_index_equal(res, tdi) - def test_isin(self): - - index = tm.makeTimedeltaIndex(4) - result = index.isin(index) - assert result.all() - - result = index.isin(list(index)) - assert result.all() - - tm.assert_almost_equal( - index.isin([index[2], 5]), np.array([False, False, True, False]) - ) - def test_misc_coverage(self): rng = timedelta_range("1 day", periods=5) From 2532b008e2e96999235834bfb0b7c87e9b17df1b Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 6 Oct 2021 20:39:35 -0700 Subject: [PATCH 2/2] simplify, share, move misplaced tests --- .../tests/indexes/datetimes/test_indexing.py | 18 +++++++++++------- pandas/tests/indexes/datetimes/test_misc.py | 9 +++++++++ pandas/tests/indexes/period/test_period.py | 10 ---------- pandas/tests/indexes/timedeltas/test_pickle.py | 11 +++++++++++ .../tests/indexes/timedeltas/test_timedelta.py | 17 +++++------------ 5 files changed, 36 insertions(+), 29 deletions(-) create mode 100644 pandas/tests/indexes/timedeltas/test_pickle.py diff --git a/pandas/tests/indexes/datetimes/test_indexing.py b/pandas/tests/indexes/datetimes/test_indexing.py index 6eaf799ae2779..4ad85f7d4e30f 100644 --- a/pandas/tests/indexes/datetimes/test_indexing.py +++ b/pandas/tests/indexes/datetimes/test_indexing.py @@ -485,19 +485,23 @@ def test_get_loc(self): with pytest.raises(InvalidIndexError, match=r"slice\(None, 2, None\)"): idx.get_loc(slice(2)) - idx = pd.to_datetime(["2000-01-01", "2000-01-04"]) + idx = DatetimeIndex(["2000-01-01", "2000-01-04"]) assert idx.get_loc("2000-01-02", method="nearest") == 0 assert idx.get_loc("2000-01-03", method="nearest") == 1 assert idx.get_loc("2000-01", method="nearest") == slice(0, 2) + def test_get_loc_time_obj(self): # time indexing idx = date_range("2000-01-01", periods=24, freq="H") - tm.assert_numpy_array_equal( - idx.get_loc(time(12)), np.array([12]), check_dtype=False - ) - tm.assert_numpy_array_equal( - idx.get_loc(time(12, 30)), np.array([]), check_dtype=False - ) + + result = idx.get_loc(time(12)) + expected = np.array([12]) + tm.assert_numpy_array_equal(result, expected, check_dtype=False) + + result = idx.get_loc(time(12, 30)) + expected = np.array([]) + tm.assert_numpy_array_equal(result, expected, check_dtype=False) + msg = "cannot yet lookup inexact labels when key is a time object" with pytest.raises(NotImplementedError, match=msg): with tm.assert_produces_warning(FutureWarning, match="deprecated"): diff --git a/pandas/tests/indexes/datetimes/test_misc.py b/pandas/tests/indexes/datetimes/test_misc.py index 408ed2db316ca..647f7739b482a 100644 --- a/pandas/tests/indexes/datetimes/test_misc.py +++ b/pandas/tests/indexes/datetimes/test_misc.py @@ -156,6 +156,15 @@ def test_range_edges9(self): class TestDatetime64: + def test_no_millisecond_field(self): + msg = "type object 'DatetimeIndex' has no attribute 'millisecond'" + with pytest.raises(AttributeError, match=msg): + DatetimeIndex.millisecond + + msg = "'DatetimeIndex' object has no attribute 'millisecond'" + with pytest.raises(AttributeError, match=msg): + DatetimeIndex([]).millisecond + def test_datetimeindex_accessors(self): dti_naive = date_range(freq="D", start=datetime(1998, 1, 1), periods=365) # GH#13303 diff --git a/pandas/tests/indexes/period/test_period.py b/pandas/tests/indexes/period/test_period.py index 83c82c18f3d1e..e0f794a188ba3 100644 --- a/pandas/tests/indexes/period/test_period.py +++ b/pandas/tests/indexes/period/test_period.py @@ -4,7 +4,6 @@ from pandas._libs.tslibs.period import IncompatibleFrequency from pandas import ( - DatetimeIndex, Index, NaT, Period, @@ -49,15 +48,6 @@ def test_where(self): # This is handled in test_indexing pass - def test_no_millisecond_field(self): - msg = "type object 'DatetimeIndex' has no attribute 'millisecond'" - with pytest.raises(AttributeError, match=msg): - DatetimeIndex.millisecond - - msg = "'DatetimeIndex' object has no attribute 'millisecond'" - with pytest.raises(AttributeError, match=msg): - DatetimeIndex([]).millisecond - def test_make_time_series(self): index = period_range(freq="A", start="1/1/2001", end="12/1/2009") series = Series(1, index=index) diff --git a/pandas/tests/indexes/timedeltas/test_pickle.py b/pandas/tests/indexes/timedeltas/test_pickle.py new file mode 100644 index 0000000000000..befe709728bdd --- /dev/null +++ b/pandas/tests/indexes/timedeltas/test_pickle.py @@ -0,0 +1,11 @@ +from pandas import timedelta_range +import pandas._testing as tm + + +class TestPickle: + def test_pickle_after_set_freq(self): + tdi = timedelta_range("1 day", periods=4, freq="s") + tdi = tdi._with_freq(None) + + res = tm.round_trip_pickle(tdi) + tm.assert_index_equal(res, tdi) diff --git a/pandas/tests/indexes/timedeltas/test_timedelta.py b/pandas/tests/indexes/timedeltas/test_timedelta.py index 111e1f8996eb8..952036428d3c9 100644 --- a/pandas/tests/indexes/timedeltas/test_timedelta.py +++ b/pandas/tests/indexes/timedeltas/test_timedelta.py @@ -7,10 +7,10 @@ from pandas import ( Index, Int64Index, + NaT, Series, Timedelta, TimedeltaIndex, - date_range, timedelta_range, ) import pandas._testing as tm @@ -42,13 +42,6 @@ def test_numeric_compat(self): def test_shift(self): pass # this is handled in test_arithmetic.py - def test_pickle_after_set_freq(self): - tdi = timedelta_range("1 day", periods=4, freq="s") - tdi = tdi._with_freq(None) - - res = tm.round_trip_pickle(tdi) - tm.assert_index_equal(res, tdi) - def test_misc_coverage(self): rng = timedelta_range("1 day", periods=5) @@ -127,11 +120,11 @@ def test_freq_conversion(self): # doc example # series - td = Series(date_range("20130101", periods=4)) - Series( - date_range("20121201", periods=4) + scalar = Timedelta(days=31) + td = Series( + [scalar, scalar, scalar + timedelta(minutes=5, seconds=3), NaT], + dtype="m8[ns]", ) - td[2] += timedelta(minutes=5, seconds=3) - td[3] = np.nan result = td / np.timedelta64(1, "D") expected = Series([31, 31, (31 * 86400 + 5 * 60 + 3) / 86400.0, np.nan])