diff --git a/pandas/tests/indexes/datetimes/test_date_range.py b/pandas/tests/indexes/datetimes/test_date_range.py index 54c931cd60d20..ba7e3c9d38861 100644 --- a/pandas/tests/indexes/datetimes/test_date_range.py +++ b/pandas/tests/indexes/datetimes/test_date_range.py @@ -14,7 +14,6 @@ import pandas as pd from pandas import DatetimeIndex, Timestamp, bdate_range, date_range, offsets -from pandas.tests.series.common import TestData import pandas.util.testing as tm from pandas.tseries.offsets import ( @@ -82,7 +81,7 @@ def test_date_range_timestamp_equiv_preserve_frequency(self): assert timestamp_instance == ts -class TestDateRanges(TestData): +class TestDateRanges: def test_date_range_nat(self): # GH#11587 msg = "Neither `start` nor `end` can be NaT" diff --git a/pandas/tests/series/common.py b/pandas/tests/series/common.py deleted file mode 100644 index 38c62e89f1873..0000000000000 --- a/pandas/tests/series/common.py +++ /dev/null @@ -1,30 +0,0 @@ -from pandas.util._decorators import cache_readonly - -import pandas as pd -import pandas.util.testing as tm - -_ts = tm.makeTimeSeries() - - -class TestData: - @cache_readonly - def ts(self): - ts = _ts.copy() - ts.name = "ts" - return ts - - @cache_readonly - def series(self): - series = tm.makeStringSeries() - series.name = "series" - return series - - @cache_readonly - def objSeries(self): - objSeries = tm.makeObjectSeries() - objSeries.name = "objects" - return objSeries - - @cache_readonly - def empty(self): - return pd.Series([], index=[]) diff --git a/pandas/tests/series/indexing/conftest.py b/pandas/tests/series/indexing/conftest.py deleted file mode 100644 index 9c7103c196d60..0000000000000 --- a/pandas/tests/series/indexing/conftest.py +++ /dev/null @@ -1,8 +0,0 @@ -import pytest - -from pandas.tests.series.common import TestData - - -@pytest.fixture(scope="module") -def test_data(): - return TestData() diff --git a/pandas/tests/series/indexing/test_alter_index.py b/pandas/tests/series/indexing/test_alter_index.py index b25fee0435da0..331747b6cd056 100644 --- a/pandas/tests/series/indexing/test_alter_index.py +++ b/pandas/tests/series/indexing/test_alter_index.py @@ -19,9 +19,9 @@ ], ) @pytest.mark.parametrize("fill", [None, -1]) -def test_align(test_data, first_slice, second_slice, join_type, fill): - a = test_data.ts[slice(*first_slice)] - b = test_data.ts[slice(*second_slice)] +def test_align(datetime_series, first_slice, second_slice, join_type, fill): + a = datetime_series[slice(*first_slice)] + b = datetime_series[slice(*second_slice)] aa, ab = a.align(b, join=join_type, fill_value=fill) @@ -61,10 +61,10 @@ def test_align(test_data, first_slice, second_slice, join_type, fill): @pytest.mark.parametrize("method", ["pad", "bfill"]) @pytest.mark.parametrize("limit", [None, 1]) def test_align_fill_method( - test_data, first_slice, second_slice, join_type, method, limit + datetime_series, first_slice, second_slice, join_type, method, limit ): - a = test_data.ts[slice(*first_slice)] - b = test_data.ts[slice(*second_slice)] + a = datetime_series[slice(*first_slice)] + b = datetime_series[slice(*second_slice)] aa, ab = a.align(b, join=join_type, method=method, limit=limit) @@ -79,44 +79,44 @@ def test_align_fill_method( assert_series_equal(ab, eb) -def test_align_nocopy(test_data): - b = test_data.ts[:5].copy() +def test_align_nocopy(datetime_series): + b = datetime_series[:5].copy() # do copy - a = test_data.ts.copy() + a = datetime_series.copy() ra, _ = a.align(b, join="left") ra[:5] = 5 assert not (a[:5] == 5).any() # do not copy - a = test_data.ts.copy() + a = datetime_series.copy() ra, _ = a.align(b, join="left", copy=False) ra[:5] = 5 assert (a[:5] == 5).all() # do copy - a = test_data.ts.copy() - b = test_data.ts[:5].copy() + a = datetime_series.copy() + b = datetime_series[:5].copy() _, rb = a.align(b, join="right") rb[:3] = 5 assert not (b[:3] == 5).any() # do not copy - a = test_data.ts.copy() - b = test_data.ts[:5].copy() + a = datetime_series.copy() + b = datetime_series[:5].copy() _, rb = a.align(b, join="right", copy=False) rb[:2] = 5 assert (b[:2] == 5).all() -def test_align_same_index(test_data): - a, b = test_data.ts.align(test_data.ts, copy=False) - assert a.index is test_data.ts.index - assert b.index is test_data.ts.index +def test_align_same_index(datetime_series): + a, b = datetime_series.align(datetime_series, copy=False) + assert a.index is datetime_series.index + assert b.index is datetime_series.index - a, b = test_data.ts.align(test_data.ts, copy=True) - assert a.index is not test_data.ts.index - assert b.index is not test_data.ts.index + a, b = datetime_series.align(datetime_series, copy=True) + assert a.index is not datetime_series.index + assert b.index is not datetime_series.index def test_align_multiindex(): @@ -154,43 +154,43 @@ def test_align_multiindex(): tm.assert_series_equal(expr, res2l) -def test_reindex(test_data): - identity = test_data.series.reindex(test_data.series.index) +def test_reindex(datetime_series, string_series): + identity = string_series.reindex(string_series.index) # __array_interface__ is not defined for older numpies # and on some pythons try: - assert np.may_share_memory(test_data.series.index, identity.index) + assert np.may_share_memory(string_series.index, identity.index) except AttributeError: pass - assert identity.index.is_(test_data.series.index) - assert identity.index.identical(test_data.series.index) + assert identity.index.is_(string_series.index) + assert identity.index.identical(string_series.index) - subIndex = test_data.series.index[10:20] - subSeries = test_data.series.reindex(subIndex) + subIndex = string_series.index[10:20] + subSeries = string_series.reindex(subIndex) for idx, val in subSeries.items(): - assert val == test_data.series[idx] + assert val == string_series[idx] - subIndex2 = test_data.ts.index[10:20] - subTS = test_data.ts.reindex(subIndex2) + subIndex2 = datetime_series.index[10:20] + subTS = datetime_series.reindex(subIndex2) for idx, val in subTS.items(): - assert val == test_data.ts[idx] - stuffSeries = test_data.ts.reindex(subIndex) + assert val == datetime_series[idx] + stuffSeries = datetime_series.reindex(subIndex) assert np.isnan(stuffSeries).all() # This is extremely important for the Cython code to not screw up - nonContigIndex = test_data.ts.index[::2] - subNonContig = test_data.ts.reindex(nonContigIndex) + nonContigIndex = datetime_series.index[::2] + subNonContig = datetime_series.reindex(nonContigIndex) for idx, val in subNonContig.items(): - assert val == test_data.ts[idx] + assert val == datetime_series[idx] # return a copy the same index here - result = test_data.ts.reindex() - assert not (result is test_data.ts) + result = datetime_series.reindex() + assert not (result is datetime_series) def test_reindex_nan(): @@ -229,25 +229,26 @@ def test_reindex_with_datetimes(): tm.assert_series_equal(result, expected) -def test_reindex_corner(test_data): +def test_reindex_corner(datetime_series): # (don't forget to fix this) I think it's fixed - test_data.empty.reindex(test_data.ts.index, method="pad") # it works + empty = Series() + empty.reindex(datetime_series.index, method="pad") # it works # corner case: pad empty series - reindexed = test_data.empty.reindex(test_data.ts.index, method="pad") + reindexed = empty.reindex(datetime_series.index, method="pad") # pass non-Index - reindexed = test_data.ts.reindex(list(test_data.ts.index)) - assert_series_equal(test_data.ts, reindexed) + reindexed = datetime_series.reindex(list(datetime_series.index)) + assert_series_equal(datetime_series, reindexed) # bad fill method - ts = test_data.ts[::2] + ts = datetime_series[::2] msg = ( r"Invalid fill method\. Expecting pad \(ffill\), backfill" r" \(bfill\) or nearest\. Got foo" ) with pytest.raises(ValueError, match=msg): - ts.reindex(test_data.ts.index, method="foo") + ts.reindex(datetime_series.index, method="foo") def test_reindex_pad(): @@ -319,12 +320,12 @@ def test_reindex_backfill(): pass -def test_reindex_int(test_data): - ts = test_data.ts[::2] +def test_reindex_int(datetime_series): + ts = datetime_series[::2] int_ts = Series(np.zeros(len(ts), dtype=int), index=ts.index) # this should work fine - reindexed_int = int_ts.reindex(test_data.ts.index) + reindexed_int = int_ts.reindex(datetime_series.index) # if NaNs introduced assert reindexed_int.dtype == np.float_ @@ -334,13 +335,13 @@ def test_reindex_int(test_data): assert reindexed_int.dtype == np.int_ -def test_reindex_bool(test_data): +def test_reindex_bool(datetime_series): # A series other than float, int, string, or object - ts = test_data.ts[::2] + ts = datetime_series[::2] bool_ts = Series(np.zeros(len(ts), dtype=bool), index=ts.index) # this should work fine - reindexed_bool = bool_ts.reindex(test_data.ts.index) + reindexed_bool = bool_ts.reindex(datetime_series.index) # if NaNs introduced assert reindexed_bool.dtype == np.object_ @@ -350,11 +351,11 @@ def test_reindex_bool(test_data): assert reindexed_bool.dtype == np.bool_ -def test_reindex_bool_pad(test_data): +def test_reindex_bool_pad(datetime_series): # fail - ts = test_data.ts[5:] + ts = datetime_series[5:] bool_ts = Series(np.zeros(len(ts), dtype=bool), index=ts.index) - filled_bool = bool_ts.reindex(test_data.ts.index, method="pad") + filled_bool = bool_ts.reindex(datetime_series.index, method="pad") assert isna(filled_bool[:5]).all() @@ -382,10 +383,10 @@ def test_reindex_categorical(): tm.assert_series_equal(result, expected) -def test_reindex_like(test_data): - other = test_data.ts[::2] +def test_reindex_like(datetime_series): + other = datetime_series[::2] assert_series_equal( - test_data.ts.reindex(other.index), test_data.ts.reindex_like(other) + datetime_series.reindex(other.index), datetime_series.reindex_like(other) ) # GH 7179 diff --git a/pandas/tests/series/indexing/test_boolean.py b/pandas/tests/series/indexing/test_boolean.py index 01b4a3c84a565..bcbb162697e50 100644 --- a/pandas/tests/series/indexing/test_boolean.py +++ b/pandas/tests/series/indexing/test_boolean.py @@ -12,8 +12,8 @@ from pandas.tseries.offsets import BDay -def test_getitem_boolean(test_data): - s = test_data.series +def test_getitem_boolean(string_series): + s = string_series mask = s > s.median() # passing list is OK @@ -55,10 +55,10 @@ def test_getitem_boolean_empty(): s[Series([True], dtype=bool)] -def test_getitem_boolean_object(test_data): +def test_getitem_boolean_object(string_series): # using column from DataFrame - s = test_data.series + s = string_series mask = s > s.median() omask = mask.astype(object) @@ -83,8 +83,8 @@ def test_getitem_boolean_object(test_data): s[omask] = 5 -def test_getitem_setitem_boolean_corner(test_data): - ts = test_data.ts +def test_getitem_setitem_boolean_corner(datetime_series): + ts = datetime_series mask_shifted = ts.shift(1, freq=BDay()) > ts.median() # these used to raise...?? @@ -104,38 +104,38 @@ def test_getitem_setitem_boolean_corner(test_data): ts.loc[mask_shifted] = 1 -def test_setitem_boolean(test_data): - mask = test_data.series > test_data.series.median() +def test_setitem_boolean(string_series): + mask = string_series > string_series.median() # similar indexed series - result = test_data.series.copy() - result[mask] = test_data.series * 2 - expected = test_data.series * 2 + result = string_series.copy() + result[mask] = string_series * 2 + expected = string_series * 2 assert_series_equal(result[mask], expected[mask]) # needs alignment - result = test_data.series.copy() - result[mask] = (test_data.series * 2)[0:5] - expected = (test_data.series * 2)[0:5].reindex_like(test_data.series) - expected[-mask] = test_data.series[mask] + result = string_series.copy() + result[mask] = (string_series * 2)[0:5] + expected = (string_series * 2)[0:5].reindex_like(string_series) + expected[-mask] = string_series[mask] assert_series_equal(result[mask], expected[mask]) -def test_get_set_boolean_different_order(test_data): - ordered = test_data.series.sort_values() +def test_get_set_boolean_different_order(string_series): + ordered = string_series.sort_values() # setting - copy = test_data.series.copy() + copy = string_series.copy() copy[ordered > 0] = 0 - expected = test_data.series.copy() + expected = string_series.copy() expected[expected > 0] = 0 assert_series_equal(copy, expected) # getting - sel = test_data.series[ordered > 0] - exp = test_data.series[test_data.series > 0] + sel = string_series[ordered > 0] + exp = string_series[string_series > 0] assert_series_equal(sel, exp) diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index 19d3e76f52adf..d1c93990e9167 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -38,15 +38,15 @@ def test_basic_indexing(): s[5] = 0 -def test_basic_getitem_with_labels(test_data): - indices = test_data.ts.index[[5, 10, 15]] +def test_basic_getitem_with_labels(datetime_series): + indices = datetime_series.index[[5, 10, 15]] - result = test_data.ts[indices] - expected = test_data.ts.reindex(indices) + result = datetime_series[indices] + expected = datetime_series.reindex(indices) assert_series_equal(result, expected) - result = test_data.ts[indices[0] : indices[2]] - expected = test_data.ts.loc[indices[0] : indices[2]] + result = datetime_series[indices[0] : indices[2]] + expected = datetime_series.loc[indices[0] : indices[2]] assert_series_equal(result, expected) # integer indexes, be careful @@ -89,27 +89,24 @@ def test_getitem_setitem_ellipsis(): assert (result == 5).all() -def test_getitem_get(test_data): - test_series = test_data.series - test_obj_series = test_data.objSeries +def test_getitem_get(datetime_series, string_series, object_series): + idx1 = string_series.index[5] + idx2 = object_series.index[5] - idx1 = test_series.index[5] - idx2 = test_obj_series.index[5] + assert string_series[idx1] == string_series.get(idx1) + assert object_series[idx2] == object_series.get(idx2) - assert test_series[idx1] == test_series.get(idx1) - assert test_obj_series[idx2] == test_obj_series.get(idx2) + assert string_series[idx1] == string_series[5] + assert object_series[idx2] == object_series[5] - assert test_series[idx1] == test_series[5] - assert test_obj_series[idx2] == test_obj_series[5] - - assert test_series.get(-1) == test_series.get(test_series.index[-1]) - assert test_series[5] == test_series.get(test_series.index[5]) + assert string_series.get(-1) == string_series.get(string_series.index[-1]) + assert string_series[5] == string_series.get(string_series.index[5]) # missing - d = test_data.ts.index[0] - BDay() + d = datetime_series.index[0] - BDay() msg = r"Timestamp\('1999-12-31 00:00:00', freq='B'\)" with pytest.raises(KeyError, match=msg): - test_data.ts[d] + datetime_series[d] # None # GH 5652 @@ -118,20 +115,20 @@ def test_getitem_get(test_data): assert result is None -def test_getitem_fancy(test_data): - slice1 = test_data.series[[1, 2, 3]] - slice2 = test_data.objSeries[[1, 2, 3]] - assert test_data.series.index[2] == slice1.index[1] - assert test_data.objSeries.index[2] == slice2.index[1] - assert test_data.series[2] == slice1[1] - assert test_data.objSeries[2] == slice2[1] +def test_getitem_fancy(string_series, object_series): + slice1 = string_series[[1, 2, 3]] + slice2 = object_series[[1, 2, 3]] + assert string_series.index[2] == slice1.index[1] + assert object_series.index[2] == slice2.index[1] + assert string_series[2] == slice1[1] + assert object_series[2] == slice2[1] -def test_getitem_generator(test_data): - gen = (x > 0 for x in test_data.series) - result = test_data.series[gen] - result2 = test_data.series[iter(test_data.series > 0)] - expected = test_data.series[test_data.series > 0] +def test_getitem_generator(string_series): + gen = (x > 0 for x in string_series) + result = string_series[gen] + result2 = string_series[iter(string_series > 0)] + expected = string_series[string_series > 0] assert_series_equal(result, expected) assert_series_equal(result2, expected) @@ -169,11 +166,11 @@ def test_getitem_with_duplicates_indices(result_1, duplicate_item, expected_1): assert result[2] == result_1[2] -def test_getitem_out_of_bounds(test_data): +def test_getitem_out_of_bounds(datetime_series): # don't segfault, GH #495 msg = "index out of bounds" with pytest.raises(IndexError, match=msg): - test_data.ts[len(test_data.ts)] + datetime_series[len(datetime_series)] # GH #917 s = Series([]) @@ -190,8 +187,8 @@ def test_getitem_setitem_integers(): tm.assert_almost_equal(s["a"], 5) -def test_getitem_box_float64(test_data): - value = test_data.ts[5] +def test_getitem_box_float64(datetime_series): + value = datetime_series[5] assert isinstance(value, np.float64) @@ -308,14 +305,14 @@ def test_getitem_dataframe(): s[df > 5] -def test_setitem(test_data): - test_data.ts[test_data.ts.index[5]] = np.NaN - test_data.ts[[1, 2, 17]] = np.NaN - test_data.ts[6] = np.NaN - assert np.isnan(test_data.ts[6]) - assert np.isnan(test_data.ts[2]) - test_data.ts[np.isnan(test_data.ts)] = 5 - assert not np.isnan(test_data.ts[2]) +def test_setitem(datetime_series, string_series): + datetime_series[datetime_series.index[5]] = np.NaN + datetime_series[[1, 2, 17]] = np.NaN + datetime_series[6] = np.NaN + assert np.isnan(datetime_series[6]) + assert np.isnan(datetime_series[2]) + datetime_series[np.isnan(datetime_series)] = 5 + assert not np.isnan(datetime_series[2]) # caught this bug when writing tests series = Series(tm.makeIntIndex(20).astype(float), index=tm.makeIntIndex(20)) @@ -324,11 +321,11 @@ def test_setitem(test_data): assert (series[::2] == 0).all() # set item that's not contained - s = test_data.series.copy() + s = string_series.copy() s["foobar"] = 1 app = Series([1], index=["foobar"], name="series") - expected = test_data.series.append(app) + expected = string_series.append(app) assert_series_equal(s, expected) # Test for issue #10193 @@ -370,52 +367,52 @@ def test_setitem_dtypes(): assert_series_equal(s, Series([np.nan, 1.0])) -def test_set_value(test_data): - idx = test_data.ts.index[10] - res = test_data.ts._set_value(idx, 0) - assert res is test_data.ts - assert test_data.ts[idx] == 0 +def test_set_value(datetime_series, string_series): + idx = datetime_series.index[10] + res = datetime_series._set_value(idx, 0) + assert res is datetime_series + assert datetime_series[idx] == 0 # equiv - s = test_data.series.copy() + s = string_series.copy() res = s._set_value("foobar", 0) assert res is s assert res.index[-1] == "foobar" assert res["foobar"] == 0 - s = test_data.series.copy() + s = string_series.copy() s.loc["foobar"] = 0 assert s.index[-1] == "foobar" assert s["foobar"] == 0 -def test_setslice(test_data): - sl = test_data.ts[5:20] +def test_setslice(datetime_series): + sl = datetime_series[5:20] assert len(sl) == len(sl.index) assert sl.index.is_unique is True # FutureWarning from NumPy about [slice(None, 5). @pytest.mark.filterwarnings("ignore:Using a non-tuple:FutureWarning") -def test_basic_getitem_setitem_corner(test_data): +def test_basic_getitem_setitem_corner(datetime_series): # invalid tuples, e.g. td.ts[:, None] vs. td.ts[:, 2] msg = "Can only tuple-index with a MultiIndex" with pytest.raises(ValueError, match=msg): - test_data.ts[:, 2] + datetime_series[:, 2] with pytest.raises(ValueError, match=msg): - test_data.ts[:, 2] = 2 + datetime_series[:, 2] = 2 # weird lists. [slice(0, 5)] will work but not two slices - result = test_data.ts[[slice(None, 5)]] - expected = test_data.ts[:5] + result = datetime_series[[slice(None, 5)]] + expected = datetime_series[:5] assert_series_equal(result, expected) # OK msg = r"unhashable type(: 'slice')?" with pytest.raises(TypeError, match=msg): - test_data.ts[[5, slice(None, None)]] + datetime_series[[5, slice(None, None)]] with pytest.raises(TypeError, match=msg): - test_data.ts[[5, slice(None, None)]] = 2 + datetime_series[[5, slice(None, None)]] = 2 @pytest.mark.parametrize("tz", ["US/Eastern", "UTC", "Asia/Tokyo"]) @@ -556,25 +553,25 @@ def test_categorical_assigning_ops(): tm.assert_series_equal(s, exp) -def test_slice(test_data): - numSlice = test_data.series[10:20] - numSliceEnd = test_data.series[-10:] - objSlice = test_data.objSeries[10:20] +def test_slice(string_series, object_series): + numSlice = string_series[10:20] + numSliceEnd = string_series[-10:] + objSlice = object_series[10:20] - assert test_data.series.index[9] not in numSlice.index - assert test_data.objSeries.index[9] not in objSlice.index + assert string_series.index[9] not in numSlice.index + assert object_series.index[9] not in objSlice.index assert len(numSlice) == len(numSlice.index) - assert test_data.series[numSlice.index[0]] == numSlice[numSlice.index[0]] + assert string_series[numSlice.index[0]] == numSlice[numSlice.index[0]] - assert numSlice.index[1] == test_data.series.index[11] - assert tm.equalContents(numSliceEnd, np.array(test_data.series)[-10:]) + assert numSlice.index[1] == string_series.index[11] + assert tm.equalContents(numSliceEnd, np.array(string_series)[-10:]) # Test return view. - sl = test_data.series[10:20] + sl = string_series[10:20] sl[:] = 0 - assert (test_data.series[10:20] == 0).all() + assert (string_series[10:20] == 0).all() def test_slice_can_reorder_not_uniquely_indexed(): @@ -582,13 +579,13 @@ def test_slice_can_reorder_not_uniquely_indexed(): s[::-1] # it works! -def test_loc_setitem(test_data): - inds = test_data.series.index[[3, 4, 7]] +def test_loc_setitem(string_series): + inds = string_series.index[[3, 4, 7]] - result = test_data.series.copy() + result = string_series.copy() result.loc[inds] = 5 - expected = test_data.series.copy() + expected = string_series.copy() expected[[3, 4, 7]] = 5 assert_series_equal(result, expected) @@ -597,16 +594,16 @@ def test_loc_setitem(test_data): assert_series_equal(result, expected) # set slice with indices - d1, d2 = test_data.series.index[[5, 15]] + d1, d2 = string_series.index[[5, 15]] result.loc[d1:d2] = 6 expected[5:16] = 6 # because it's inclusive assert_series_equal(result, expected) # set index value - test_data.series.loc[d1] = 4 - test_data.series.loc[d2] = 6 - assert test_data.series[d1] == 4 - assert test_data.series[d2] == 6 + string_series.loc[d1] = 4 + string_series.loc[d2] = 6 + assert string_series[d1] == 4 + assert string_series[d2] == 6 def test_setitem_na(): @@ -776,10 +773,10 @@ def test_underlying_data_conversion(): tm.assert_frame_equal(df, expected) -def test_preserve_refs(test_data): - seq = test_data.ts[[5, 10, 15]] +def test_preserve_refs(datetime_series): + seq = datetime_series[[5, 10, 15]] seq[1] = np.NaN - assert not np.isnan(test_data.ts[10]) + assert not np.isnan(datetime_series[10]) def test_cast_on_putmask(): @@ -902,11 +899,11 @@ def test_take_categorical(): assert_series_equal(result, expected) -def test_head_tail(test_data): - assert_series_equal(test_data.series.head(), test_data.series[:5]) - assert_series_equal(test_data.series.head(0), test_data.series[0:0]) - assert_series_equal(test_data.series.tail(), test_data.series[-5:]) - assert_series_equal(test_data.series.tail(0), test_data.series[0:0]) +def test_head_tail(string_series): + assert_series_equal(string_series.head(), string_series[:5]) + assert_series_equal(string_series.head(0), string_series[0:0]) + assert_series_equal(string_series.tail(), string_series[-5:]) + assert_series_equal(string_series.tail(0), string_series[0:0]) def test_uint_drop(any_int_dtype): diff --git a/pandas/tests/series/indexing/test_loc.py b/pandas/tests/series/indexing/test_loc.py index 2f7807526a29d..d3402725130fb 100644 --- a/pandas/tests/series/indexing/test_loc.py +++ b/pandas/tests/series/indexing/test_loc.py @@ -13,30 +13,30 @@ def test_loc_uint64(val, expected): assert s.loc[val] == expected -def test_loc_getitem(test_data): - inds = test_data.series.index[[3, 4, 7]] - assert_series_equal(test_data.series.loc[inds], test_data.series.reindex(inds)) - assert_series_equal(test_data.series.iloc[5::2], test_data.series[5::2]) +def test_loc_getitem(string_series, datetime_series): + inds = string_series.index[[3, 4, 7]] + assert_series_equal(string_series.loc[inds], string_series.reindex(inds)) + assert_series_equal(string_series.iloc[5::2], string_series[5::2]) # slice with indices - d1, d2 = test_data.ts.index[[5, 15]] - result = test_data.ts.loc[d1:d2] - expected = test_data.ts.truncate(d1, d2) + d1, d2 = datetime_series.index[[5, 15]] + result = datetime_series.loc[d1:d2] + expected = datetime_series.truncate(d1, d2) assert_series_equal(result, expected) # boolean - mask = test_data.series > test_data.series.median() - assert_series_equal(test_data.series.loc[mask], test_data.series[mask]) + mask = string_series > string_series.median() + assert_series_equal(string_series.loc[mask], string_series[mask]) # ask for index value - assert test_data.ts.loc[d1] == test_data.ts[d1] - assert test_data.ts.loc[d2] == test_data.ts[d2] + assert datetime_series.loc[d1] == datetime_series[d1] + assert datetime_series.loc[d2] == datetime_series[d2] -def test_loc_getitem_not_monotonic(test_data): - d1, d2 = test_data.ts.index[[5, 15]] +def test_loc_getitem_not_monotonic(datetime_series): + d1, d2 = datetime_series.index[[5, 15]] - ts2 = test_data.ts[::2][[1, 2, 0]] + ts2 = datetime_series[::2][[1, 2, 0]] msg = r"Timestamp\('2000-01-10 00:00:00'\)" with pytest.raises(KeyError, match=msg): @@ -73,41 +73,41 @@ def test_loc_getitem_setitem_integer_slice_keyerrors(): s2.loc[3:11] = 0 -def test_loc_getitem_iterator(test_data): - idx = iter(test_data.series.index[:10]) - result = test_data.series.loc[idx] - assert_series_equal(result, test_data.series[:10]) +def test_loc_getitem_iterator(string_series): + idx = iter(string_series.index[:10]) + result = string_series.loc[idx] + assert_series_equal(result, string_series[:10]) -def test_loc_setitem_boolean(test_data): - mask = test_data.series > test_data.series.median() +def test_loc_setitem_boolean(string_series): + mask = string_series > string_series.median() - result = test_data.series.copy() + result = string_series.copy() result.loc[mask] = 0 - expected = test_data.series + expected = string_series expected[mask] = 0 assert_series_equal(result, expected) -def test_loc_setitem_corner(test_data): - inds = list(test_data.series.index[[5, 8, 12]]) - test_data.series.loc[inds] = 5 +def test_loc_setitem_corner(string_series): + inds = list(string_series.index[[5, 8, 12]]) + string_series.loc[inds] = 5 msg = r"\['foo'\] not in index" with pytest.raises(KeyError, match=msg): - test_data.series.loc[inds + ["foo"]] = 5 + string_series.loc[inds + ["foo"]] = 5 -def test_basic_setitem_with_labels(test_data): - indices = test_data.ts.index[[5, 10, 15]] +def test_basic_setitem_with_labels(datetime_series): + indices = datetime_series.index[[5, 10, 15]] - cp = test_data.ts.copy() - exp = test_data.ts.copy() + cp = datetime_series.copy() + exp = datetime_series.copy() cp[indices] = 0 exp.loc[indices] = 0 assert_series_equal(cp, exp) - cp = test_data.ts.copy() - exp = test_data.ts.copy() + cp = datetime_series.copy() + exp = datetime_series.copy() cp[indices[0] : indices[2]] = 0 exp.loc[indices[0] : indices[2]] = 0 assert_series_equal(cp, exp) diff --git a/pandas/tests/series/indexing/test_numeric.py b/pandas/tests/series/indexing/test_numeric.py index b4996575b0a05..083324101f437 100644 --- a/pandas/tests/series/indexing/test_numeric.py +++ b/pandas/tests/series/indexing/test_numeric.py @@ -262,22 +262,22 @@ def test_setitem_float_labels(): assert_series_equal(s, tmp) -def test_slice_float_get_set(test_data): +def test_slice_float_get_set(datetime_series): msg = ( r"cannot do slice indexing on with these indexers \[{key}\]" r" of " ) with pytest.raises(TypeError, match=msg.format(key=r"4\.0")): - test_data.ts[4.0:10.0] + datetime_series[4.0:10.0] with pytest.raises(TypeError, match=msg.format(key=r"4\.0")): - test_data.ts[4.0:10.0] = 0 + datetime_series[4.0:10.0] = 0 with pytest.raises(TypeError, match=msg.format(key=r"4\.5")): - test_data.ts[4.5:10.0] + datetime_series[4.5:10.0] with pytest.raises(TypeError, match=msg.format(key=r"4\.5")): - test_data.ts[4.5:10.0] = 0 + datetime_series[4.5:10.0] = 0 def test_slice_floats2(): @@ -312,6 +312,6 @@ def test_int_indexing(): s["c"] -def test_getitem_int64(test_data): +def test_getitem_int64(datetime_series): idx = np.int64(5) - assert test_data.ts[idx] == test_data.ts[5] + assert datetime_series[idx] == datetime_series[5]