From 1999da1961f66a7fb2543e4e13f9ad3104b3e97e Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 9 Nov 2021 14:16:16 -0800 Subject: [PATCH] CLN: misplaced indexing tests --- pandas/tests/arithmetic/test_datetime64.py | 33 +++++++++++++++++ .../indexes/datetimes/test_partial_slicing.py | 35 ------------------- pandas/tests/indexes/period/test_indexing.py | 7 +--- pandas/tests/indexes/period/test_period.py | 6 ++-- pandas/tests/indexes/timedeltas/test_ops.py | 11 ------ pandas/tests/indexing/test_loc.py | 6 ++++ pandas/tests/series/indexing/test_indexing.py | 14 -------- pandas/tests/series/test_api.py | 9 +++++ pandas/tests/series/test_constructors.py | 15 ++++++++ 9 files changed, 67 insertions(+), 69 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index 82f1e60f0aea5..0b36a14d30184 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -361,6 +361,39 @@ def test_dt64arr_timestamp_equality(self, box_with_array): expected = tm.box_expected([False, False, False], xbox) tm.assert_equal(result, expected) + @pytest.mark.parametrize( + "datetimelike", + [ + Timestamp("20130101"), + datetime(2013, 1, 1), + np.datetime64("2013-01-01T00:00", "ns"), + ], + ) + @pytest.mark.parametrize( + "op,expected", + [ + (operator.lt, [True, False, False, False]), + (operator.le, [True, True, False, False]), + (operator.eq, [False, True, False, False]), + (operator.gt, [False, False, False, True]), + ], + ) + def test_dt64_compare_datetime_scalar(self, datetimelike, op, expected): + # GH#17965, test for ability to compare datetime64[ns] columns + # to datetimelike + ser = Series( + [ + Timestamp("20120101"), + Timestamp("20130101"), + np.nan, + Timestamp("20130103"), + ], + name="A", + ) + result = op(ser, datetimelike) + expected = Series(expected, name="A") + tm.assert_series_equal(result, expected) + class TestDatetimeIndexComparisons: diff --git a/pandas/tests/indexes/datetimes/test_partial_slicing.py b/pandas/tests/indexes/datetimes/test_partial_slicing.py index 896c43db5e356..2f32f9e18311d 100644 --- a/pandas/tests/indexes/datetimes/test_partial_slicing.py +++ b/pandas/tests/indexes/datetimes/test_partial_slicing.py @@ -1,7 +1,6 @@ """ test partial slicing on Series/Frame """ from datetime import datetime -import operator import numpy as np import pytest @@ -412,40 +411,6 @@ def test_loc_datetime_length_one(self): result = df.loc["2016-10-01T00:00:00":] tm.assert_frame_equal(result, df) - @pytest.mark.parametrize( - "datetimelike", - [ - Timestamp("20130101"), - datetime(2013, 1, 1), - np.datetime64("2013-01-01T00:00", "ns"), - ], - ) - @pytest.mark.parametrize( - "op,expected", - [ - (operator.lt, [True, False, False, False]), - (operator.le, [True, True, False, False]), - (operator.eq, [False, True, False, False]), - (operator.gt, [False, False, False, True]), - ], - ) - def test_selection_by_datetimelike(self, datetimelike, op, expected): - # GH issue #17965, test for ability to compare datetime64[ns] columns - # to datetimelike - df = DataFrame( - { - "A": [ - Timestamp("20120101"), - Timestamp("20130101"), - np.nan, - Timestamp("20130103"), - ] - } - ) - result = op(df.A, datetimelike) - expected = Series(expected, name="A") - tm.assert_series_equal(result, expected) - @pytest.mark.parametrize( "start", [ diff --git a/pandas/tests/indexes/period/test_indexing.py b/pandas/tests/indexes/period/test_indexing.py index dfa750bf933a0..1b5e64bca03a0 100644 --- a/pandas/tests/indexes/period/test_indexing.py +++ b/pandas/tests/indexes/period/test_indexing.py @@ -205,6 +205,7 @@ def test_getitem_seconds(self): # GH7116 # these show deprecations as we are trying # to slice with non-integer indexers + # FIXME: don't leave commented-out # with pytest.raises(IndexError): # idx[v] continue @@ -814,12 +815,6 @@ def test_get_value(self): result2 = idx2.get_value(input2, p1) tm.assert_series_equal(result2, expected2) - def test_loc_str(self): - # https://github.com/pandas-dev/pandas/issues/33964 - index = period_range(start="2000", periods=20, freq="B") - series = Series(range(20), index=index) - assert series.loc["2000-01-14"] == 9 - @pytest.mark.parametrize("freq", ["H", "D"]) def test_get_value_datetime_hourly(self, freq): # get_loc and get_value should treat datetime objects symmetrically diff --git a/pandas/tests/indexes/period/test_period.py b/pandas/tests/indexes/period/test_period.py index e6c31d22e626f..a7dad4e7f352c 100644 --- a/pandas/tests/indexes/period/test_period.py +++ b/pandas/tests/indexes/period/test_period.py @@ -211,7 +211,7 @@ def _check_all_fields(self, periodindex): ] periods = list(periodindex) - s = Series(periodindex) + ser = Series(periodindex) for field in fields: field_idx = getattr(periodindex, field) @@ -219,10 +219,10 @@ def _check_all_fields(self, periodindex): for x, val in zip(periods, field_idx): assert getattr(x, field) == val - if len(s) == 0: + if len(ser) == 0: continue - field_s = getattr(s.dt, field) + field_s = getattr(ser.dt, field) assert len(periodindex) == len(field_s) for x, val in zip(periods, field_s): assert getattr(x, field) == val diff --git a/pandas/tests/indexes/timedeltas/test_ops.py b/pandas/tests/indexes/timedeltas/test_ops.py index 2a5051b2982bb..f5d601bcfbcd1 100644 --- a/pandas/tests/indexes/timedeltas/test_ops.py +++ b/pandas/tests/indexes/timedeltas/test_ops.py @@ -1,8 +1,6 @@ -import numpy as np import pytest from pandas import ( - Series, TimedeltaIndex, timedelta_range, ) @@ -30,15 +28,6 @@ def test_nonunique_contains(self): ): assert idx[0] in idx - def test_unknown_attribute(self): - # see gh-9680 - tdi = timedelta_range(start=0, periods=10, freq="1s") - ts = Series(np.random.normal(size=10), index=tdi) - assert "foo" not in ts.__dict__.keys() - msg = "'Series' object has no attribute 'foo'" - with pytest.raises(AttributeError, match=msg): - ts.foo - def test_infer_freq(self, freq_sample): # GH#11018 idx = timedelta_range("1", freq=freq_sample, periods=10) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index b0aa05371271b..ed9b5cc0850b9 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -2941,3 +2941,9 @@ def test_loc_set_multiple_items_in_multiple_new_columns(self): ) tm.assert_frame_equal(df, expected) + + def test_getitem_loc_str_periodindex(self): + # GH#33964 + index = pd.period_range(start="2000", periods=20, freq="B") + series = Series(range(20), index=index) + assert series.loc["2000-01-14"] == 9 diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index d77f831bee8bc..6c3587c7eeada 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -377,17 +377,3 @@ def test_frozenset_index(): assert s[idx1] == 2 s[idx1] = 3 assert s[idx1] == 3 - - -def test_boolean_index(): - # GH18579 - s1 = Series([1, 2, 3], index=[4, 5, 6]) - s2 = Series([1, 3, 2], index=s1 == 2) - tm.assert_series_equal(Series([1, 3, 2], [False, True, False]), s2) - - -def test_index_ndim_gt_1_raises(): - # GH18579 - df = DataFrame([[1, 2], [3, 4], [5, 6]], index=[3, 6, 9]) - with pytest.raises(ValueError, match="Index data must be 1-dimensional"): - Series([1, 3, 2], index=df) diff --git a/pandas/tests/series/test_api.py b/pandas/tests/series/test_api.py index b49c209a59a06..aaf98e46f2f09 100644 --- a/pandas/tests/series/test_api.py +++ b/pandas/tests/series/test_api.py @@ -182,3 +182,12 @@ def test_inspect_getmembers(self): ser = Series(dtype=object) with tm.assert_produces_warning(None): inspect.getmembers(ser) + + def test_unknown_attribute(self): + # GH#9680 + tdi = pd.timedelta_range(start=0, periods=10, freq="1s") + ser = Series(np.random.normal(size=10), index=tdi) + assert "foo" not in ser.__dict__.keys() + msg = "'Series' object has no attribute 'foo'" + with pytest.raises(AttributeError, match=msg): + ser.foo diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 2c33284df18c5..1b488b4cf0b77 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -154,6 +154,12 @@ def test_constructor(self, datetime_series): with pytest.raises(NotImplementedError, match=msg): Series(m) + def test_constructor_index_ndim_gt_1_raises(self): + # GH#18579 + df = DataFrame([[1, 2], [3, 4], [5, 6]], index=[3, 6, 9]) + with pytest.raises(ValueError, match="Index data must be 1-dimensional"): + Series([1, 3, 2], index=df) + @pytest.mark.parametrize("input_class", [list, dict, OrderedDict]) def test_constructor_empty(self, input_class): with tm.assert_produces_warning(FutureWarning): @@ -276,6 +282,15 @@ def test_constructor_list_like(self): result = Series(obj, index=[0, 1, 2]) tm.assert_series_equal(result, expected) + def test_constructor_boolean_index(self): + # GH#18579 + s1 = Series([1, 2, 3], index=[4, 5, 6]) + + index = s1 == 2 + result = Series([1, 3, 2], index=index) + expected = Series([1, 3, 2], index=[False, True, False]) + tm.assert_series_equal(result, expected) + @pytest.mark.parametrize("dtype", ["bool", "int32", "int64", "float64"]) def test_constructor_index_dtype(self, dtype): # GH 17088