Skip to content

ENH: infer freq in timedelta_range #36634

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

Open
simonjayhawkins opened this issue Sep 25, 2020 · 3 comments
Open

ENH: infer freq in timedelta_range #36634

simonjayhawkins opened this issue Sep 25, 2020 · 3 comments
Labels
Enhancement Needs Discussion Requires discussion from core team before further action Timedelta Timedelta data type

Comments

@simonjayhawkins
Copy link
Member

xref #36582, #35897, #36595, #32377, #31195

there may be cases where a frequency should not be inferred by timedelta_range due to rounding issues

e.g. pd.timedelta_range('0s','1s', periods=31)

The docs https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.timedelta_range.html begin 'Return a fixed frequency TimedeltaIndex...' so having None (no inferred frequency) maybe not desirable.

cc @jbrockmendel @jreback @phofl

@simonjayhawkins simonjayhawkins added API Design Needs Discussion Requires discussion from core team before further action Timedelta Timedelta data type labels Sep 25, 2020
@simonjayhawkins
Copy link
Member Author

so having None (no inferred frequency) maybe not desirable.

for instance should pd.timedelta_range('0s','1s', periods=31) perhaps raise with a message along the lines 'ValueError: cannot infer a fixed frequency from start, end and periods due to rounding errors` or is a fractional freq a feasible option?

@simonjayhawkins
Copy link
Member Author

maybe could only infer frequency if no rounding errors...

> c:\users\simon\pandas\pandas\core\arrays\timedeltas.py(271)_generate_range()
-> freq = to_offset(td)
(Pdb) td
Timedelta('0 days 00:00:00.033333333')
(Pdb) td.value
33333333
(Pdb) start.value
0
(Pdb) end.value
1000000000
(Pdb) periods
31
(Pdb) assert start.value + td.value * (periods - 1) == end.value
*** AssertionError
(Pdb) start.value + td.value * (periods -1)
999999990

@jbrockmendel
Copy link
Member

or is a fractional freq a feasible option?

Not with the current implementation with TDI backed by i8.

maybe could only infer frequency if no rounding errors...

Could warn in the cases with rounding errors.

Note: this will also affect date_range.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Needs Discussion Requires discussion from core team before further action Timedelta Timedelta data type
Projects
None yet
Development

No branches or pull requests

3 participants