Skip to content

Commit 4928160

Browse files
author
David Jun
committed
Resolving #61086 : BUG: Collision between equivalent frequencies 'QS-FEB' and 'QS-NOV'
1 parent 4bd0594 commit 4928160

File tree

3 files changed

+7
-37
lines changed

3 files changed

+7
-37
lines changed

doc/source/whatsnew/v3.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ Other enhancements
7575
- :meth:`pandas.concat` will raise a ``ValueError`` when ``ignore_index=True`` and ``keys`` is not ``None`` (:issue:`59274`)
7676
- :py:class:`frozenset` elements in pandas objects are now natively printed (:issue:`60690`)
7777
- Add ``"delete_rows"`` option to ``if_exists`` argument in :meth:`DataFrame.to_sql` deleting all records of the table before inserting data (:issue:`37210`).
78-
- Added :func:`DateTimeIndex.set_freq` and :class:`DateTimeIndex` now supports assigning equivalent quarerly start frequency (:issue:`61086`)
7978
- Added half-year offset classes :class:`HalfYearBegin`, :class:`HalfYearEnd`, :class:`BHalfYearBegin` and :class:`BHalfYearEnd` (:issue:`60928`)
8079
- 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`)
8180
- Implemented :meth:`Series.str.isascii` and :meth:`Series.str.isascii` (:issue:`59091`)
@@ -655,6 +654,7 @@ Datetimelike
655654
- Bug in :meth:`DatetimeIndex.is_year_start` and :meth:`DatetimeIndex.is_quarter_start` returning ``False`` on double-digit frequencies (:issue:`58523`)
656655
- Bug in :meth:`DatetimeIndex.union` and :meth:`DatetimeIndex.intersection` when ``unit`` was non-nanosecond (:issue:`59036`)
657656
- Bug in :meth:`Series.dt.microsecond` producing incorrect results for pyarrow backed :class:`Series`. (:issue:`59154`)
657+
- Bug in :meth:`_validate_inferred_freq` where DateTimeIndex incorrectly raised a ``ValueError`` when assigning a logically equivalent frequency (:issue:`61086`)
658658
- Bug in :meth:`to_datetime` not respecting dayfirst if an uncommon date string was passed. (:issue:`58859`)
659659
- Bug in :meth:`to_datetime` on float array with missing values throwing ``FloatingPointError`` (:issue:`58419`)
660660
- Bug in :meth:`to_datetime` on float32 df with year, month, day etc. columns leads to precision issues and incorrect result. (:issue:`60506`)

pandas/core/indexes/datetimes.py

-28
Original file line numberDiff line numberDiff line change
@@ -421,34 +421,6 @@ def _can_range_setop(self, other) -> bool:
421421

422422
# --------------------------------------------------------------------
423423

424-
def _set_freq(self, freq, *, inplace: bool = False):
425-
"""
426-
Set a new frequency for this DatetimeIndex.
427-
428-
Parameters
429-
----------
430-
freq : str, Timedelta, datetime.timedelta, or DateOffset, default 'S'
431-
Frequency strings can have multiples, e.g. '5h'. See
432-
:ref:`here <timeseries.offset_aliases>` for a list of
433-
frequency aliases.
434-
inplace : bool, default False
435-
If True, modifies object in place. Otherwise, returns a new DateTimeIndex
436-
437-
Returns
438-
-------
439-
DatetimeIndex
440-
Fixed frequency DatetimeIndex
441-
"""
442-
443-
if inplace:
444-
self._freq = to_offset(freq)
445-
else:
446-
new_index = self.copy()
447-
new_index.freq = to_offset(freq)
448-
return new_index
449-
450-
# --------------------------------------------------------------------
451-
452424
def _get_time_micros(self) -> npt.NDArray[np.int64]:
453425
"""
454426
Return the number of microseconds since midnight.

pandas/tests/indexes/datetimes/test_constructors.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -1205,11 +1205,9 @@ def test_dti_constructor_object_dtype_dayfirst_yearfirst_with_tz(self):
12051205
expected2 = DatetimeIndex([yfirst]).as_unit("s")
12061206
tm.assert_index_equal(result2, expected2)
12071207

1208-
def test_datetimeindex_equivalent_freq(self):
1209-
idx2 = date_range("2020-02-01", freq="QS-FEB", periods=4)
1210-
new_idx2 = idx2._set_freq("QS-MAY")
1211-
tm.assert_index_equal(new_idx2, idx2)
1212-
assert new_idx2.freq == "QS-MAY"
1213-
1214-
idx2._set_freq("QS-MAY", inplace=True)
1215-
assert idx2.freq == "QS-MAY"
1208+
def test_validate_inferred_freq_equivalence(self):
1209+
idx = date_range("2020-02-01", freq="QS-FEB", periods=4)
1210+
1211+
new_idx = DatetimeIndex(idx, freq="QS-MAY")
1212+
1213+
assert isinstance(new_idx, DatetimeIndex)

0 commit comments

Comments
 (0)