diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 649629714c3b1..fe74258325902 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -635,7 +635,7 @@ Datetimelike - Bug in :meth:`DataFrame.eq` comparison against ``NaT`` incorrectly returning ``True`` or ``NaN`` (:issue:`15697`, :issue:`22163`) - Bug in :class:`DatetimeIndex` subtraction that incorrectly failed to raise ``OverflowError`` (:issue:`22492`, :issue:`22508`) - Bug in :class:`DatetimeIndex` incorrectly allowing indexing with ``Timedelta`` object (:issue:`20464`) -- +- Bug in :class:`DatetimeIndex` where frequency was being set if original frequency was ``None`` (:issue:`22150`) Timedelta ^^^^^^^^^ diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 46741ab15aa31..9b00f21668bf5 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -860,8 +860,6 @@ def union_many(self, others): if isinstance(this, DatetimeIndex): this._tz = timezones.tz_standardize(tz) - if this.freq is None: - this.freq = to_offset(this.inferred_freq) return this def join(self, other, how='left', level=None, return_indexers=False, diff --git a/pandas/tests/frame/test_apply.py b/pandas/tests/frame/test_apply.py index 8beab3fb816df..1452e1ab8d98d 100644 --- a/pandas/tests/frame/test_apply.py +++ b/pandas/tests/frame/test_apply.py @@ -11,6 +11,8 @@ import warnings import numpy as np +from hypothesis import given +from hypothesis.strategies import composite, dates, integers, sampled_from from pandas import (notna, DataFrame, Series, MultiIndex, date_range, Timestamp, compat) @@ -1155,3 +1157,24 @@ def test_agg_cython_table_raises(self, df, func, expected, axis): # GH21224 with pytest.raises(expected): df.agg(func, axis=axis) + + @composite + def indices(draw, max_length=5): + date = draw( + dates( + min_value=Timestamp.min.ceil("D").to_pydatetime().date(), + max_value=Timestamp.max.floor("D").to_pydatetime().date(), + ).map(Timestamp) + ) + periods = draw(integers(0, max_length)) + freq = draw(sampled_from(list("BDHTS"))) + dr = date_range(date, periods=periods, freq=freq) + return pd.DatetimeIndex(list(dr)) + + @given(index=indices(5), num_columns=integers(0, 5)) + def test_frequency_is_original(self, index, num_columns): + # GH22150 + original = index.copy() + df = DataFrame(True, index=index, columns=range(num_columns)) + df.apply(lambda x: x) + assert index.freq == original.freq