0

I had this pandas series s:

3/28/22    127943
3/29/22    127970
3/30/22    127997
3/31/22    128019
4/1/22     128052
4/2/22     128059
4/3/22     128065
4/4/22     128086
4/5/22     128106
4/6/22     128144

I tried to write a lambda function to calculate the difference between elements (instead of using pandas built-in function diff) but it resulted in an error

s.apply(lambda i, j: j - i for i, j in zip(s[:-1], s[1:])
File ~\Anaconda3\lib\site-packages\pandas\core\apply.py:412, in Apply.agg_list_like(self)
    410 # if we are empty
    411 if not len(results):
--> 412     raise ValueError("no results")
    414 if len(failed_names) > 0:
    415     warnings.warn(
    416         depr_nuisance_columns_msg.format(failed_names),
    417         FutureWarning,
    418         stacklevel=find_stack_level(),
    419     )

ValueError: no results

What did the error mean? Is there a way to fix it, or a better method to achieve a similar result of applying built-in function diff?

Can I extend the fixed/new function to dataframe instead of series? Thanks

Nemo
  • 1,124
  • 2
  • 16
  • 39

0 Answers0