Repository: pandas-dev/pandas
File: pandas/tests/indexing/test_loc.py (Python)

Code before:
        res = obj.loc[key]
        if isinstance(exp, (DataFrame, Series)):
            tm.assert_equal(res, exp)
        else:
            assert res == exp

    @pytest.mark.parametrize(
        "data",
        [
            [datetime(2025, 10, 30)],
            [Timestamp(2025, 10, 30)] * 2,
            [Timedelta(1)],
            [Timedelta(1), Timedelta(2)],
        ],
    )
    def test_loc_empty_slice_assignment_with_datetime(self, data):
        # issue #50942
        # empty slice assignment with datetime or timedelta should not raise exceptions
        mask = [False] * len(data)
        try:
            df = DataFrame(data=data, columns=["A"])
            res = df.loc[mask]
            res = df
            tm.assert_frame_equal(res, df)
        except Exception:
            pytest.fail("loc empty slice assignment raised Exception unexpectedly!")


class TestLocBaseIndependent:
    # Tests for loc that do not depend on subclassing Base
    def test_loc_npstr(self):
        # GH#45580
        df = DataFrame(index=date_range("2021", "2022"))
        result = df.loc[np.array(["2021/6/1"])[0] :]
        expected = df.iloc[151:]
        tm.assert_frame_equal(result, expected)

    @pytest.mark.parametrize(
        "msg, key",
        [
            (r"Period\('2019', 'Y-DEC'\), 'foo', 'bar'", (Period(2019), "foo", "bar")),
            (r"Period\('2019', 'Y-DEC'\), 'y1', 'bar'", (Period(2019), "y1", "bar")),
            (r"Period\('2019', 'Y-DEC'\), 'foo', 'z1'", (Period(2019), "foo", "z1")),
            (
                r"Period\('2018', 'Y-DEC'\), Period\('2016', 'Y-DEC'\), 'bar'",
                (Period(2018), Period(2016), "bar"),
            ),
            (r"Period\('2018', 'Y-DEC'\), 'foo', 'y1'", (Period(2018), "foo", "y1")),
            (
                r"Period\('2017', 'Y-DEC'\), 'foo', Period\('2015', 'Y-DEC'\)",
                (Period(2017), "foo", Period(2015)),

Review comment (suggestion):
1. You can remove the `try/except`
2. In the original issue, it's testing a setitem, so this would need to be 
```python
expected = df.copy()
df.loc[mask] = df
tm.assert_frame_equal(df, expected) 
```

Code after:
        res = obj.loc[key]
        if isinstance(exp, (DataFrame, Series)):
            tm.assert_equal(res, exp)
        else:
            assert res == exp

    @pytest.mark.parametrize(
        "data",
        [
            [datetime(2025, 10, 30)],
            [Timestamp(2025, 10, 30)] * 2,
            [Timedelta(1)],
            [Timedelta(1), Timedelta(2)],
        ],
    )
    def test_loc_empty_slice_assignment_with_datetime(self, data):
        # issue #50942
        # empty slice assignment with datetime or timedelta should not raise exceptions
        mask = [False] * len(data)

        df = DataFrame(data=data, columns=["A"])
        expected = df.copy()
        df.loc[mask] = df
        tm.assert_frame_equal(df, expected)


class TestLocBaseIndependent:
    # Tests for loc that do not depend on subclassing Base
    def test_loc_npstr(self):
        # GH#45580
        df = DataFrame(index=date_range("2021", "2022"))
        result = df.loc[np.array(["2021/6/1"])[0] :]
        expected = df.iloc[151:]
        tm.assert_frame_equal(result, expected)

    @pytest.mark.parametrize(
        "msg, key",
        [
            (r"Period\('2019', 'Y-DEC'\), 'foo', 'bar'", (Period(2019), "foo", "bar")),
            (r"Period\('2019', 'Y-DEC'\), 'y1', 'bar'", (Period(2019), "y1", "bar")),
            (r"Period\('2019', 'Y-DEC'\), 'foo', 'z1'", (Period(2019), "foo", "z1")),
            (
                r"Period\('2018', 'Y-DEC'\), Period\('2016', 'Y-DEC'\), 'bar'",
                (Period(2018), Period(2016), "bar"),
            ),
            (r"Period\('2018', 'Y-DEC'\), 'foo', 'y1'", (Period(2018), "foo", "y1")),
            (
                r"Period\('2017', 'Y-DEC'\), 'foo', Period\('2015', 'Y-DEC'\)",
                (Period(2017), "foo", Period(2015)),
            ),
            (r"Period\('2017', 'Y-DEC'\), 'z1', 'bar'", (Period(2017), "z1", "bar")),