Description
Pandas version checks
-
I have checked that this issue has not already been reported.
-
I have confirmed this bug exists on the latest version of pandas.
-
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
In [1]: DatetimeIndex(date_range('2000', periods=2).as_unit('s').normalize(), freq='D')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
File ~/pandas-dev/pandas/core/arrays/datetimelike.py:1912, in TimelikeOps._validate_frequency(cls, index, freq, **kwargs)
1911 if not np.array_equal(index.asi8, on_freq.asi8):
-> 1912 raise ValueError
1913 except ValueError as err:
ValueError:
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
Cell In[1], line 1
----> 1 DatetimeIndex(date_range('2000', periods=2).as_unit('s').normalize(), freq='D')
File ~/pandas-dev/pandas/core/indexes/datetimes.py:335, in DatetimeIndex.__new__(cls, data, freq, tz, normalize, closed, ambiguous, dayfirst, yearfirst, dtype, copy, name)
332 data = data.copy()
333 return cls._simple_new(data, name=name)
--> 335 dtarr = DatetimeArray._from_sequence_not_strict(
336 data,
337 dtype=dtype,
338 copy=copy,
339 tz=tz,
340 freq=freq,
341 dayfirst=dayfirst,
342 yearfirst=yearfirst,
343 ambiguous=ambiguous,
344 )
346 subarr = cls._simple_new(dtarr, name=name)
347 return subarr
File ~/pandas-dev/pandas/core/arrays/datetimes.py:360, in DatetimeArray._from_sequence_not_strict(cls, data, dtype, copy, tz, freq, dayfirst, yearfirst, ambiguous)
356 result = result.as_unit(unit)
358 if inferred_freq is None and freq is not None:
359 # this condition precludes `freq_infer`
--> 360 cls._validate_frequency(result, freq, ambiguous=ambiguous)
362 elif freq_infer:
363 # Set _freq directly to bypass duplicative _validate_frequency
364 # check.
365 result._freq = to_offset(result.inferred_freq)
File ~/pandas-dev/pandas/core/arrays/datetimelike.py:1923, in TimelikeOps._validate_frequency(cls, index, freq, **kwargs)
1917 raise err
1918 # GH#11587 the main way this is reached is if the `np.array_equal`
1919 # check above is False. This can also be reached if index[0]
1920 # is `NaT`, in which case the call to `cls._generate_range` will
1921 # raise a ValueError, which we re-raise with a more targeted
1922 # message.
-> 1923 raise ValueError(
1924 f"Inferred frequency {inferred} from passed values "
1925 f"does not conform to passed frequency {freq.freqstr}"
1926 ) from err
ValueError: Inferred frequency None from passed values does not conform to passed frequency D
In [2]: DatetimeIndex(date_range('2000', periods=2).as_unit('ns').normalize(), freq='D')
Out[2]: DatetimeIndex(['2000-01-01', '2000-01-02'], dtype='datetime64[ns]', freq='D')
Issue Description
I don't think the first case should raise?
Expected Behavior
DatetimeIndex(['2000-01-01', '2000-01-02'], dtype='datetime64[s]', freq='D')
Installed Versions
INSTALLED VERSIONS
commit : 954cf55
python : 3.11.1.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.102.1-microsoft-standard-WSL2
Version : #1 SMP Wed Mar 2 00:30:59 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8
pandas : 2.0.0.dev0+1168.g954cf55938
numpy : 1.24.1
pytz : 2022.7
dateutil : 2.8.2
setuptools : 65.7.0
pip : 22.3.1
Cython : 0.29.33
pytest : 7.2.0
hypothesis : 6.52.1
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.8.0
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : None
qtpy : None
pyqt5 : None
None