Sam
Sam

Reputation: 565

Python convert a type 'datetime.timedelta' to 'pandas.tseries.offsets'

How can I convert a 'datetime.timedelta' object to a 'pandas.tseries.offsets' in Python? For instance: datetime.timedelta(1) to to_offset('1D') ?

The long story: I would like to plot a Pandas DataFrame with OHLC bars, but when there are too many data points, the bars are too thin and the charts becomes un readable. In that case, I would like to compute bars with a longer period so as to get less than 100 bars on my chart.

Given a DataFrame df (data can be anything between second to monthly periodicity)

  1. step 1: get dataframe period:

    source_period = min([t2-t1 for t2, t1 in zip(df.index[1:], df.index[:-1])])

    (I cannot use df.index.freq here)

  2. step 2: estimate target period:

    target_period = source_period * float(len(df.index))/100

  3. step 3: group-combine data

    grouped_df = df.groupby(pd.TimeGrouper(period_convert(target_period ))).agg(ohlc_combine)

I miss the period_convert function where I do not really know where to start.

For the sake of having a starting point, my ridiculously ugly hack to do so:

target_datapoints = 100
source_period = min([t2-t1 for t2, t1 in zip(df.index[1:], df.index[:-1])])
target_period = source_period.total_seconds() * float(len(df.index)) / target_datapoints
target_offset = str(int(target_period)) + 'S'
target_df = df.groupby(pd.TimeGrouper(target_offset)).agg(ohlc_combine)

Issues: ugly, slow, edgy cases not handled...

Upvotes: 0

Views: 1344

Answers (1)

Jeff
Jeff

Reputation: 129048

you can just construct it directly

e.g. say source period was 60S and now target_period is 301S, just use that.

If you want to change these to 'bigger' time periods, you could do this (requires numpy 1.7), (which returns a string):

In [18]: pd.tslib.repr_timedelta64(np.timedelta64(timedelta(seconds=301)).astype('m8[ns]'))
Out[18]: '00:05:01'

But that's almost as hard to deal with.

It might be nice to have a function to do this actually.

Upvotes: 1

Related Questions