Skip to content

BUG: can't resample with non-nano dateindex, out-of-nanosecond-bounds #51274

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 8 commits into from
Feb 13, 2023

Conversation

MarcoGorelli
Copy link
Member

Comment on lines 2133 to 2140
# TODO is there anything which can be reused here?
freq_value = freq.nanos
if unit == "us":
freq_value = freq_value // 1_000
elif unit == "ms":
freq_value = freq_value // 1_000_000
elif unit == "s":
freq_value = freq_value // 1_000_000_000
Copy link
Member Author

@MarcoGorelli MarcoGorelli Feb 9, 2023

Choose a reason for hiding this comment

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

@jbrockmendel is there any existing function which can be reused here?

there is periods_per_second, but that takes npydatetime_unit rather than str (is there a function to convert between them?)

I didn't find one, but I'll look more carefully later

Copy link
Member

Choose a reason for hiding this comment

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

abbrev_to_npy_unit

in tzconversion there are two places where we do approximately this, one of them has a comment to de-duplicate

@MarcoGorelli MarcoGorelli marked this pull request as ready for review February 9, 2023 21:34
@mroeschke mroeschke added Resample resample method Non-Nano datetime64/timedelta64 with non-nanosecond resolution labels Feb 10, 2023
offset = offset.as_unit("ns")
offset = offset.as_unit(unit)

freq_value = freq.nanos // (
Copy link
Member

Choose a reason for hiding this comment

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

would it be simpler to do Timedelta(freq).as_unit(unit)._value?

Copy link
Member

Choose a reason for hiding this comment

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

(i think freq.nanos is a pattern we need to move away from regardless since there is a risk of overflow)

Copy link
Member Author

Choose a reason for hiding this comment

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

good one, thanks!

@jbrockmendel
Copy link
Member

One comment on the non-test code, otherwise looks good. i dont know the resample code well enough to know on sight if the test "expected" is correct, but trust you

@MarcoGorelli
Copy link
Member Author

One comment on the non-test code, otherwise looks good. i dont know the resample code well enough to know on sight if the test "expected" is correct, but trust you

Thanks for your review @jbrockmendel !

Regarding the expected value: it matches what pandas would currently give for nanosecond resolution:

In [33]:     idx = date_range("1983-01-01", "2000-01-01", freq="Y")
    ...:     ser = Series([1, 4, 2, 8, 5, 7, 1, 4, 2, 8, 5, 7, 1, 4, 2, 8, 5], index=idx)
    ...:     ser.resample("2Y").mean()
Out[33]:
1983-12-31    1.0
1985-12-31    3.0
1987-12-31    6.5
1989-12-31    4.0
1991-12-31    3.0
1993-12-31    6.5
1995-12-31    4.0
1997-12-31    3.0
1999-12-31    6.5
Freq: 2A-DEC, dtype: float64

, so if that's right, then this is right.

OK to merge?

Copy link
Member

@jbrockmendel jbrockmendel left a comment

Choose a reason for hiding this comment

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

LGTM

@mroeschke mroeschke added this to the 2.0 milestone Feb 13, 2023
@mroeschke mroeschke merged commit f3d4113 into pandas-dev:main Feb 13, 2023
@mroeschke
Copy link
Member

Thanks @MarcoGorelli

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Non-Nano datetime64/timedelta64 with non-nanosecond resolution Resample resample method
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: can't resample with non-nano dateindex, out-of-nanosecond-bounds
3 participants