-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[ ] (optional) I have confirmed this bug exists on the master branch of pandas.
Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.
Code Sample, a copy-pastable example
import pandas as pd
from datetime import datetime
data=pd.DataFrame([[430., 630.],
[540., 660.],
[610., 720.]],
columns=[1, 2],
index=pd.date_range(datetime(2019, 1, 1, 12),
datetime(2019, 1, 1, 12, 30),
freq='15min'))
pd.tseries.frequencies.to_offset(pd.infer_freq(data.index))
>> <15 * Minutes>
pd.tseries.frequencies.to_offset(pd.infer_freq(data.index)) / 2
>> <450 * Secondes>
pd.tseries.offsets.DateOffset(minutes=15)
>> <DateOffset: minutes=15>
pd.tseries.offsets.DateOffset(minutes=15) / 2
>> TypeError: unsupported operand type(s) for /: 'DateOffset' and 'int'
Problem description
The function pd.tseries.frequencies.to_offset
does not really provide a DateOffset
object. As I understand the code right there are several types of DateOffsets e.g. Minutes, Seconds that are not compareable with the DateOffset object because they are having different properties.
Expected Output
One Unified pd.DateOffset Object with same properties for all inherit Objects like Minutes and Seconds
Output of pd.show_versions()
Comment From: jbrockmendel
One Unified pd.DateOffset Object with same properties for all inherit Objects like Minutes and Seconds
Why would you expect this? the DateOffset
class is way less efficient than the Minute
or Second
class.
Comment From: meteoDaniel
@jbrockmendel if it is less efficient why it was not depricate? I was very confused by the different behaviour of DateOffset objects. I think there should not be several DateOffset objects available.
To answer your question: If I am calling the function to_offset
I would expect to receive a DateOffset
object but I receive Seconds, Minutes or other 'Time' objects.
This is a part of the docstring for to_offset
:
def to_offset(freq) -> Optional[DateOffset]:
"""
Return DateOffset object from string or tuple representation
or datetime.timedelta object.
Parameters
----------
freq : str, tuple, datetime.timedelta, DateOffset or None
Returns
-------
DateOffset
None if freq is None.
Comment From: jbrockmendel
if it is less efficient why it was not depricate?
I'd be fine with this if you'd like to make a PR.
Comment From: meteoDaniel
Next week I can have a closer look to pandas source code to prepare a PR. I have learned that it is a huge effort to make it accurate for such a big project. I hope some other guys will join
the conversation.
Actually my idea is to return e.g. a minute object if DateOffset(hours=1,Minutes=20)
is parsed like that because minute is the smallest given level. And to deprecate the origin DateOffset
object. In the end working with minutes secondes or other objects makes no differences in mathematical operations and it still defines your Dateoffset. And for sure it should go along with Timedelta operations.