Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
05f3491
API/BUG: freq retention in value_counts
sanjanam1998 Oct 1, 2025
163c0f3
adding whats new
sanjanam1998 Oct 1, 2025
efbb2ce
Merge branch 'main' into main
sanjanam1998 Oct 6, 2025
449313e
Merge branch 'pandas-dev:main' into main
sanjanam1998 Oct 7, 2025
a946317
Merge branch 'pandas-dev:main' into main
sanjanam1998 Oct 15, 2025
74ad212
preserving freq without patching
sanjanam1998 Oct 15, 2025
9b3eeeb
Merge branch 'main' into main
sanjanam1998 Oct 15, 2025
6a1c63d
Merge branch 'main' into main
sanjanam1998 Oct 16, 2025
61a8b67
Merge branch 'pandas-dev:main' into main
sanjanam1998 Oct 17, 2025
b6d277c
Merge branch 'main' into main
sanjanam1998 Oct 17, 2025
c8131c7
Merge branch 'pandas-dev:main' into main
sanjanam1998 Oct 18, 2025
d8acdff
git fix
sanjanam1998 Oct 18, 2025
5331158
Merge branch 'main' into main
sanjanam1998 Oct 18, 2025
0890621
Merge branch 'main' into main
sanjanam1998 Oct 20, 2025
79a82d5
Merge branch 'pandas-dev:main' into main
sanjanam1998 Oct 21, 2025
b643a7c
test changes
sanjanam1998 Oct 22, 2025
660da5b
Merge branch 'main' into main
sanjanam1998 Oct 22, 2025
977d887
Merge branch 'main' into main
sanjanam1998 Oct 22, 2025
7198d97
Update pandas/core/algorithms.py
sanjanam1998 Oct 23, 2025
6d0133e
Merge branch 'main' into main
sanjanam1998 Oct 23, 2025
5c36d37
Merge branch 'main' into main
sanjanam1998 Oct 24, 2025
409f8bb
Merge branch 'pandas-dev:main' into main
sanjanam1998 Oct 28, 2025
85630e8
test optimize
sanjanam1998 Oct 28, 2025
ffad503
none typing
sanjanam1998 Oct 29, 2025
7b573c2
improving test cases
sanjanam1998 Oct 30, 2025
7acab2f
Merge branch 'main' into main
sanjanam1998 Oct 30, 2025
5a1ce64
Merge branch 'main' into main
sanjanam1998 Oct 30, 2025
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test changes
  • Loading branch information
sanjanam1998 committed Oct 22, 2025
commit b643a7c106fa78066215d44b03245a25dd25711a
20 changes: 4 additions & 16 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,24 +937,12 @@ def value_counts_internal(
and not sort
and isinstance(values, (DatetimeIndex, TimedeltaIndex))
and values.inferred_freq is not None
and len(idx) == len(values)
and idx.equals(values)
):
# freq preservation
# Check if the result would be the same as input
if len(idx) == len(values) and idx.equals(values):
# Rebuild idx with the correct type and inferred frequency
if isinstance(values, DatetimeIndex):
idx = DatetimeIndex(
idx._data if hasattr(idx, "_data") else idx.values,
freq=values.inferred_freq,
name=idx.name,
)

elif isinstance(values, TimedeltaIndex):
idx = TimedeltaIndex(
idx._data if hasattr(idx, "_data") else idx.values,
freq=values.inferred_freq,
name=idx.name,
)
# Rebuild idx with the correct type and inferred frequency
idx.freq = values.inferred_freq

result = Series(counts, index=idx, name=name, copy=False)

Expand Down
200 changes: 64 additions & 136 deletions pandas/tests/base/test_value_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,151 +341,79 @@ def test_value_counts_object_inference_deprecated():
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",
"index",
[
("dt", "D"),
("td", "D"),
("td", Timedelta(hours=1)),
pd.date_range("2016-01-01", periods=5, freq="D"),
pd.timedelta_range(Timedelta(0), periods=5, freq="h"),
],
ids=["DatetimeIndex[D]", "TimedeltaIndex[h]"],
)
def test_value_counts_freq_drops_datetimelike_when_sorted(kind, freq):
idx = _vc_make_index(kind, periods=5, freq=freq)
vc = idx.value_counts() # default sort=True (reorders)
assert vc.index.freq is None


