Build a Pandas pd.tseries.offsets from timedelta

pd.timedelta month
pandas timedelta days
pandas timedelta to years
pandas month offset
pandas timedelta example
pandas business days offset
pandas timedelta to datetime
pandas add days to date

I'd like to build a Pandas pd.tseries.offsets from a datetime.timedelta

In [1]: from pandas.tseries.frequencies import to_offset
In [2]: import datetime
In [3]: td = datetime.timedelta(hours=1)


In [4]: to_offset('1H')
Out [4]: <Hour>

In [5]: to_offset(td)
Out [5]: ValueError

Any (other) idea ?


This now works for pandas 23.x. I could not find when it was introduced.

td = datetime.timedelta(hours=1)

to_offset('1H')
>>> <Hour>

to_offset(td)
>>> <Hour>

Time deltas, Parsing¶. You can construct a Timedelta scalar through various arguments: In [​15]: pd.Timedelta(pd.offsets.Second(2)) Out[15]: Timedelta('0 days 00:00:02'). DateOffset work as follows. Each offset specify a set of dates that conform to the DateOffset. For example, Bday defines this set to be the set of dates that are weekdays (M-F). To test if a date is in the set of a DateOffset dateOffset we can use the onOffset method: dateOffset.onOffset (date). If a date is not on a valid date, the rollback


to_offset returns a pd.DateOffset. So you can directly build this object:

>>> td = datetime.timedelta(hours=1)
>>> pd.DateOffset(seconds=td.total_seconds())
<DateOffset: kwds={'seconds': 3600.0}>

>>> to_offset(pd.DateOffset(seconds=td.total_seconds()))
<DateOffset: kwds={'seconds': 3600.0}>

For a slightly nicer string representation:

>>> pd.DateOffset(days=td.days, 
                  hours=td.seconds // 3600, 
                  minutes=(td.seconds // 60) % 60)
<DateOffset: kwds={'hours': 1, 'minutes': 0, 'days': 0}>

pandas.tseries.offsets.DateOffset, Parameters that add to the offset (like Timedelta):. years. months. weeks from pandas.tseries.offsets import DateOffset >>> ts = pd.Timestamp('2017-01-01  CustomBusinessDay (n=1, normalize=False, weekmask='Mon Tue Wed Thu Fri', holidays=None, calendar=None, offset=datetime.timedelta(0)) [source] ¶ DateOffset subclass representing possibly n custom business days, excluding holidays.


This cannot be done using the to_offset function directly.

However, what you can do is convert your datetime.timedelta into the appropriate string and then pass that in as an argument.

datetime.timdeltas have two properties, td.days and td.seconds which tell you the difference in days and seconds, respectively. This is to say that the timedelta corresponding to 2 days and 15 seconds would have td.days = 2 and td.seconds = 15.

The code below will construct a string with the format "XXXDYYYS" and then pass that to to_offset. The disadvantage of this is that when you print it, it doesn't look as pretty, that is it won't say <Hour>, it'll say <3600 seconds>. But I assume you can live with that.

from pandas.tseries.frequencies import to_offset
import datetime


td = datetime.timedelta(days=4, hours=1, seconds=12)

s = '{td.days}D{td.seconds}S'.format(td=td)
print(s)
# 4D3612S

offset = to_offset(s)
print(offset)
# <349212 * Seconds>

And as a nice little function:

def td_to_offset(td):
    return to_offset('{td.days}D{td.seconds}S'.format(td=td))

Python, Python | Pandas tseries.offsets. Parameters that add to the offset (like Timedelta): years, months etc. Returns : DateOffsets. Example #1: Use pandas.​tseries.offsets.DateOffset function to create dateoffsets of 2 days. Python | Pandas tseries.offsets.BusinessDay Dateoffsets are a standard kind of date increment used for a date range in Pandas. It works exactly like relativedelta in terms of the keyword args we pass in. DateOffets work as follows, each offset specify a set of dates that conform to the DateOffset.


Python, offset : datetime.timedelta(0). Returns : Business day offset. Example #1: Use pandas.tseries.offsets.BusinessDay() function to create an offset  Python | Pandas tseries.offsets.CustomBusinessHour Dateoffsets are a standard kind of date increment used for a date range in Pandas. It works exactly like relativedelta in terms of the keyword args we pass in. DateOffets work as follows, each offset specify a set of dates that conform to the DateOffset.


Working with Time Series, For example, you can manually build a date using the datetime type: In [1]: for Python's native datetime.timedelta type, and is based on numpy.timedelta64 . from pandas.tseries.offsets import BDay pd.date_range('2015-07-01', periods=5,​  to_timedelta¶. Using the top-level pd.to_timedelta, you can convert a scalar, array, list, or Series from a recognized timedelta format / value into a Timedelta type. It will construct Series if the input is a Series, a scalar if the input is scalar-like, otherwise it will output a TimedeltaIndex.


Working with Pandas datetime, import pandas as pd import numpy as np df=pd.read_csv('. Here we are adding a day(timedelta of 1 day) to the Date column in dataframe and creating import USFederalHolidayCalendar from pandas.tseries.offsets import  pandas.Timedelta¶ class pandas.Timedelta¶. Represents a duration, the difference between two dates or times. Timedelta is the pandas equivalent of python’s datetime.timedelta and is interchangeable with it in most cases.