Skip to content
Merged
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
preserving freq without patching
  • Loading branch information
sanjanam1998 committed Oct 15, 2025
commit 74ad212e80a50918ced82a727cf4cb98d6fd8f48
59 changes: 29 additions & 30 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,26 @@ def value_counts_internal(
Series,
)

def _preserve_freq(original_values, result_index):
freq = getattr(original_values, "freq", None)

if (
freq is not None
and type(original_values) is type(result_index)
and len(result_index) == len(original_values)
and result_index.equals(original_values)
):
try:
# Rebuild index with freq using the same constructor
return type(result_index)(
result_index._data, freq=freq, name=result_index.name
)
except (TypeError, ValueError):
# If reconstruction fails, return original index
pass

return result_index

index_name = getattr(values, "name", None)
name = "proportion" if normalize else "count"

Expand Down Expand Up @@ -929,6 +949,15 @@ def value_counts_internal(
# Starting in 3.0, we no longer perform dtype inference on the
# Index object we construct here, xref GH#56161
idx = Index(keys, dtype=keys.dtype, name=index_name)

if (
bins is None
and not sort
and hasattr(values, "freq")
and values.freq is not None
):
idx = _preserve_freq(values, idx)

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

if sort:
Expand All @@ -937,36 +966,6 @@ def value_counts_internal(
if normalize:
result = result / counts.sum()

# freq patching for DatetimeIndex, TimedeltaIndex
try:
from pandas import (
DatetimeIndex,
TimedeltaIndex,
)

if (
bins is None
and not sort
and isinstance(values, (DatetimeIndex, TimedeltaIndex))
and values.freq is not None
and isinstance(result.index, (DatetimeIndex, TimedeltaIndex))
and len(result.index) == len(values)
and result.index.equals(values)
):
base_freq = values.freq
# Rebuild the index with the original freq; name preserved.
if isinstance(result.index, DatetimeIndex):
result.index = DatetimeIndex(
result.index._data, freq=base_freq, name=result.index.name
)
else: # TimedeltaIndex
result.index = TimedeltaIndex(
result.index._data, freq=base_freq, name=result.index.name
)
except Exception:
# If freq patching fails, does not affect value_counts
pass

return result


Expand Down
Loading