@pytest.mark.parametrize(
"kind,freq",
"build,kwargs,exp_preserve,exp_hasnans,exp_index_fn",
[
("dt", "D"),
("td", "D"),
("td", Timedelta(hours=1)),
(lambda idx: idx, {"sort": False}, True, False, lambda idx, obj: idx),
(
lambda idx: idx,
{"sort": False, "normalize": True},
True,
False,
lambda idx, obj: idx,
),
(lambda idx: idx, {}, False, False, None),
(
lambda idx: idx.insert(1, idx[1]),
{"sort": False},
False,
False,
lambda idx, obj: type(idx)(idx, freq=None),
),
(
lambda idx: idx.delete(2),
{"sort": False},
False,
False,
lambda idx, obj: type(idx)(obj, freq=None),
),
(
lambda idx: idx.insert(1, pd.NaT),
{"sort": False, "dropna": False},
False,
True,
lambda idx, obj: type(idx)(
list(idx[:1]) + [pd.NaT] + list(idx[1:]), freq=None
),
),
(
lambda idx: idx.insert(1, pd.NaT),
{"sort": False, "dropna": True},
False,
False,
lambda idx, obj: type(idx)(idx, freq=None),
),
],
)
def test_value_counts_freq_drops_datetimelike_with_duplicates(kind, freq):
base = _vc_make_index(kind, periods=5, freq=freq)
obj = base.insert(1, base[1]) # duplicate one label
vc = obj.value_counts(sort=False)
assert vc.index.freq is None


@pytest.mark.parametrize(
"kind,freq",
[
("dt", "D"),
("td", "D"),
("td", Timedelta(hours=1)),
],
)
def test_value_counts_freq_drops_datetimelike_with_gap(kind, freq):
base = _vc_make_index(kind, periods=5, freq=freq)
obj = base.delete(2) # remove one step to break contiguity
vc = obj.value_counts(sort=False)
assert vc.index.freq is None
def test_value_counts_freq_datetimelike(
index, build, kwargs, exp_preserve, exp_hasnans, exp_index_fn
):
obj = build(index)
vc = obj.value_counts(**kwargs)

# without sort
if exp_index_fn is not None:
expected_idx = exp_index_fn(index, obj)
tm.assert_index_equal(vc.index, expected_idx)

@pytest.mark.parametrize(
"kind,freq,dropna,expect_hasnans",
[
("dt", "D", False, True), # keep NaT
("dt", "D", True, False), # drop NaT
("td", "D", False, True),
("td", "D", True, False),
("td", Timedelta(hours=1), False, True),
("td", Timedelta(hours=1), True, False),
],
)
def test_value_counts_freq_drops_datetimelike_with_nat(
kind, freq, dropna, expect_hasnans
):
base = _vc_make_index(kind, periods=3, freq=freq)
obj = base.insert(1, pd.NaT)
vc = obj.value_counts(dropna=dropna, sort=False)
assert vc.index.freq is None
assert vc.index.hasnans is expect_hasnans
# freq preservation / drop
if exp_preserve:
assert vc.index.freq == index.freq
else:
assert vc.index.freq is None

# NaT presence
assert vc.index.hasnans is exp_hasnans

@pytest.mark.parametrize(
"freq,start,periods,sort",
[
("D", "2016-01-01", 5, False),
("D", "2016-01-01", 5, True),
("M", "2016-01", 6, False), # MonthEnd
("M", "2016-01", 6, True),
("Q-DEC", "2016Q1", 4, False), # QuarterEnd (Dec anchored)
("Q-DEC", "2016Q1", 4, True),
("Y-DEC", "2014", 3, False), # YearEnd (Dec anchored)
("Y-DEC", "2014", 3, True),
],
)
def test_value_counts_period_freq_preserved_sort_and_nosort(freq, start, periods, sort):
pi = pd.period_range(start=start, periods=periods, freq=freq)
vc = pi.value_counts(sort=sort)
assert isinstance(vc.index, pd.PeriodIndex)
assert vc.index.dtype == pi.dtype
assert vc.index.freq == pi.freq


def test_value_counts_period_freq_preserved_with_duplicates():
pi = pd.period_range("2016-01", periods=5, freq="M")
obj = pi.insert(1, pi[1]) # duplicate one label
vc = obj.value_counts(sort=False)
assert isinstance(vc.index, pd.PeriodIndex)
assert vc.index.dtype == pi.dtype
assert vc.index.freq == pi.freq


def test_value_counts_period_freq_preserved_with_gap():
pi = pd.period_range("2016-01", periods=5, freq="M")
obj = pi.delete(2) # remove one element
vc = obj.value_counts(sort=False)
assert isinstance(vc.index, pd.PeriodIndex)
assert vc.index.dtype == pi.dtype
assert vc.index.freq == pi.freq


def test_value_counts_period_freq_preserved_with_normalize():
pi = pd.period_range("2016-01", periods=4, freq="M")
vc = pi.value_counts(normalize=True, sort=False)
assert isinstance(vc.index, pd.PeriodIndex)
assert vc.index.dtype == pi.dtype
assert vc.index.freq == pi.freq
assert np.isclose(vc.values, 1 / len(pi)).all()


def test_value_counts_period_freq_preserved_with_nat_dropna_true():
pi = pd.period_range("2016-01", periods=5, freq="M")
obj = pi.insert(1, pd.NaT)
vc = obj.value_counts(dropna=True, sort=False)
assert not vc.index.hasnans
assert isinstance(vc.index, pd.PeriodIndex)
assert vc.index.dtype == pi.dtype
assert vc.index.freq == pi.freq
# without normalize
if kwargs.get("normalize", False):
expected_val = 1.0 / len(index)
assert np.isclose(vc.to_numpy(), expected_val).all()
Loading