Viktor
Viktor

Reputation: 101

Setting X-axis limit on TimeDeltaIndex in Pandas

I have a Pandas data frame with TimeDeltaIndex (logically meant to represent data versus elapsed time) and would like to plot and set specific x-axis limits by providing start and end points as Timedeltas. However I get an error. I know that I can achieve the same by a number of ways, such as using numerical X values, or converting to Timestamps with arbitrary start time. But I'm curious why this approach is not working. Also, while on this subject, was TimeDeltaIndex conceived for this purpose - representing data versus elapsed time? Or is there a better (more flexible and convenient) tool?

import pandas as pd
import numpy as np

data=np.random.random(10)
index=pd.timedelta_range(start="00:00:00", periods=10, freq='T')

df=pd.DataFrame(data, index)

start=pd.Timedelta(value=2, unit='T')
end=pd.Timedelta(value=7, unit='T')

df.plot(xlim=(start, end))

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Upvotes: 0

Views: 253

Answers (1)

CasseNoisette
CasseNoisette

Reputation: 11

You don't need to pass Timedelta values :

import pandas as pd
import numpy as np

data=np.random.random(10)
index=pd.timedelta_range(start="00:00:00", periods=10, freq='T')

df=pd.DataFrame(data, index)

start=0 # i.e the first index value
end=40 # i.e the 40th index value

df.plot(xlim=(start, end))

This works for me !

Upvotes: 1

Related Questions