0

I am using the below logic to calculate the average timedelta inside a python list.

from datetime import datetime,timedelta def entry_rate(entry_timestamps): entry_deltas = [d1-d2 for d1, d2 in zip(entry_timestamps[1:], entry_timestamps[:-1])] average_timedelta = (sum(entry_deltas, timedelta( 0)) / len(entry_deltas)).total_seconds() if len(entry_deltas) != 0 else -1.0 return average_timedelta 

Here is the input to the function:-

entry_timestamps = [datetime.datetime(2019, 11, 13, 7, 36, 21), datetime.datetime(2019, 11, 13, 7, 40, 53), datetime.datetime(2019, 11, 13, 7, 45, 25), datetime.datetime(2019, 11, 13, 7, 49, 58), datetime.datetime(2019, 11, 13, 7, 54, 30), datetime.datetime(2019, 11, 13, 7, 58, 32), datetime.datetime(2019, 11, 13, 8, 2, 34), datetime.datetime(2019, 11, 13, 8, 6, 36), datetime.datetime(2019, 11, 13, 8, 10, 38), datetime.datetime(2019, 11, 13, 8, 14, 40), datetime.datetime(2019, 11, 13, 8, 18, 42), datetime.datetime(2019, 11, 13, 8, 22, 44), datetime.datetime(2019, 11, 13, 8, 26, 46), datetime.datetime(2019, 11, 13, 8, 29, 18)] 

However, I am getting the below error.

in prepare_llog(llog_pd) 288 289 result_df['entry_rate'] =\ --> 290 result_df['entry_timestamps'].apply(lambda x: compute_entry_rate(x)) 291 292

~/miniconda3/envs/jupyter_21f8c25de0/lib/python3.6/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds) 4043
else: 4044 values = self.astype(object).values -> 4045 mapped = lib.map_infer(values, f, convert=convert_dtype) 4046 4047 if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

in (x) 288 289 result_df['entry_rate'] =\ --> 290 result_df['entry_timestamps'].apply(lambda x: compute_entry_rate(x)) 291 292

in compute_entry_rate(entry_timestamps) 49 50 average_timedelta = (sum(entry_deltas, timedelta( ---> 51 0)) / len(entry_deltas)).total_seconds() if len(entry_deltas) != 0 else -1.0 52 return average_timedelta 53

TypeError: _() takes 1 positional argument but 2 were given

Can anyone please suggest how to fix this?

2
  • Can you edit your question to include the entire stack trace, not just the last line of it? Commented Feb 19, 2020 at 19:16
  • 1
    calling entry_rate(entry_timestamps) causes no error to me Commented Feb 19, 2020 at 19:23

1 Answer 1

1

You can do this with pandas like this:

S = pd.Series([datetime.datetime(2019, 11, 13, 7, 36, 21), datetime.datetime(2019, 11, 13, 7, 40, 53), datetime.datetime(2019, 11, 13, 7, 45, 25), datetime.datetime(2019, 11, 13, 7, 49, 58), datetime.datetime(2019, 11, 13, 7, 54, 30), datetime.datetime(2019, 11, 13, 7, 58, 32), datetime.datetime(2019, 11, 13, 8, 2, 34), datetime.datetime(2019, 11, 13, 8, 6, 36), datetime.datetime(2019, 11, 13, 8, 10, 38), datetime.datetime(2019, 11, 13, 8, 14, 40), datetime.datetime(2019, 11, 13, 8, 18, 42), datetime.datetime(2019, 11, 13, 8, 22, 44), datetime.datetime(2019, 11, 13, 8, 26, 46), datetime.datetime(2019, 11, 13, 8, 29, 18)]) S.diff().mean().total_seconds() 

Output:

244.384615384 

Where as your function, entry_rate returns, without errors.:

 entry_rate(entry_timestamps) 

Output:

 244.384615 

Timings:

Using entry_rate:

5.16 µs ± 193 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Using pandas functions:

389 µs ± 22.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Sign up to request clarification or add additional context in comments.

3 Comments

strange. Is it something todo with Python or Pandas version?
@amitpanda I am using version 1.0.1 with python 3.7.3
I am using pandas 0.25.3 and python 3.6

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.