Skip to content

BUG: DatetimeIndex with non-nano values and freq='D' throws ValueError #50773

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

Merged
merged 3 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def _from_sequence_not_strict(

if inferred_freq is None and freq is not None:
# this condition precludes `freq_infer`
cls._validate_frequency(result, freq, ambiguous=ambiguous)
cls._validate_frequency(result, freq, ambiguous=ambiguous, unit=result.unit)
Copy link
Member

Choose a reason for hiding this comment

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

i have a branch where i added unit=index.unit inside _validate_frequency. any reason to that here instead of there?

Copy link
Member Author

Choose a reason for hiding this comment

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

sorry which branch?

Copy link
Member

Choose a reason for hiding this comment

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

Just a local branch

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah that works too


elif freq_infer:
# Set _freq directly to bypass duplicative _validate_frequency
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/frame/methods/test_asfreq.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@


class TestAsFreq:
@pytest.fixture(params=["s", "ms", "us", "ns"])
def unit(self, request):
return request.param

def test_asfreq2(self, frame_or_series):
ts = frame_or_series(
[0.0, 1.0, 2.0],
Expand Down Expand Up @@ -197,3 +201,11 @@ def test_asfreq_with_unsorted_index(self, frame_or_series):

result = result.asfreq("D")
tm.assert_equal(result, expected)

def test_asfreq_after_normalize(self, unit):
# https://github.com/pandas-dev/pandas/issues/50727
result = DatetimeIndex(
date_range("2000", periods=2).as_unit(unit).normalize(), freq="D"
)
expected = DatetimeIndex(["2000-01-01", "2000-01-02"], freq="D").as_unit(unit)
tm.assert_index_equal(result, expected)