Skip to content

BUG: Week ('W') does not work as suffixes for a string passed to Timedelta. #12691

Open
@belteshassar

Description

@belteshassar

I initially posted this on #12136, as I intended to fix it with the same patch, but I think it deserves its own issue as my initial idea for a fix may in fact change the behaviour for strings suffixed by capital M. Previously, those would be interpreted as minutes because of the unit.lower() in https://github.com/pydata/pandas/blob/master/pandas/tslib.pyx#L2820

Apparently, timedelta_from_spec() doesn't recognize all the abbreviations that are allowed in the unit parameter. Consider for example:

>>> pd.Timedelta('1Y')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas\tslib.pyx", line 2373, in pandas.tslib.Timedelta.__new__ (pandas\tslib.c:43241)
    value = np.timedelta64(parse_timedelta_string(value, unit, False))
  File "pandas\tslib.pyx", line 3010, in pandas.tslib.parse_timedelta_string (pandas\tslib.c:52603)
    raise
  File "pandas\tslib.pyx", line 3005, in pandas.tslib.parse_timedelta_string (pandas\tslib.c:52506)
    r = timedelta_from_spec(number, frac, unit)
  File "pandas\tslib.pyx", line 2822, in pandas.tslib.timedelta_from_spec (pandas\tslib.c:50601)
    raise ValueError("invalid abbreviation: {0}".format(unit))
ValueError: invalid abbreviation: Y

while

>>> pd.Timedelta(1, unit='Y')
Timedelta('365 days 05:49:12')

Same goes for Week.

Month is slightly trickier. As noted above, we have this inconsistency that we might want to do something about:

>>> pd.Timedelta('1M')
Timedelta('0 days 00:01:00')
>>> pd.Timedelta(1, unit='M')
Timedelta('30 days 10:29:06')

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions