From d2d9fe708dd46c02b57ad4e270d72154528045ea Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 28 Feb 2021 19:33:32 -0800 Subject: [PATCH 1/3] TST: collect tests by method --- asv_bench/benchmarks/timeseries.py | 2 +- pandas/core/frame.py | 2 + pandas/tests/indexes/datetimes/test_misc.py | 17 ++++++++ pandas/tests/indexes/period/test_period.py | 15 ------- .../tests/indexes/period/test_period_range.py | 8 ++++ pandas/tests/indexes/test_index_new.py | 7 ++++ .../tests/indexes/timedeltas/test_indexing.py | 28 +++++++------ .../timedeltas/test_partial_slicing.py | 42 ------------------- pandas/tests/series/indexing/test_getitem.py | 32 ++++++++++++++ 9 files changed, 82 insertions(+), 71 deletions(-) delete mode 100644 pandas/tests/indexes/timedeltas/test_partial_slicing.py diff --git a/asv_bench/benchmarks/timeseries.py b/asv_bench/benchmarks/timeseries.py index 94498e54f0f06..7b52b3606a267 100644 --- a/asv_bench/benchmarks/timeseries.py +++ b/asv_bench/benchmarks/timeseries.py @@ -97,7 +97,7 @@ def setup(self, tz): idx = date_range(start="1/1/2000", periods=1000, freq="H", tz=tz) self.df = DataFrame(np.random.randn(1000, 2), index=idx) - def time_reest_datetimeindex(self, tz): + def time_reset_datetimeindex(self, tz): self.df.reset_index() diff --git a/pandas/core/frame.py b/pandas/core/frame.py index e8793c364586b..bc2051a130079 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -589,12 +589,14 @@ def __init__( elif isinstance(data, (np.ndarray, Series, Index)): if data.dtype.names: + # i.e. numpy structured array data_columns = list(data.dtype.names) data = {k: data[k] for k in data_columns} if columns is None: columns = data_columns mgr = dict_to_mgr(data, index, columns, dtype=dtype) elif getattr(data, "name", None) is not None: + # i.e. Series/Index with non-None name mgr = dict_to_mgr({data.name: data}, index, columns, dtype=dtype) else: mgr = ndarray_to_mgr(data, index, columns, dtype=dtype, copy=copy) diff --git a/pandas/tests/indexes/datetimes/test_misc.py b/pandas/tests/indexes/datetimes/test_misc.py index d230aa43e43d1..eff87a2b3f275 100644 --- a/pandas/tests/indexes/datetimes/test_misc.py +++ b/pandas/tests/indexes/datetimes/test_misc.py @@ -37,6 +37,8 @@ def test_range_edges(self): ) tm.assert_index_equal(idx, exp) + def test_range_edges2(self): + idx = date_range( start=Timestamp("1970-01-01 00:00:00.000000004"), end=Timestamp("1970-01-01 00:00:00.000000001"), @@ -45,6 +47,8 @@ def test_range_edges(self): exp = DatetimeIndex([], freq="N") tm.assert_index_equal(idx, exp) + def test_range_edges3(self): + idx = date_range( start=Timestamp("1970-01-01 00:00:00.000000001"), end=Timestamp("1970-01-01 00:00:00.000000001"), @@ -53,6 +57,8 @@ def test_range_edges(self): exp = DatetimeIndex(["1970-01-01 00:00:00.000000001"], freq="N") tm.assert_index_equal(idx, exp) + def test_range_edges4(self): + idx = date_range( start=Timestamp("1970-01-01 00:00:00.000001"), end=Timestamp("1970-01-01 00:00:00.000004"), @@ -69,6 +75,8 @@ def test_range_edges(self): ) tm.assert_index_equal(idx, exp) + def test_range_edges5(self): + idx = date_range( start=Timestamp("1970-01-01 00:00:00.001"), end=Timestamp("1970-01-01 00:00:00.004"), @@ -85,6 +93,7 @@ def test_range_edges(self): ) tm.assert_index_equal(idx, exp) + def test_range_edges6(self): idx = date_range( start=Timestamp("1970-01-01 00:00:01"), end=Timestamp("1970-01-01 00:00:04"), @@ -101,6 +110,7 @@ def test_range_edges(self): ) tm.assert_index_equal(idx, exp) + def test_range_edges7(self): idx = date_range( start=Timestamp("1970-01-01 00:01"), end=Timestamp("1970-01-01 00:04"), @@ -117,6 +127,7 @@ def test_range_edges(self): ) tm.assert_index_equal(idx, exp) + def test_range_edges8(self): idx = date_range( start=Timestamp("1970-01-01 01:00"), end=Timestamp("1970-01-01 04:00"), @@ -133,6 +144,7 @@ def test_range_edges(self): ) tm.assert_index_equal(idx, exp) + def test_range_edges9(self): idx = date_range( start=Timestamp("1970-01-01"), end=Timestamp("1970-01-04"), freq="D" ) @@ -234,6 +246,7 @@ def test_datetimeindex_accessors(self): exp = DatetimeIndex([], freq="D", tz=dti.tz, name="name") tm.assert_index_equal(res, exp) + def test_datetimeindex_accessors2(self): dti = date_range(freq="BQ-FEB", start=datetime(1998, 1, 1), periods=4) assert sum(dti.is_quarter_start) == 0 @@ -241,6 +254,7 @@ def test_datetimeindex_accessors(self): assert sum(dti.is_year_start) == 0 assert sum(dti.is_year_end) == 1 + def test_datetimeindex_accessors3(self): # Ensure is_start/end accessors throw ValueError for CustomBusinessDay, bday_egypt = offsets.CustomBusinessDay(weekmask="Sun Mon Tue Wed Thu") dti = date_range(datetime(2013, 4, 30), periods=5, freq=bday_egypt) @@ -248,10 +262,12 @@ def test_datetimeindex_accessors(self): with pytest.raises(ValueError, match=msg): dti.is_month_start + def test_datetimeindex_accessors4(self): dti = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-03"]) assert dti.is_month_start[0] == 1 + def test_datetimeindex_accessors5(self): tests = [ (Timestamp("2013-06-01", freq="M").is_month_start, 1), (Timestamp("2013-06-01", freq="BM").is_month_start, 0), @@ -290,6 +306,7 @@ def test_datetimeindex_accessors(self): for ts, value in tests: assert ts == value + def test_datetimeindex_accessors6(self): # GH 6538: Check that DatetimeIndex and its TimeStamp elements # return the same weekofyear accessor close to new year w/ tz dates = ["2013/12/29", "2013/12/30", "2013/12/31"] diff --git a/pandas/tests/indexes/period/test_period.py b/pandas/tests/indexes/period/test_period.py index 03e78af0b2bdd..032b376f6d6a9 100644 --- a/pandas/tests/indexes/period/test_period.py +++ b/pandas/tests/indexes/period/test_period.py @@ -253,14 +253,6 @@ def test_is_(self): assert not index.is_(index - 2) assert not index.is_(index - 0) - def test_periods_number_check(self): - msg = ( - "Of the three parameters: start, end, and periods, exactly two " - "must be specified" - ) - with pytest.raises(ValueError, match=msg): - period_range("2011-1-1", "2012-1-1", "B") - def test_index_duplicate_periods(self): # monotonic idx = PeriodIndex([2000, 2007, 2007, 2009, 2009], freq="A-JUN") @@ -348,13 +340,6 @@ def test_with_multi_index(self): assert isinstance(s.index.values[0][0], Period) - def test_convert_array_of_periods(self): - rng = period_range("1/1/2000", periods=20, freq="D") - periods = list(rng) - - result = Index(periods) - assert isinstance(result, PeriodIndex) - def test_pickle_freq(self): # GH2891 prng = period_range("1/1/2011", "1/1/2012", freq="M") diff --git a/pandas/tests/indexes/period/test_period_range.py b/pandas/tests/indexes/period/test_period_range.py index a5be19731b54a..c94ddf57c0ee1 100644 --- a/pandas/tests/indexes/period/test_period_range.py +++ b/pandas/tests/indexes/period/test_period_range.py @@ -12,6 +12,14 @@ class TestPeriodRange: + def test_required_arguments(self): + msg = ( + "Of the three parameters: start, end, and periods, exactly two " + "must be specified" + ) + with pytest.raises(ValueError, match=msg): + period_range("2011-1-1", "2012-1-1", "B") + @pytest.mark.parametrize("freq", ["D", "W", "M", "Q", "A"]) def test_construction_from_string(self, freq): # non-empty diff --git a/pandas/tests/indexes/test_index_new.py b/pandas/tests/indexes/test_index_new.py index 4fba4b13835b3..5937f43102190 100644 --- a/pandas/tests/indexes/test_index_new.py +++ b/pandas/tests/indexes/test_index_new.py @@ -80,6 +80,13 @@ def test_constructor_infer_periodindex(self): tm.assert_index_equal(rs, xp) assert isinstance(rs, PeriodIndex) + def test_from_list_of_periods(self): + rng = period_range("1/1/2000", periods=20, freq="D") + periods = list(rng) + + result = Index(periods) + assert isinstance(result, PeriodIndex) + @pytest.mark.parametrize("pos", [0, 1]) @pytest.mark.parametrize( "klass,dtype,ctor", diff --git a/pandas/tests/indexes/timedeltas/test_indexing.py b/pandas/tests/indexes/timedeltas/test_indexing.py index 7acfb50fe944b..5f0101eb4478c 100644 --- a/pandas/tests/indexes/timedeltas/test_indexing.py +++ b/pandas/tests/indexes/timedeltas/test_indexing.py @@ -7,13 +7,15 @@ import numpy as np import pytest -import pandas as pd from pandas import ( Index, + NaT, Timedelta, TimedeltaIndex, + Timestamp, notna, timedelta_range, + to_timedelta, ) import pandas._testing as tm @@ -64,10 +66,10 @@ def test_getitem(self): @pytest.mark.parametrize( "key", [ - pd.Timestamp("1970-01-01"), - pd.Timestamp("1970-01-02"), + Timestamp("1970-01-01"), + Timestamp("1970-01-02"), datetime(1970, 1, 1), - pd.Timestamp("1970-01-03").to_datetime64(), + Timestamp("1970-01-03").to_datetime64(), # non-matching NA values np.datetime64("NaT"), ], @@ -81,7 +83,7 @@ def test_timestamp_invalid_key(self, key): class TestGetLoc: def test_get_loc(self): - idx = pd.to_timedelta(["0 days", "1 days", "2 days"]) + idx = to_timedelta(["0 days", "1 days", "2 days"]) for method in [None, "pad", "backfill", "nearest"]: assert idx.get_loc(idx[1], method) == 1 @@ -117,7 +119,7 @@ def test_get_loc(self): def test_get_loc_nat(self): tidx = TimedeltaIndex(["1 days 01:00:00", "NaT", "2 days 01:00:00"]) - assert tidx.get_loc(pd.NaT) == 1 + assert tidx.get_loc(NaT) == 1 assert tidx.get_loc(None) == 1 assert tidx.get_loc(float("nan")) == 1 assert tidx.get_loc(np.nan) == 1 @@ -125,12 +127,12 @@ def test_get_loc_nat(self): class TestGetIndexer: def test_get_indexer(self): - idx = pd.to_timedelta(["0 days", "1 days", "2 days"]) + idx = to_timedelta(["0 days", "1 days", "2 days"]) tm.assert_numpy_array_equal( idx.get_indexer(idx), np.array([0, 1, 2], dtype=np.intp) ) - target = pd.to_timedelta(["-1 hour", "12 hours", "1 day 1 hour"]) + target = to_timedelta(["-1 hour", "12 hours", "1 day 1 hour"]) tm.assert_numpy_array_equal( idx.get_indexer(target, "pad"), np.array([-1, 0, 1], dtype=np.intp) ) @@ -158,25 +160,25 @@ def test_where_invalid_dtypes(self): tdi = timedelta_range("1 day", periods=3, freq="D", name="idx") tail = tdi[2:].tolist() - i2 = Index([pd.NaT, pd.NaT] + tail) + i2 = Index([NaT, NaT] + tail) mask = notna(i2) - expected = Index([pd.NaT.value, pd.NaT.value] + tail, dtype=object, name="idx") + expected = Index([NaT.value, NaT.value] + tail, dtype=object, name="idx") assert isinstance(expected[0], int) result = tdi.where(mask, i2.asi8) tm.assert_index_equal(result, expected) - ts = i2 + pd.Timestamp.now() + ts = i2 + Timestamp.now() expected = Index([ts[0], ts[1]] + tail, dtype=object, name="idx") result = tdi.where(mask, ts) tm.assert_index_equal(result, expected) - per = (i2 + pd.Timestamp.now()).to_period("D") + per = (i2 + Timestamp.now()).to_period("D") expected = Index([per[0], per[1]] + tail, dtype=object, name="idx") result = tdi.where(mask, per) tm.assert_index_equal(result, expected) - ts = pd.Timestamp.now() + ts = Timestamp.now() expected = Index([ts, ts] + tail, dtype=object, name="idx") result = tdi.where(mask, ts) tm.assert_index_equal(result, expected) diff --git a/pandas/tests/indexes/timedeltas/test_partial_slicing.py b/pandas/tests/indexes/timedeltas/test_partial_slicing.py deleted file mode 100644 index cca211c1eb155..0000000000000 --- a/pandas/tests/indexes/timedeltas/test_partial_slicing.py +++ /dev/null @@ -1,42 +0,0 @@ -import numpy as np - -from pandas import ( - Series, - timedelta_range, -) -import pandas._testing as tm - - -class TestSlicing: - def test_partial_slice(self): - rng = timedelta_range("1 day 10:11:12", freq="h", periods=500) - s = Series(np.arange(len(rng)), index=rng) - - result = s["5 day":"6 day"] - expected = s.iloc[86:134] - tm.assert_series_equal(result, expected) - - result = s["5 day":] - expected = s.iloc[86:] - tm.assert_series_equal(result, expected) - - result = s[:"6 day"] - expected = s.iloc[:134] - tm.assert_series_equal(result, expected) - - def test_partial_slice_high_reso(self): - - # higher reso - rng = timedelta_range("1 day 10:11:12", freq="us", periods=2000) - s = Series(np.arange(len(rng)), index=rng) - - result = s["1 day 10:11:12":] - expected = s.iloc[0:] - tm.assert_series_equal(result, expected) - - result = s["1 day 10:11:12.001":] - expected = s.iloc[1000:] - tm.assert_series_equal(result, expected) - - result = s["1 days, 10:11:12.001001"] - assert result == s.iloc[1001] diff --git a/pandas/tests/series/indexing/test_getitem.py b/pandas/tests/series/indexing/test_getitem.py index e6dfafabbfec2..d57a4c271680b 100644 --- a/pandas/tests/series/indexing/test_getitem.py +++ b/pandas/tests/series/indexing/test_getitem.py @@ -202,6 +202,38 @@ def test_getitem_slice_strings_with_datetimeindex(self): expected = ts[1:4] tm.assert_series_equal(result, expected) + def test_getitem_partial_str_slice_with_timedeltaindex(self): + rng = timedelta_range("1 day 10:11:12", freq="h", periods=500) + ser = Series(np.arange(len(rng)), index=rng) + + result = ser["5 day":"6 day"] + expected = ser.iloc[86:134] + tm.assert_series_equal(result, expected) + + result = ser["5 day":] + expected = ser.iloc[86:] + tm.assert_series_equal(result, expected) + + result = ser[:"6 day"] + expected = ser.iloc[:134] + tm.assert_series_equal(result, expected) + + def test_getitem_partial_str_slice_high_reso_with_timedeltaindex(self): + # higher reso + rng = timedelta_range("1 day 10:11:12", freq="us", periods=2000) + ser = Series(np.arange(len(rng)), index=rng) + + result = ser["1 day 10:11:12":] + expected = ser.iloc[0:] + tm.assert_series_equal(result, expected) + + result = ser["1 day 10:11:12.001":] + expected = ser.iloc[1000:] + tm.assert_series_equal(result, expected) + + result = ser["1 days, 10:11:12.001001"] + assert result == ser.iloc[1001] + def test_getitem_slice_2d(self, datetime_series): # GH#30588 multi-dimensional indexing deprecated From 2e7c7ab123305da339004214d039b862f726a536 Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 28 Feb 2021 19:47:18 -0800 Subject: [PATCH 2/3] TST: collect CategoricalIndex tests by method --- .../tests/indexes/categorical/test_append.py | 62 +++++++++++++++++++ .../indexes/categorical/test_category.py | 53 ---------------- .../tests/indexes/categorical/test_formats.py | 6 ++ 3 files changed, 68 insertions(+), 53 deletions(-) create mode 100644 pandas/tests/indexes/categorical/test_append.py diff --git a/pandas/tests/indexes/categorical/test_append.py b/pandas/tests/indexes/categorical/test_append.py new file mode 100644 index 0000000000000..b48c3219f5111 --- /dev/null +++ b/pandas/tests/indexes/categorical/test_append.py @@ -0,0 +1,62 @@ +import pytest + +from pandas import ( + CategoricalIndex, + Index, +) +import pandas._testing as tm + + +class TestAppend: + @pytest.fixture + def ci(self): + categories = list("cab") + return CategoricalIndex(list("aabbca"), categories=categories, ordered=False) + + def test_append(self, ci): + # append cats with the same categories + result = ci[:3].append(ci[3:]) + tm.assert_index_equal(result, ci, exact=True) + + foos = [ci[:1], ci[1:3], ci[3:]] + result = foos[0].append(foos[1:]) + tm.assert_index_equal(result, ci, exact=True) + + def test_append_empty(self, ci): + # empty + result = ci.append([]) + tm.assert_index_equal(result, ci, exact=True) + + def test_append_mismatched_categories(self, ci): + # appending with different categories or reordered is not ok + msg = "all inputs must be Index" + with pytest.raises(TypeError, match=msg): + ci.append(ci.values.set_categories(list("abcd"))) + with pytest.raises(TypeError, match=msg): + ci.append(ci.values.reorder_categories(list("abc"))) + + def test_append_category_objects(self, ci): + # with objects + result = ci.append(Index(["c", "a"])) + expected = CategoricalIndex(list("aabbcaca"), categories=ci.categories) + tm.assert_index_equal(result, expected, exact=True) + + def test_append_non_categories(self, ci): + # invalid objects -> cast to object via concat_compat + result = ci.append(Index(["a", "d"])) + expected = Index(["a", "a", "b", "b", "c", "a", "a", "d"]) + tm.assert_index_equal(result, expected, exact=True) + + def test_append_object(self, ci): + # GH#14298 - if base object is not categorical -> coerce to object + result = Index(["c", "a"]).append(ci) + expected = Index(list("caaabbca")) + tm.assert_index_equal(result, expected, exact=True) + + def test_append_to_another(self): + # hits Index._concat + fst = Index(["a", "b"]) + snd = CategoricalIndex(["d", "e"]) + result = fst.append(snd) + expected = Index(["a", "b", "d", "e"]) + tm.assert_index_equal(result, expected) diff --git a/pandas/tests/indexes/categorical/test_category.py b/pandas/tests/indexes/categorical/test_category.py index 8c9caf2e59011..222ca692091ab 100644 --- a/pandas/tests/indexes/categorical/test_category.py +++ b/pandas/tests/indexes/categorical/test_category.py @@ -30,53 +30,6 @@ def test_can_hold_identifiers(self): key = idx[0] assert idx._can_hold_identifiers_and_holds_name(key) is True - def test_append(self): - - ci = self.create_index() - categories = ci.categories - - # append cats with the same categories - result = ci[:3].append(ci[3:]) - tm.assert_index_equal(result, ci, exact=True) - - foos = [ci[:1], ci[1:3], ci[3:]] - result = foos[0].append(foos[1:]) - tm.assert_index_equal(result, ci, exact=True) - - # empty - result = ci.append([]) - tm.assert_index_equal(result, ci, exact=True) - - # appending with different categories or reordered is not ok - msg = "all inputs must be Index" - with pytest.raises(TypeError, match=msg): - ci.append(ci.values.set_categories(list("abcd"))) - with pytest.raises(TypeError, match=msg): - ci.append(ci.values.reorder_categories(list("abc"))) - - # with objects - result = ci.append(Index(["c", "a"])) - expected = CategoricalIndex(list("aabbcaca"), categories=categories) - tm.assert_index_equal(result, expected, exact=True) - - # invalid objects -> cast to object via concat_compat - result = ci.append(Index(["a", "d"])) - expected = Index(["a", "a", "b", "b", "c", "a", "a", "d"]) - tm.assert_index_equal(result, expected, exact=True) - - # GH14298 - if base object is not categorical -> coerce to object - result = Index(["c", "a"]).append(ci) - expected = Index(list("caaabbca")) - tm.assert_index_equal(result, expected, exact=True) - - def test_append_to_another(self): - # hits Index._concat - fst = Index(["a", "b"]) - snd = CategoricalIndex(["d", "e"]) - result = fst.append(snd) - expected = Index(["a", "b", "d", "e"]) - tm.assert_index_equal(result, expected) - def test_insert(self): ci = self.create_index() @@ -326,12 +279,6 @@ def test_map_str(self): class TestCategoricalIndex2: # Tests that are not overriding a test in Base - def test_format_different_scalar_lengths(self): - # GH35439 - idx = CategoricalIndex(["aaaaaaaaa", "b"]) - expected = ["aaaaaaaaa", "b"] - assert idx.format() == expected - @pytest.mark.parametrize( "dtype, engine_type", [ diff --git a/pandas/tests/indexes/categorical/test_formats.py b/pandas/tests/indexes/categorical/test_formats.py index 0f1cb55b9811c..2009d78e47c1c 100644 --- a/pandas/tests/indexes/categorical/test_formats.py +++ b/pandas/tests/indexes/categorical/test_formats.py @@ -7,6 +7,12 @@ class TestCategoricalIndexRepr: + def test_format_different_scalar_lengths(self): + # GH#35439 + idx = CategoricalIndex(["aaaaaaaaa", "b"]) + expected = ["aaaaaaaaa", "b"] + assert idx.format() == expected + def test_string_categorical_index_repr(self): # short idx = CategoricalIndex(["a", "bb", "ccc"]) From 922d6ea93252bb2cf02dc8b519a75732aadf6827 Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 28 Feb 2021 20:12:50 -0800 Subject: [PATCH 3/3] TST: split large tests --- pandas/tests/frame/test_constructors.py | 8 ++++++++ pandas/tests/indexes/datetimes/test_indexing.py | 7 +++++++ pandas/tests/indexes/datetimes/test_partial_slicing.py | 6 ------ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 280d1da4070d9..cc3416cafc1cf 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -1721,12 +1721,15 @@ def test_constructor_with_datetimes(self): ) tm.assert_series_equal(result, expected) + def test_constructor_with_datetimes1(self): + # GH 2809 ind = date_range(start="2000-01-01", freq="D", periods=10) datetimes = [ts.to_pydatetime() for ts in ind] datetime_s = Series(datetimes) assert datetime_s.dtype == "M8[ns]" + def test_constructor_with_datetimes2(self): # GH 2810 ind = date_range(start="2000-01-01", freq="D", periods=10) datetimes = [ts.to_pydatetime() for ts in ind] @@ -1740,6 +1743,7 @@ def test_constructor_with_datetimes(self): ) tm.assert_series_equal(result, expected) + def test_constructor_with_datetimes3(self): # GH 7594 # don't coerce tz-aware tz = pytz.timezone("US/Eastern") @@ -1757,6 +1761,7 @@ def test_constructor_with_datetimes(self): df.dtypes, Series({"End Date": "datetime64[ns, US/Eastern]"}) ) + def test_constructor_with_datetimes4(self): # tz-aware (UTC and other tz's) # GH 8411 dr = date_range("20130101", periods=3) @@ -1769,6 +1774,7 @@ def test_constructor_with_datetimes(self): df = DataFrame({"value": dr}) assert str(df.iat[0, 0].tz) == "US/Eastern" + def test_constructor_with_datetimes5(self): # GH 7822 # preserver an index with a tz on dict construction i = date_range("1/1/2011", periods=5, freq="10s", tz="US/Eastern") @@ -1781,7 +1787,9 @@ def test_constructor_with_datetimes(self): df = DataFrame({"a": i}) tm.assert_frame_equal(df, expected) + def test_constructor_with_datetimes6(self): # multiples + i = date_range("1/1/2011", periods=5, freq="10s", tz="US/Eastern") i_no_tz = date_range("1/1/2011", periods=5, freq="10s") df = DataFrame({"a": i, "b": i_no_tz}) expected = DataFrame({"a": i.to_series().reset_index(drop=True), "b": i_no_tz}) diff --git a/pandas/tests/indexes/datetimes/test_indexing.py b/pandas/tests/indexes/datetimes/test_indexing.py index c65d9098a86a4..d29d4647f4753 100644 --- a/pandas/tests/indexes/datetimes/test_indexing.py +++ b/pandas/tests/indexes/datetimes/test_indexing.py @@ -551,6 +551,13 @@ def test_get_loc_reasonable_key_error(self): with pytest.raises(KeyError, match="2000"): index.get_loc("1/1/2000") + def test_get_loc_year_str(self): + rng = date_range("1/1/2000", "1/1/2010") + + result = rng.get_loc("2009") + expected = slice(3288, 3653) + assert result == expected + class TestContains: def test_dti_contains_with_duplicates(self): diff --git a/pandas/tests/indexes/datetimes/test_partial_slicing.py b/pandas/tests/indexes/datetimes/test_partial_slicing.py index 05ee67eee0da5..882515799f943 100644 --- a/pandas/tests/indexes/datetimes/test_partial_slicing.py +++ b/pandas/tests/indexes/datetimes/test_partial_slicing.py @@ -55,12 +55,6 @@ def test_slice_year(self): expected = df[df.index.year == 2005] tm.assert_frame_equal(result, expected) - rng = date_range("1/1/2000", "1/1/2010") - - result = rng.get_loc("2009") - expected = slice(3288, 3653) - assert result == expected - @pytest.mark.parametrize( "partial_dtime", [