Monday, 12 August 2013

Pandas: use the "and" of two series in `where`

Pandas: use the "and" of two series in `where`

I have written a small function to get the log returns of a series:
def get_log_returns(series):
logs = numpy.log(series.astype('float64') /
series.astype('float64').shift(1))
return logs
Now I would like to make sure I only include logs that are "reasonable". I
know I can use where to exclude logs that are infinity:
def get_log_returns(series):
logs = numpy.log(series.astype('float64') /
series.astype('float64').shift(1))
return logs.where(logs < numpy.inf)
But what if on top I wanted to exclude logs that are negative? I was
hoping that something like this would work:
def get_log_returns(series):
logs = numpy.log(series.astype('float64') /
series.astype('float64').shift(1))
return logs.where((logs < numpy.inf) and (logs > 0))
But this gives me a
ValueError: The truth value of an array with more than one element is
ambiguous.
Use a.any() or a.all()
Any ideas how to do this?

No comments:

Post a Comment