Repository: pandas-dev/pandas
File: pandas/tests/base/test_value_counts.py (Python)

Code before:
    res = obj.value_counts(dropna=dropna)
    if dropna is True:
        expected = Series([1], index=Index([True], dtype=obj.dtype), name="count")
    else:
        expected = Series([1, 1, 1], index=[True, pd.NA, np.nan], name="count")
    tm.assert_series_equal(res, expected)


def test_value_counts_object_inference_deprecated():
    # GH#56161
    dti = pd.date_range("2016-01-01", periods=3, tz="UTC")

    idx = dti.astype(object)
    res = idx.value_counts()

    exp = dti.value_counts()
    exp.index = exp.index.astype(object)
    tm.assert_series_equal(res, exp)


def _vc_make_index(kind: str, periods=5, freq="D"):
    if kind == "dt":
        return pd.date_range("2016-01-01", periods=periods, freq=freq)
    if kind == "td":
        return pd.timedelta_range(Timedelta(0), periods=periods, freq=freq)
    raise ValueError("kind must be 'dt' or 'td'")


@pytest.mark.parametrize(
    "kind,freq,normalize",
    [
        ("dt", "D", False),
        ("dt", "D", True),
        ("td", "D", False),
        ("td", "D", True),
        ("td", Timedelta(hours=1), False),
        ("td", Timedelta(hours=1), True),
    ],
)
def test_value_counts_freq_preserved_datetimelike_no_sort(kind, freq, normalize):
    idx = _vc_make_index(kind, periods=5, freq=freq)
    vc = idx.value_counts(sort=False, normalize=normalize)
    assert vc.index.freq == idx.freq
    if normalize:
        assert np.isclose(vc.values, 1 / len(idx)).all()


@pytest.mark.parametrize(
    "kind,freq",
    [
        ("dt", "D"),

Review comment (suggestion):
Can you just write 1 parametrized test (with `DatetimeIndex` and `TimedeltaIndex` as the hardcoded inputs)? You can also use `tm.assert_index_equal`

Code after:
    obj = klass(values)
    res = obj.value_counts(dropna=dropna)
    if dropna is True:
        expected = Series([1], index=Index([True], dtype=obj.dtype), name="count")
    else:
        expected = Series([1, 1, 1], index=[True, pd.NA, np.nan], name="count")
    tm.assert_series_equal(res, expected)


def test_value_counts_object_inference_deprecated():
    # GH#56161
    dti = pd.date_range("2016-01-01", periods=3, tz="UTC")

    idx = dti.astype(object)
    res = idx.value_counts()

    exp = dti.value_counts()
    exp.index = exp.index.astype(object)
    tm.assert_series_equal(res, exp)


@pytest.mark.parametrize(
    ("index", "expected_index"),
    [
        [
            pd.date_range("2016-01-01", periods=5, freq="D"),
            pd.date_range("2016-01-01", periods=5, freq="D"),
        ],
        [
            pd.timedelta_range(Timedelta(0), periods=5, freq="h"),
            pd.timedelta_range(Timedelta(0), periods=5, freq="h"),
        ],
        [
            DatetimeIndex(
                [Timestamp("2016-01-01") + Timedelta(days=i) for i in range(1)]
                + [Timestamp("2016-01-02")]
                + [Timestamp("2016-01-01") + Timedelta(days=i) for i in range(1, 5)]
            ),
            DatetimeIndex(pd.date_range("2016-01-01", periods=5, freq="D")),
        ],
        [
            TimedeltaIndex(
                [Timedelta(hours=i) for i in range(1)]
                + [Timedelta(hours=1)]
                + [Timedelta(hours=i) for i in range(1, 5)],
            ),
            TimedeltaIndex(pd.timedelta_range(Timedelta(0), periods=5, freq="h")),
        ],
        [
            DatetimeIndex(
                [Timestamp("2016-01-01") + Timedelta(days=i) for i in range(2)]