Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

BUG: Collision between equivalent frequencies 'QS-… #61142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Next Next commit
Resolving #61086 : BUG: Collision between equivalent frequencies 'QS-…
…FEB' and 'QS-NOV'
  • Loading branch information
David Jun committed Mar 18, 2025
commit 4bd0594096b699f6a2d36cf8340db5946783abdc
1 change: 1 addition & 0 deletions 1 doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Other enhancements
- :meth:`pandas.concat` will raise a ``ValueError`` when ``ignore_index=True`` and ``keys`` is not ``None`` (:issue:`59274`)
- :py:class:`frozenset` elements in pandas objects are now natively printed (:issue:`60690`)
- Add ``"delete_rows"`` option to ``if_exists`` argument in :meth:`DataFrame.to_sql` deleting all records of the table before inserting data (:issue:`37210`).
- Added :func:`DateTimeIndex.set_freq` and :class:`DateTimeIndex` now supports assigning equivalent quarerly start frequency (:issue:`61086`)
- Added half-year offset classes :class:`HalfYearBegin`, :class:`HalfYearEnd`, :class:`BHalfYearBegin` and :class:`BHalfYearEnd` (:issue:`60928`)
- Errors occurring during SQL I/O will now throw a generic :class:`.DatabaseError` instead of the raw Exception type from the underlying driver manager library (:issue:`60748`)
- Implemented :meth:`Series.str.isascii` and :meth:`Series.str.isascii` (:issue:`59091`)
Expand Down
7 changes: 6 additions & 1 deletion 7 pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -2519,8 +2519,13 @@ def _validate_inferred_freq(
-------
freq : DateOffset or None
"""
offset1 = to_offset(freq)
offset2 = to_offset(inferred_freq)

freq_equal = type(offset1) == type(offset2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think this is sufficient, for example QuarterBegin: startingMonth=2 and QuarterBegin: startingMonth=3 will both have the same type, but are not the same frequency.


if inferred_freq is not None:
if freq is not None and freq != inferred_freq:
if freq is not None and not freq_equal:
raise ValueError(
f"Inferred frequency {inferred_freq} from passed "
"values does not conform to passed frequency "
Expand Down
28 changes: 28 additions & 0 deletions 28 pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,34 @@ def _can_range_setop(self, other) -> bool:

# --------------------------------------------------------------------

def _set_freq(self, freq, *, inplace: bool = False):
"""
Set a new frequency for this DatetimeIndex.

Parameters
----------
freq : str, Timedelta, datetime.timedelta, or DateOffset, default 'S'
Frequency strings can have multiples, e.g. '5h'. See
:ref:`here <timeseries.offset_aliases>` for a list of
frequency aliases.
inplace : bool, default False
If True, modifies object in place. Otherwise, returns a new DateTimeIndex

Returns
-------
DatetimeIndex
Fixed frequency DatetimeIndex
"""

if inplace:
self._freq = to_offset(freq)
else:
new_index = self.copy()
new_index.freq = to_offset(freq)
return new_index

# --------------------------------------------------------------------

def _get_time_micros(self) -> npt.NDArray[np.int64]:
"""
Return the number of microseconds since midnight.
Expand Down
9 changes: 9 additions & 0 deletions 9 pandas/tests/indexes/datetimes/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,3 +1204,12 @@ def test_dti_constructor_object_dtype_dayfirst_yearfirst_with_tz(self):
result2 = DatetimeIndex([val], tz="US/Pacific", yearfirst=True)
expected2 = DatetimeIndex([yfirst]).as_unit("s")
tm.assert_index_equal(result2, expected2)

def test_datetimeindex_equivalent_freq(self):
idx2 = date_range("2020-02-01", freq="QS-FEB", periods=4)
new_idx2 = idx2._set_freq("QS-MAY")
tm.assert_index_equal(new_idx2, idx2)
assert new_idx2.freq == "QS-MAY"

idx2._set_freq("QS-MAY", inplace=True)
assert idx2.freq == "QS-MAY"
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.