Skip to content

Bad freq invalidation in DatetimeIndex.where #24555

@TomAugspurger

Description

@TomAugspurger

What's the expected output here?

In [16]: i = pd.date_range('20130101', periods=3, tz='US/Eastern') In [17]: i2 = pd.Index([pd.NaT, pd.NaT] + i[2:].tolist()) In [18]: i.where(pd.notna(i2), i2) Out[18]: DatetimeIndex(['NaT', 'NaT', '2013-01-03 00:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq='D')

The returned DatetimeIndex doesn't pass freq validation.

In [23]: result._eadata._validate_frequency(result, result.freq) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) ~/sandbox/pandas-alt/pandas/core/arrays/datetimelike.py in _validate_frequency(cls, index, freq, **kwargs) 863 periods=len(index), freq=freq, --> 864 **kwargs) 865 if not np.array_equal(index.asi8, on_freq.asi8): ~/sandbox/pandas-alt/pandas/core/arrays/datetimes.py in _generate_range(cls, start, end, periods, freq, tz, normalize, ambiguous, nonexistent, closed) 299 if start is NaT or end is NaT: --> 300 raise ValueError("Neither `start` nor `end` can be NaT") 301 ValueError: Neither `start` nor `end` can be NaT During handling of the above exception, another exception occurred: ValueError Traceback (most recent call last) <ipython-input-23-24fa3f452eb0> in <module> ----> 1 result._eadata._validate_frequency(result, result.freq) ~/sandbox/pandas-alt/pandas/core/arrays/datetimelike.py in _validate_frequency(cls, index, freq, **kwargs) 877 raise ValueError('Inferred frequency {infer} from passed values ' 878 'does not conform to passed frequency {passed}' --> 879 .format(infer=inferred, passed=freq.freqstr)) 880 881 # monotonicity/uniqueness properties are called via frequencies.infer_freq, ValueError: Inferred frequency None from passed values does not conform to passed frequency D

Should the freq be None?

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDatetimeDatetime data dtypeFrequencyDateOffsetsfreq retentionUser expects "freq" attribute to be preserved

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions